Python Forum
Promoted QGroupBoxes take data one from the other
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Promoted QGroupBoxes take data one from the other
#1
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?
Reply
#2
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())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Video [PyQt] Get a executable file (.exe) from a .py file add a promoted class in a QWidget MiguelonReyes 0 614 Oct-17-2023, 11:43 PM
Last Post: MiguelonReyes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020