Python Forum
[PyQt] Get class 's children on init - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyQt] Get class 's children on init (/thread-1466.html)



Get class 's children on init - panoss - Jan-05-2017

class DataForm(QtGui.QGroupBox):
   def __init__(self, parent, task=None):
       QtGui.QWidget.__init__(self, parent)   

       self.labelHello = QtGui.QLabel(self)
       self.labelHello.setText("This is My Widget")


       children = self.children()
I 'm trying to get the children in the init of the class.
It' a GroupBox with two text boxes in it.
But it gets no children, returns empty.


RE: Get class 's children on init - nilamo - Jan-05-2017

I can't find any documentation for a .getChildren(), but there is a .findChildren().  Have you tried that?
Example of usage: http://nullege.com/codes/show/src@g@i@git-cola-HEAD@cola@[email protected]/33/PyQt4.QtGui.QGroupBox.findChildren
 # assume "expanded = True"

       for widget in self.findChildren(QtGui.QWidget):
           widget.setHidden(not expanded)



RE: Get class 's children on init - panoss - Jan-05-2017

Sorry, I meant: children = self.children()
Yes I've tried what you suggest, it also fails.

Maybe I should get the children not in init but in some other function (I wonder which one?) because they, may, have not yet been created?


This class 'promotes' a QGroupBox to 'DataForm', maybe my constructor is incorrect?
DataForm(QtGui.QGroupBox):

   def __init__, parent):
       QtGui.QGroupBox.__init__(self, parent)
I tried in showEvent, now it works BUT brings ALL the widgets of the form!!! and not only the ones in my widget (my GroupBox):


class DataForm(QtGui.QGroupBox):
   signal_hided = QtCore.pyqtSignal()
   signal_shown = QtCore.pyqtSignal()
   def __init__(self, parent):
       QtGui.QGroupBox.__init__(self, parent)

def showEvent(self, event):
   super(DataForm, self).showEvent(event)
   self.signal_shown.emit()

   children = self.children()
   for widget in children:
       print('DataForm:showEvent=', widget.objectName()) 



RE: Get class 's children on init - panoss - Jan-06-2017

On the mousePressEvent  I get the correct children!!


def mousePressEvent (self, event):
   children = self.findChildren(QtGui.QTextEdit)  
   for widget in children:
       print('DataForm:getChildren=', widget.objectName())
So maybe it's just in which event I use this code?
In which event do you think I should put it?

Init is rejected, paint event is rejected, showEvent rejected too...