PySide class variable cannot be changed

I have a class called Panel that is derived from PySide.QtGui.QWidget. I want to track how many panels I created. To do this, I introduce a class variable called count. A fragment of the class definition is as follows:

class Panel(QtGui.QWidget):
    count = 0
    def __init__(self, parent=None):
        super(Panel, self).__init__(parent)
        Panel.count += 1
        print(Panel.count)

but when I create new panels, only a sequence of zeros is printed. But I expect to see the sequence 1 2 3 ... In other words Panel.count += 1, it seems to be ignored without any warnings or errors.

When I do the same with another class that is derived from a regular Python object, not a QWidget, it works as expected.

Any ideas why this is?

+4
source share
4 answers

, Shiboken Python ++, PySide. type Shiboken.ObjectType ( print(Panel.__class__)). python, .

0

​​PySide, .

, PySide-1.2.1 ( , self):

>>> from PySide import QtGui
>>> app = QtGui.QApplication([])
>>> class Panel(QtGui.QWidget):
...     count = 0
...     def __init__(self, parent=None):
...         super(Panel, self).__init__(parent)
...         Panel.count += 1
...         print(Panel.count)
... 
>>> p1 = Panel()
1
>>> p2 = Panel()
2
>>> p3 = Panel()
3
>>> Panel.count
3
+1

: , QT. .

+1

, - , - . , , .

0

Source: https://habr.com/ru/post/1532523/


All Articles