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
A checkbox can be selected on start (by default its off), by calling the method . setChecked. Each time a checkbox is clicked, it emits the signal stateChanged(). This signal can be connected to a callback function (which pyqt names slot), and you can run a Python function on clicked.
To create a checkbox, first import QCheckBox from the module PyQt5. QtWidgets . Then create a new checkbox. As parameter you can set the text that is shown next to the checkbox.
PyQt5 – isChecked() method for Check BoxisChecked method is used to know if the check box is checked or not. This method will return true if the check box is checked else it will return false. If we use this method after creating the check box it will always return False as by default check box is not checked.
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:
Checking the box, changes the label:
Unchecking the box, also changes the label:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With