2

I have a GUI with many check boxes (in python). users should click many check boxes to run the application. I want to create a button that automatically select and "clicks" some predefined check boxes.

I know how to create the button, and also have the application "know" the check box is checked.

however, when looking at the GUI, the check boxes are left empty, so the user doesn't know which check box is checked. see below the check box definition:

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        QtCore.QObject.connect(self.legacyrunsens, 
        QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), legacychecksens)

so, I call legacychecksens(2) , but on the GUI the checkbox is not marked.

2
  • 1
    PyQt4, PyQt5, PySide or PySide2? Commented Jul 4, 2018 at 12:43
  • I'm using PyQt4 Commented Jul 4, 2018 at 13:12

1 Answer 1

3

The solution consists of connecting the clicked signal to the setChecked(True) method of the QCheckBox through functools.partial(), in the following part I show the example with PySide4

from PyQt4 import QtCore, QtGui
from functools import partial


class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        lay = QtGui.QVBoxLayout(self)

        button = QtGui.QPushButton("Default")
        lay.addWidget(button)

        options = ["A", "B", "C", "D", "E", "F"]
        default = ["A", "B", "C"] 

        for option in options:
            checkbox = QtGui.QCheckBox(option)
            lay.addWidget(checkbox)
            if option in default:
                button.clicked.connect(partial(checkbox.setChecked, True))


if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
Sign up to request clarification or add additional context in comments.

2 Comments

I'm using PyQt4
@MosheGabay if my answer helps you do not forget to mark it as correct, if you do not know how to do it, review the tour, that is the best way to thank

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.