Python Forum

Full Version: Promoted QGroupBoxes take data one from the other
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I 've made a class:
from PyQt4 import QtGui


class myClass(QtGui.QGroupBox):
    my_children = []

    def __init__(self, parent):
        QtGui.QGroupBox.__init__(self, parent)

        self.button1 = QtGui.QPushButton('button1', self)
        self.button1.clicked.connect(self.button1_clicked)

    def button1_clicked(self):
        print('my name is: ', self.objectName())
        self.getChildren()

    def getChildren(self):
        if not self.my_children:
            children = self.children()
            for widget in children:
                self.my_children.append(widget)
                print('myClass:getChildren=', widget.objectName())
        else:
            print('full of children!! And they are:')
            for widget in self.my_children:
                print('myClass:getChildren=', widget.objectName())
I promote two QGroupBoxes in to myClass:
When I click on the button of the first GroupBox, I get it's children.
All ok.
But when I click on the button of the second GroupBox, I get the children of the first!
Like if there is one only instance of the class!

What have I done wrong?
Ok, I found it!
The problem was that I declared my variable outside of the class, so it was an object variable instead of a class variable!
I moved it in the class constructor and works fine!

from PyQt4 import QtGui

class myClass(QtGui.QGroupBox):    

   def __init__(self, parent):
       QtGui.QGroupBox.__init__(self, parent)

       self.my_children = []

       self.button1 = QtGui.QPushButton('button1', self)
       self.button1.clicked.connect(self.button1_clicked)

   def button1_clicked(self):
       print('my name is: ', self.objectName())
       self.getChildren()

   def getChildren(self):
       if not self.my_children:
           children = self.children()
           for widget in children:
               self.my_children.append(widget)
               print('myClass:getChildren=', widget.objectName())
       else:
           print('full of children!! And they are:')
           for widget in self.my_children:
               print('myClass:getChildren=', widget.objectName())