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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
What's wrong with using the if statement in the function? I'm not understanding your editted question.
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!
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 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 for details...
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")

Comments

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.