6

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?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Dariusz
  • 960
  • 13
  • 36

2 Answers2

11

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
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
1

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.

Dariusz
  • 960
  • 13
  • 36