Reputation: 129
I made a GUI in Qt Designer and I have 144 check boxes. I want to connect all of them with a QPushButton to check and uncheck all of them.
How can I do that? They are all inside of a QGridLayout.
They are named following a "trend", so I tried to write their names in a list and call each item of the list to check, but I did not manage.
This example is more or less like what I have
# -*- 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_())
I want to make a button to check and uncheck all of them without having to type ao the connect
s
Upvotes: 0
Views: 644
Reputation: 244282
It is tedious to have to make many connections, but as you say you can create an object list with findChildren
, but first add a button to the design.
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)
...
Then we implement the logic in another class and we use findChildren
to obtain the 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_())
Upvotes: 1