10

1) I have a checkbox called "ch_check" in my UI created with Qt designer that needs to be tested

2) There is also a button, "bt_button", which triggers a simple function:

self.dlg.bt_button.clicked.connect(self.doCheck)

3) The function:

def doCheck(self):
    if ch_check.isChecked():
        self.dlg.le_text.setText("Hello")
    else:
        self.dlg.le_text.setText("Nope")

However I can't figure out how to reference the box properly. How would I do that? Do I need to connect the checkbox somehow first? All the examples I found so far use checkboxes to fire off functions and whatnot while completely ignoring this basic usage. I found this question but it's not answering how to address the existing checkbox: How to check if a checkbox is checked in pyqt

Community
  • 1
  • 1
MapEngine
  • 553
  • 1
  • 9
  • 21

2 Answers2

23

You can do this utilizing the StateChanged signal. For this example we have a simple .ui and a simple .py file:

The .ui file defines two widgets. A check box (ch_check) and a single QLabel (my_label)

The python file:

from PyQt4 import QtCore
from PyQt4 import QtGui 
import sys
from test_ui import Ui_MainWindow

class CheckDialog(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        # Set up the user interface from Designer.
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.ch_check.stateChanged.connect(self.state_changed)

    def state_changed(self, int):
        if self.ui.ch_check.isChecked():
            self.ui.my_label.setText("CHECKED!")
        else:
            self.ui.my_label.setText("UNCHECKED!")


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = CheckDialog()
    window.show()
    sys.exit(app.exec_())

Explanation:

We set up our signal with this line:

self.ui.ch_check.stateChanged.connect(self.state_changed)

When the state of the checkbox changes, it will call the state_changed function. This is where your logic to check whether the box is checked or unchecked goes.

def state_changed(self, int):
    if self.ui.ch_check.isChecked():
        self.ui.my_label.setText("CHECKED!")
    else:
        self.ui.my_label.setText("UNCHECKED!")

In the function, we determine if the check box has been checked. If so, we change our label to say "CHECKED", if it is unchecked the label changes to "UNCHECKED".


Example:

When the application is launched the UI looks like this:

Newly opened app

Checking the box, changes the label:

Checked checkbox

Unchecking the box, also changes the label:

Unchecked checkbox

Andy
  • 49,085
  • 60
  • 166
  • 233
  • That's exactly what I'm not trying to do. I just want to check the checkbox status. Nothing else. Updated the question for more clarity, hopefully. – MapEngine May 26 '15 at 21:26
  • What's wrong with using the if statement in the function? I'm not understanding your editted question. – Andy May 26 '15 at 21:31
  • Your answer is fine, but not what I was looking for. My problem was much simpler. I guess I made it sound overly complicated, I only needed to add **self.dlg.** to my if.. d'oh! – MapEngine May 26 '15 at 21:38
  • 1
    I have trouble understanding where `int` parameter came form in `def state_changed(self, int):` we have not passed anything to `self.state_changed` so why should `int` be there as a parameter? – Sherafati May 31 '20 at 14:10
  • 1
    @Sherafati the `int` is required since the checkbox's signal `stateChanged` emits an int. The method dealing with the signal needs to accept this argument. See [doc.qt.io](https://doc.qt.io/qt-5/qcheckbox.html#stateChanged) for details... – stephinity Apr 05 '21 at 15:05
2

As mentioned in the comment above, I just made a small mistake. The correct code would be:

def doCheck(self):
    checker = self.dlg.ch_check
    if self.dlg.ch_check.isChecked():
        self.dlg.le_text.setText("Hello")
    else:
        self.dlg.le_text.setText("Nope")
MapEngine
  • 553
  • 1
  • 9
  • 21