I am trying to add a separate data structure to each table widget in my stacked tree widget list. When I access the selected table widget, I want this information to be available to the code managing the stacked widget.
I am primarily a C# developer, so basically I'm looking at something like the .tag attribute of a Data Grid View.
I have been searching google for some time and it doesn't seem possible. I'm looking into a parallel list of data structures corresponding to each entry in the stacked list, but don't know how to determine the data structure list index from the selected tree widget
PySide (just like PyQt) is a binding that exposes Qt objects by wrapping them into Python objects. In Python, "everything is an object", which can also be read as "everything is an object
instance".
When a Qt object is made available to Python, it actually is an instance of a object
subclass that "wraps" the Qt object by providing an interface to its members, including its functions.
Except for basic protected types, any Python object can have any arbitrary attribute which can be set and accessible through the dot notation, so the solution is quite simple: just create the attribute.
self.myTableWidget = QTableWidget()
self.myTableWidget.someValue = 'hello'
It's important to keep two aspects in mind, though.
First of all, one should always be very careful about creating such attributes: as a rule of thumb (valid for Python in general, not just Qt bindings), custom members should not overwrite existing ones if they have different behavior or purpose, because they may be necessary in other places within the code or, more importantly, they can be virtual functions that are internally called by Qt and, as such, should respect the expected behavior (including function arguments and returned values). It's good habit to always check the official Qt API documentation for the class at hand until you're experienced enough: just check the "List of all members, including inherited members" link near the top of every documentation page.
Then, Qt has its own way of setting and accessing properties (see the Qt property system) which, in some cases, may be preferable to pure Python attributes which are completely invisible to Qt, since they are actually part of the Python wrapper, not the Qt object. One typical case is the objectName
property, which can be used as a selector type in QSS (similarly to the id
attribute of CSS) or to search for child widgets using findChild()
.