0

我在 Qt Designer 中制作了一个 GUI,我有 144 个复选框。我想用 QPushButton 将它们全部连接起来以选中和取消选中所有这些。

我怎样才能做到这一点?它们都在 QGridLayout 中。

他们是按照“趋势”命名的,所以我试着把他们的名字写在一个列表中,并调用列表中的每个项目来检查,但我没有做到。

这个例子或多或少像我所拥有的

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'check.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.checkBox_2 = QtGui.QCheckBox(Form)
        self.checkBox_2.setObjectName(_fromUtf8("checkBox_2"))
        self.gridLayout.addWidget(self.checkBox_2, 2, 0, 1, 2)
        self.checkBox = QtGui.QCheckBox(Form)
        self.checkBox.setObjectName(_fromUtf8("checkBox"))
        self.gridLayout.addWidget(self.checkBox, 2, 2, 1, 1)
        self.checkBox_3 = QtGui.QCheckBox(Form)
        self.checkBox_3.setObjectName(_fromUtf8("checkBox_3"))
        self.gridLayout.addWidget(self.checkBox_3, 3, 0, 1, 2)
        self.checkBox_4 = QtGui.QCheckBox(Form)
        self.checkBox_4.setObjectName(_fromUtf8("checkBox_4"))
        self.gridLayout.addWidget(self.checkBox_4, 3, 2, 1, 1)
        self.checkBox_5 = QtGui.QCheckBox(Form)
        self.checkBox_5.setObjectName(_fromUtf8("checkBox_5"))
        self.gridLayout.addWidget(self.checkBox_5, 1, 2, 1, 1)
        self.checkBox_6 = QtGui.QCheckBox(Form)
        self.checkBox_6.setObjectName(_fromUtf8("checkBox_6"))
        self.gridLayout.addWidget(self.checkBox_6, 1, 0, 1, 1)


        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.checkBox_2.setText(_translate("Form", "CheckBox", None))
        self.checkBox.setText(_translate("Form", "CheckBox", None))
        self.checkBox_3.setText(_translate("Form", "CheckBox", None))
        self.checkBox_4.setText(_translate("Form", "CheckBox", None))
        self.checkBox_5.setText(_translate("Form", "CheckBox", None))
        self.checkBox_6.setText(_translate("Form", "CheckBox", None))




if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

我想制作一个按钮来检查和取消选中所有这些,而不必键入 ao the connects

4

1 回答 1

1

必须进行许多连接很乏味,但是正如您所说,您可以使用 创建一个对象列表findChildren,但首先要在设计中添加一个按钮。

class Ui_Form(object):
    def setupUi(self, Form):
        ...
        self.gridLayout.addWidget(self.checkBox_6, 1, 0, 1, 1)


        self.btn = QtGui.QPushButton("Check", Form)
        self.gridLayout.addWidget(self.btn, 4, 0, 1, 3)


        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
        ...

然后我们在另一个类中实现逻辑,我们findChildren用来获取QCheckBox

class Widget(QtGui.QWidget, Ui_Form):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setupUi(self)
        self.checkBoxList = self.findChildren(QtGui.QCheckBox)
        self.btn.clicked.connect(self.onClicked)

    def onClicked(self):
        state = self.sender().text() == "Check"
        for btn in self.checkBoxList:
            btn.setChecked(state)

        self.sender().setText("Uncheck" if state else "Check")

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = Widget()
    Form.show()
    sys.exit(app.exec_())
于 2017-08-12T17:50:44.723 回答