[PyQt] findChildren: setting visibility problems
Chris Wood
c.c.wood at gmail.com
Mon Oct 13 20:12:05 BST 2014
I've found that where I have a QLabel as a child of my QWidget, and a
QLabel which is a child of a QGroupBox which is a child of my QWidget, and
I toggle the visibility of all the children of the QWidget via the click
action of a button:
def toggleVis(self):
self.results = findChildren((QLabel,QGroupBox))
print self.results
for w in self.results:
w.setVisible(not w.isVisible())
After hiding the objects initially, and clicking the button again, the
QLabel which is a child of the QGroupBox does not reappear, but all 3
objects are listed when self.results is printed (full code at end of
email). I expected this to work, since findChildren is recursive.
However, it *does* work if I reverse the list (for w in
reverse(self.results)). I get the same behaviour on both Unix (Fedora 20)
and Windows 7. Is this documented? Perhaps a different way round this would
be to specify names (I could probably use some regex to ensure I capture
the objects I want), but wanted to make sure I hadn't missed something
obvious from the docs first...
Cheers,
Chris
------
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
vbox = QtGui.QVBoxLayout()
gb = QtGui.QGroupBox()
button = QtGui.QPushButton("Hide")
button.clicked.connect(self.toggle)
vbox.addWidget(button)
lbl = QtGui.QLabel("Test Label")
vbox.addWidget(lbl)
gbLayout = QtGui.QFormLayout()
lbl1 = QtGui.QLabel("Hello", gb)
gbLayout.addWidget(lbl1)
le1 = QtGui.QLineEdit(gb)
gbLayout.addWidget(le1)
gb.setLayout(gbLayout)
vbox.addWidget(gb)
gb.setTitle("Test")
self.setLayout(vbox)
self.show()
def toggle(self):
self.l = self.findChildren((QtGui.QGroupBox, QtGui.QLabel))
print self.l
for i in self.l:
i.setVisible(not i.isVisible())
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141013/8700f8d4/attachment.html>
More information about the PyQt
mailing list