QTreeWidgetItem does not get Checked - python-3.x

I am trying to Check Tree items from a function after the Tree is initialized. Check marks never appear. I'm not sure if I need to 'refresh' the Tree or that I am not interacting with the right object.
I've read about this widget a lot and I see the same question more or less on several websites but the answers are 8+ years old, two pages of code or I just don't understand them.
What am I doing wrong here? Why are there no check marks showing up in the tree when I call checkActiveTreeItems()?
After a REST call, I want to update the checked items in the tree. This seems to be ok but the items never get checked.
def checkActiveTreeItems(self):
for label in self.transactie['transactie']['labels']:
test = QtWidgets.QTreeWidgetItem([label])
test.setCheckState(0, QtCore.Qt.Checked)
This is the function that creates the tree, I do not call it again after setting the QTreeWidgetItem to Checked in the function above.
def initTree(self):
print("initTree")
self.treeWidget = QtWidgets.QTreeWidget(self.gridLayoutWidget)
self.treeWidget.setSelectionMode(QtWidgets.QAbstractItemView.ContiguousSelection)
self.treeWidget.setObjectName("treeWidget")
inkomstenstructuur = requests.get(url="http://localhost:32769/_db/piekenpijp/incoming/inkomstenstructuur")
iscontent = json.loads(inkomstenstructuur.content)
self.inkomsten = iscontent
inkomstentree = QtWidgets.QTreeWidgetItem(["Inkomsten"])
for groep in iscontent:
child = QtWidgets.QTreeWidgetItem([groep['groep']])
inkomstentree.addChild(child)
for categorie in groep['categorien']:
child2 = QtWidgets.QTreeWidgetItem([categorie])
child2.setCheckState(0, QtCore.Qt.Unchecked)
child.addChild(child2)
uitgavenstructuur = requests.get(url="http://localhost:32769/_db/piekenpijp/incoming/uitgavenstructuur")
uscontent = json.loads(uitgavenstructuur.content)
self.uitgaven = uscontent
uitgaventree = QtWidgets.QTreeWidgetItem(["Uitgaven"])
for groep in uscontent:
child = QtWidgets.QTreeWidgetItem([groep['groep']])
uitgaventree.addChild(child)
for categorie in groep['categorien']:
child2 = QtWidgets.QTreeWidgetItem([categorie])
child2.setCheckState(0, QtCore.Qt.Unchecked)
child.addChild(child2)
self.treeWidget.itemChanged.connect(self.vinkje)
self.treeWidget.resize(500, 200)
self.treeWidget.setColumnCount(1)
self.treeWidget.setHeaderLabels(["Categorie"])
self.treeWidget.addTopLevelItem(inkomstentree)
self.treeWidget.addTopLevelItem(uitgaventree)
self.treeWidget.expandItem(uitgaventree)
self.gridLayout.addWidget(self.treeWidget, 0, 2, 1, 1)

Your current method creates a new item, sets its check-state, and then throws it away without using it. Instead, you need to find the items in the tree that you have already created:
def checkActiveTreeItems(self):
for label in self.transactie['transactie']['labels']:
for item in self.treeWidget.findItems(label, Qt.MatchExactly):
item.setCheckState(0, QtCore.Qt.Checked)

Related

XGetWindowProperty and ctypes

Question
I'm trying to find NET_WM_NAME property for each of the window/client that X11 reports. Problem is that there's nothing returned - number of items is 0 and returned data results in empty string. I've looked at multiple code examples through out github and examples written in C and C++ , specifically Why is XGetWindowProperty returning null? as well as Xlib XGetWindowProperty Zero items returned , however I cannot find where is the problem with my code. Seemingly everything is fine, order of parameters passed to XGetWindowProperty function is in accordance with documentation, and the function returns success status, but results are empty. Where is the problem with my code ?
Code
Below is the code I am working with. The issue is xgetwindowproperty function. The other parts below it work fine, and are provided only for completeness.
#! /usr/bin/env python3
import sys
from ctypes import *
def xgetwindowproperty(display,w):
actual_type_return = c_ulong()
actual_format_return = c_int()
nitems_return = c_ulong()
bytes_after_return = c_ulong()
prop_return = POINTER(c_ubyte)()
wm_name = Xlib.XInternAtom(display,'_NET_WM_NAME',False)
utf8atom = Xlib.XInternAtom(display,'UTF8_STRING',False)
print('_NET_WM_NAME',wm_name, 'UTF8_STRING',utf8atom)
# AnyPropertyType = c_long(0)
status = Xlib.XGetWindowProperty(
display,
w,
wm_name,
0,
65536,
False,
utf8atom,
byref(actual_type_return),
byref(actual_format_return),
byref(nitems_return),
byref(bytes_after_return),
byref(prop_return)
)
print(nitems_return.value) # returns 0
# empty string as result
print( 'Prop', ''.join([ chr(c) for c in prop_return[:bytes_after_return.value] ]) )
Xlib.XFree(prop_return)
print('#'*10)
# -------
Xlib = CDLL("libX11.so.6")
display = Xlib.XOpenDisplay(None)
if display == 0:
sys.exit(2)
w = Xlib.XRootWindow(display, c_int(0))
root = c_ulong()
children = POINTER(c_ulong)()
parent = c_ulong()
nchildren = c_uint()
Xlib.XQueryTree(display, w, byref(root), byref(parent), byref(children), byref(nchildren))
for i in range(nchildren.value):
print("Child:",children[i])
xgetwindowproperty(display,children[i])

Qt3D Window and two layers, viewports, layerfilters, etc

I want to code a Qt3DWindow where I would have two QLayer-s, QLayerFilter-s, QViewport-s, etc. I need this because I want to render entities in QViewport 1 in a usual way and entities in QViewport 2 always visible (even when they are actually behind entities 1).
Here is my code:
class Window(Qt3DExtras.Qt3DWindow):
def __init__(self):
super().__init__()
self.root_entity = Qt3DCore.QEntity()
self.entity_1 = Qt3DCore.QEntity(self.root_entity)
self.entity_2 = Qt3DCore.QEntity(self.root_entity)
self.setRootEntity(self.root_entity)
# some other code
self.surselector = Qt3DRender.QRenderSurfaceSelector()
self.surselector.setSurface(self)
# Viewport 1
self.viewport_1 = Qt3DRender.QViewport(self.surselector)
self.layerfilter_1 = Qt3DRender.QLayerFilter(self.viewport_1)
self.layer_1 = Qt3DRender.QLayer(self.entity_1)
self.entity_1.addComponent(self.layer_1)
self.layer_1.setRecursive(True)
self.layerfilter_1.addLayer(self.layer_1)
self.cameraSelector_1 = Qt3DRender.QCameraSelector(self.layerfilter_1)
self.clearBuffers_1 = Qt3DRender.QClearBuffers(self.cameraSelector_1)
self.cameraSelector_1.setCamera(self.camera())
self.clearBuffers_1.setBuffers(Qt3DRender.QClearBuffers.AllBuffers)
# Viewport 2
self.viewport_2 = Qt3DRender.QViewport(self.surselector)
self.layerfilter_2 = Qt3DRender.QLayerFilter(self.viewport_2)
self.layer_2 = Qt3DRender.QLayer(self.entity_2)
self.entity_2.addComponent(self.layer_2)
self.layer_2.setRecursive(True)
self.layerfilter_2.addLayer(self.layer_2)
self.cameraSelector_2 = Qt3DRender.QCameraSelector(self.layerfilter_2)
self.clearBuffers_2 = Qt3DRender.QClearBuffers(self.cameraSelector_2)
self.cameraSelector_2.setCamera(self.camera())
# The line below causes that no entity appears
self.clearBuffers_2.setBuffers(Qt3DRender.QClearBuffers.AllBuffers)
# The code below is what make viewport 2 / layer 2 / layerfilter 2 different from view port 1 / etc.
self.rendstate_2 = Qt3DRender.QRenderStateSet(self.clearBuffers_2)
self.rendstate_2.setParent(self.clearBuffers_2)
depth = Qt3DRender.QDepthTest()
depth.setDepthFunction(Qt3DRender.QDepthTest.LessOrEqual)
self.rendstate_2.addRenderState(depth)
self.setActiveFrameGraph(self.surselector)
If I understand correctly you want both viewports be drawn over each other (i.e. not in left and right half), while the first one draws all normal entities and the second one the ones that are always visible.
First of all, I think you don't need the viewports. Qt defaults this to the rectangle (0, 0, 1, 1) if none is present, but I'm not sure about this.
The actual issue is this line
self.clearBuffers_2.setBuffers(Qt3DRender.QClearBuffers.AllBuffers)
because here you delete the color buffer as well, which deletes everything the first viewport rendered. Try setting it to Qt3DRender.QClearBuffers.DepthBuffer. This only clears your depth buffer.The entites of the second viewport will be drawn ontop of the rendering of the first viewport.

GML room_goto() Error, Expecting Number

I'm trying to make a game that chooses a room from a pool of rooms using GML, but I get the following error:
FATAL ERROR in action number 3 of Create Event for object obj_control:
room_goto argument 1 incorrect type (5) expecting a Number (YYGI32)
at gml_Object_obj_control_CreateEvent_3 (line 20) - room_goto(returnRoom)
pool = ds_list_create()
ds_list_insert(pool, 0, rm_roomOne)
ds_list_insert(pool, 1, rm_roomTwo)
ds_list_insert(pool, 2, rm_roomThree)
ds_list_insert(pool, 3, rm_roomFour)
var returnIndex;
var returnRoom;
returnIndex = irandom(ds_list_size(pool))
returnRoom = ds_list_find_value(pool, returnIndex)
if (ds_list_size(pool) == 0){
room_goto(rm_menu_screen)
}else{
room_goto(returnRoom)
}
I don't get the error message saying it's expecting a number.
This is weird indeed... I think this should actually work.. But I have no GM around to test :(
For now you can also solve this using "choose". This saves a list (and saves memory, because you're not cleaning up the list by deleting it - thus it resides in memory)
room_goto(choose(rm_roomOne, rm_roomTwo, rm_roomThree, rm_roomFour));
choose basically does exactly what you're looking for. Might not be the best way to go if you're re-using the group of items though.

The Button.connect syntax in Genie

I want to apply a certain behaviour to a label. When a lateral button is clicked, the corresponding label should rotate 90 degrees. It can be easily done in vala, but I can't discover the particular syntax on genie.
The vala code I am trying to reproduce comes from elementary OS getting started guide:
hello_button.clicked.connect(() =>
{
hello_label.label = "Hello World!";
hello_button.sensitive = false;
});
rotate_button.clicked.connect(() =>
{
rotate_label.angle = 90;
rotate_label.label = "Verbal";
rotate_button.sensitive = false;
});
I actually managed to reproduce almost entirely the code in Genie, for the exception of the rotation. Here is how far I got:
/* ANOTHER GTK EXPERIMENT WITH GENIE BASED ON ELEMENTARY INTRODUCTORY PAGE
** compile with valac --pkg gtk+03.0 layoutgtkexample.gs */
[indent=4]
uses Gtk
init
Gtk.init (ref args)
var window = new Gtk.Window()
window.title = "Hello World!"
window.set_border_width(12)
var layout = new Gtk.Grid ()
layout.column_spacing = 6
layout.row_spacing = 6
var hello_button = new Gtk.Button.with_label("Say Hello")
var hello_label = new Gtk.Label("Hello")
var rotate_button = new Gtk.Button.with_label ("Rotate")
var rotate_label = new Gtk.Label("Horizontal")
// add first row of widgets
layout.attach (hello_button, 0, 0, 1,1)
layout.attach_next_to (hello_label, hello_button, PositionType.RIGHT, 1, 1)
// add second row of widgets
layout.attach(rotate_button, 0,1,1,1)
layout.attach_next_to(rotate_label, rotate_button, PositionType.RIGHT, 1, 1)
window.add(layout)
hello_button.clicked.connect(hello_pushed)
rotate_button.clicked.connect(rotate_pushed)
window.destroy.connect(Gtk.main_quit)
window.show_all ()
Gtk.main ()
def hello_pushed (btn:Button)
btn.label = "Hello World!"
btn.sensitive = false
def rotate_pushed (btn:Button)
btn.label = "Vertical"
//btn.angle = 90
btn.sensitive = false
The problem is to do with where identifiers are valid and is known as "scope".
The Vala example makes use of an anonymous function, also called a lambda expression in Vala. An anonymous function can be a "closure", when the variables in the scope that defines the anonymous function are also available within the anonymous function. This is useful because the callback occurs after the original block of code has been run, but the variables are still available within the callback. So in the Vala example, where both the button and label are defined in the enclosing scope, the button and label are also available in the callback anonymous function.
Unfortunately Genie isn't able to parse anonymous functions as function arguments, in this case within the connect() call. Although some work has been done on this in 2015. So you have rightly used a function name instead. The problem is the callback only passes the button as an argument and not the adjacent label. So to make the label available within the callback function we could use a class:
/* ANOTHER GTK EXPERIMENT WITH GENIE BASED ON ELEMENTARY INTRODUCTORY PAGE
** compile with valac --pkg gtk+-3.0 layoutgtkexample.gs */
[indent=4]
uses Gtk
init
Gtk.init (ref args)
new RotatingButtonWindow( "Hello World!" )
Gtk.main ()
class RotatingButtonWindow:Window
_hello_label:Label
_rotate_label:Label
construct( window_title:string )
title = window_title
set_border_width(12)
var layout = new Grid ()
layout.column_spacing = 6
layout.row_spacing = 6
// add 'hello' row of widgets
var hello_button = new Button.with_label("Say Hello")
_hello_label = new Label("Hello")
layout.attach (hello_button, 0, 0, 1,1)
layout.attach_next_to (_hello_label, hello_button, PositionType.RIGHT, 1, 1)
// add 'rotate' row of widgets
var rotate_button = new Button.with_label ("Rotate")
_rotate_label = new Label("Horizontal")
layout.attach(rotate_button, 0,1,1,1)
layout.attach_next_to(_rotate_label, rotate_button, PositionType.RIGHT, 1, 1)
add(layout)
hello_button.clicked.connect(hello_pushed)
rotate_button.clicked.connect(rotate_pushed)
destroy.connect(Gtk.main_quit)
show_all ()
def hello_pushed (btn:Button)
_hello_label.label = "Hello World!"
btn.sensitive = false
def rotate_pushed (btn:Button)
_rotate_label.label = "Vertical"
_rotate_label.angle = 90
btn.sensitive = false
A few notes:
By placing the definitions of the _hello_label and _rotate_label within the scope of the class they become available to all the functions defined in the class. Definitions like this are often called "fields". The underscore means they are not available outside the class, so in the example you cannot access them from init
construct() is called when the object is created, in the example the line new RotatingButtonWindow( "Hello World!" ) instantiates the object. If you repeat the line you will have two separate windows, that is two instances of the RotatingButtonWindow data type
You will notice that the RotatingButtonWindow type is also defined as a Window type. This means it is adding more detail to the Gtk.Window class. This is why title and set_border_width() can be used within the new class. They have been "inherited" from the parent Gtk.Window class
By using the Gtk namespace with uses Gtk we don't need to prefix everything with Gtk
As your Gtk application gets more complex you probably want to look at GtkBuilder. That allows windows and widgets to be laid out in an external file. Then use GResource to build the file into the binary of your application so there is no need to distribute the UI file separately.

Lua string from file

I'm trying to make a system which backs up and restores points for a gameserver, so it can safely restart without loosing anything.
I have made a script to do just this and the actual backing up part works fine, but the restore part does not.
This is the script that runs if 'Backup(read)' is used (Backup(write) works perfectly as it is designed to do):
if (source and read) then
System.LogAlways("[System] Restoring serverdata from file 'backup.CHK'");
for line in source:lines() do
Backup = {};
Backup.Date = (Date or line:match("File Last Modified: (.-)"));
Backup.Time = (Time or line:match("time: (.-)"));
US = tonumber((US or line:match("us: (.-)")));
NK = tonumber((NK or line:match("nk: (.-)")));
local params = {class = "Player";
position = {x = 1, y = 1, z = -1000};
Respawn = { bRespawn = 0; nTimer =0; bUnique = 1; };
bUsable = 0;
orientation = {0, 90, 135};
name = "BackupEntity"; };
local ent = System.SpawnEntity(params);
g_gameRules.game:SetTeam(1, ent.id);
g_gameRules.game:SetSynchedEntityValue(playerId, 100, (NK/3));
g_gameRules.game:SetTeam(2, ent.id);
g_gameRules.game:SetSynchedEntityValue(playerId, 100, (US/3));
System.RemoveEntity(params);
end
source:close();
return;
end
I'm not sure what I'm doing wrong,and most sites that I have looked at don't help that much. The problem is that it's not reading any values from the file.
Any help will be appreciated :).
Edit:
The reason that we have to divide the score by 3 is because the server multiplies all scores by 3. If we were not to divide it by 3, then the score will always be 3 times larger on each restore.
Example contents of the backup.CHK file:
The server is dependent on this file, and writes to it every hour. Please do not edit.
File Last Modified: 11/07/2013
This file was generated by the servers' autobackup system.
--------------------------
time: 22:51
us: 453445
nk: 454567
A couple of ideas of what might be causing the problem:
Use of (.-) lazy matching which matches the shortest pattern possible -- this can include an empty string. Usually, you want to make the pattern as specific as possible while still matching the required possible inputs. eg. It looks like (%d+) for us and nk is an appropriate fit.
The for line in source:lines() do reads one line at a time. That necessarily means not all the variables are going to be set inside the loop. Yet everything starting at local params and down uses those variables as if they were. It seems to me that section of code shouldn't even be in the loop.
Lastly, have you considered saving the Backup file as just another lua file? Doing so means you can let lua do the heavy lifting for you and you won't have to bother parsing it yourself. That also minimizes the risk for error.

Resources