How to find an object by name in pyqt?

后端 未结 2 1242
心在旅途
心在旅途 2020-12-10 14:03

I have a list of dictionaries:

globalParams = [{\'attr\':\'enabled\',\'ctrl\':\'checkBoxEnabled\',\'type\':\'checkBox\'},
                {\'attr\':\'colorMo         


        
相关标签:
2条回答
  • 2020-12-10 14:18

    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()}')
    
    
    0 讨论(0)
  • 2020-12-10 14:41

    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.

    0 讨论(0)
提交回复
热议问题