Dariusz
Dariusz

Reputation: 1054

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?

Upvotes: 7

Views: 4508

Answers (2)

ekhumoro
ekhumoro

Reputation: 120638

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

Upvotes: 11

Dariusz
Dariusz

Reputation: 1054

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.

Upvotes: 2

Related Questions