Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping over widgets inside widgets inside layouts

Similar question to what we have here Loop over widgets in PyQt Layout but a bit more complex...

I have

QVGridLayout
   QGroupBox
      QGridLayout
         QLineEdit

I'd like to access QLineEdit but so far I dont know how to access children of QGroupBox

        for i in range(self.GridLayout.count()):
            item = self.GridLayout.itemAt(i)
            for i in range(item.count()):
                lay = item.itemAt(i)
                edit = lay.findChildren(QLineEdit)
                print edit.text()

Can any1 point me to right dirrection?

like image 547
Dariusz Avatar asked Feb 07 '23 03:02

Dariusz


2 Answers

When a widget is added to a layout, it automatically becomes a child of the widget the layout it is set on. So the example reduces to a two-liner:

for group in self.GridLayout.parentWidget().findChildren(QGroupBox):
    for edit in group.findChildren(QLineEdit):
        # do stuff with edit

However, findChildren is recursive, so if all the line-edits are in group-boxes, this can be simplified to a one-liner:

for edit in self.GridLayout.parentWidget().findChildren(QLineEdit):
    # do stuff with edit
like image 160
ekhumoro Avatar answered Feb 16 '23 16:02

ekhumoro


Sorted :

for i in range(self.GridLayout.count()):
     item = self.GridLayout.itemAt(i)
     if type(item.widget()) == QGroupBox:
          child =  item.widget().children()

I had to use item.widget() to get access to GroupBox. Hope this helps some1.

like image 26
Dariusz Avatar answered Feb 16 '23 15:02

Dariusz