Skip to content Skip to sidebar Skip to footer

Loop Over Widgets In Pyqt Layout

My question is somewhat related to Get a layout's widgets in PyQT but it's not a duplicate. Instead of looking for a high level strategic view of how to do it, I'm trying to under

Solution 1:

This is a very late response but I thought it might be useful for future refence. I was looking for the answer to this question also. But I wanted to identify widget types so I could handle them accordingly. Here is example code of what I found:

for widget in centralwidget.children():
    if isinstance(widget, QLineEdit):
        print "linedit: %s  - %s" %(widget.objectName(),widget.text())

    if isinstance(widget, QCheckBox):
        print "checkBox: %s  - %s" %(widget.objectName(),widget.checkState())

I hope that will be useful for someone someday. :)

Solution 2:

Just a comment,

items = (layout.itemAt(i) foriinrange(layout.count())) 
forwin items:
   doSomething(w)

I tried the first answer but I found that it returns a WidgetItem type, so actually I did a revision:

widgets = (layout.itemAt(i).widget() for i in range(layout.count())) 
for widget in widgets:
   if isinstance(widget, QLineEdit):
        print"linedit: %s  - %s" %(widget.objectName(), widget.text())
   if isinstance(widget, QCheckBox):
        print"checkBox: %s  - %s" %(widget.objectName(), widget.checkState())

Solution 3:

You could put the widgets into a generator like so:

items = (layout.itemAt(i) foriinrange(layout.count())) 
forwin items:
   doSomething(w)

If you end up using that a lot, you could sock that code into a generator function:

def layout_widgets(layout):
   return (layout.itemAt(i) for i in range(layout.count()))


for w in layout_widgets(layout):
   doSomething(w)

Solution 4:

Found a way if you can access the WindowHandler then you can get a dictionary of all the objects by simply doing:

widgets = vars(self)

Please note that I am using QT Designer (5.12) and don't have a full example showing how I added such widgets However, here is my example:

from PyQt5.QtWidgets import *
FORM_CLASS, _ = loadUiType(path.join(path.dirname('__file__'), "main.ui"))

classHMIWindowHandler(QMainWindow, FORM_CLASS):
    def__init__(self, parent=None):
        super(HMIWindowHandler, self).__init__(parent)
        QMainWindow.__init__(self)
        self.setupUi(self)

        # Prints out self to prove that it is a Window Handler.print(self)
        print(vars(self))

        # The vars function will return all of the items of the window handler as a dict.
        widgets = vars(self)

        # Prints the dict to prove class.print(type(widgets))

        # Iterates each key checks for the class type and then prints the objectsNamefor each_key, value_of_key in widgets.items():
            if (value_of_key.__class__.__name__ == 'QPushButton'or value_of_key.__class__.__name__ == 'QLabel'):
                print(value_of_key.objectName())

The result is:

<views.admin.HMIWindowHandler object at 0x0000017BAC5D9EE0>

dict_items([('centralwidget', <PyQt5.QtWidgets.QWidget object at 0x0000017BAC5D9F70>),
('windows_stacked', <PyQt5.QtWidgets.QStackedWidget object at 0x0000017BAC79A040>),
('brewing', <PyQt5.QtWidgets.QWidget object at 0x0000017BAC79A0D0>), 
('boil_image', <PyQt5.QtWidgets.QLabel object at 0x0000017BAC79A160>)])

<class'dict'>

boil_image
mash_image

Solution 5:

I believe you already have the simplest solution to this problem. PyQt (and PySide) are mostly just wrappers around the C++ API. They do add more pythonic support in a few areas--signals/slots, basic types such as QString, etc. but for the most part, everything is left exactly as it is in the C++ API.

Of course, this need not stop you from making your own more pythonic library..

Post a Comment for "Loop Over Widgets In Pyqt Layout"