Reputation: 966
I have a list of dictionaries:
globalParams = [{'attr':'enabled','ctrl':'checkBoxEnabled','type':'checkBox'},
{'attr':'colorMode','ctrl':'comboBoxColorMode','type':'comboBox'}]
'ctrl' - name of the control in the Qt window.
typically, the code is as follows:
self.checkBoxEnabled.checkState()
but checkBoxEnabled is an object. and i have only a string name 'checkBoxEnabled' and cannot use it...
how to find an object by name in pyqt? something like? self.GetObjectByName('checkBoxEnabled').checkState()
Upvotes: 17
Views: 33935
Reputation: 727
To find all the QCheckBox items in your UI you can run a loop through all the children of the widget like as below:
from PyQt5.QtWidgets import QCheckBox
from PyQt5.QtCore import QObject
class Mainwindow(QMainWindow):
def __init__(self):
# super().__init__()
self.checkbox = QCheckBox(self)
self.checkbox_1 = QCheckBox(self)
for checkstate in self.findChildren(QCheckBox):
print(f'get check state:{checkstate.checkState()}')
Upvotes: 2
Reputation: 40502
You can use QObject::findChild
method. In pyqt it should be written like this:
checkbox = self.findChild(QtGui.QCheckBox, "checkBoxEnabled")
self
should be a parent widget of the checkbox.
Upvotes: 28