Looping over widgets inside widgets inside layouts
Asked Answered
D

2

7

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?

Dendro answered 12/9, 2016 at 12:18 Comment(0)
R
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
Rohrer answered 13/9, 2016 at 16:49 Comment(0)
D
2

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.

Dendro answered 12/9, 2016 at 14:45 Comment(1)
what is gridlayoutErechtheus

© 2022 - 2024 — McMap. All rights reserved.