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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With