# Module Qt Designer Check Box

**URL:** <https://discourse.slicer.org/t/module-qt-designer-check-box/17172>\
**Category:** Support\
**Tags:** python, designer, module\
**Created:** [April 19, 2021, 1:55pm UTC](https://discourse.slicer.org/t/module-qt-designer-check-box/17172 "2021-04-19T13:55:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabriel](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gabriel/32/10526_2.png) [@Gabriel](https://discourse.slicer.org/u/Gabriel)\
**Post date:** [April 19, 2021, 1:55pm UTC](https://discourse.slicer.org/t/module-qt-designer-check-box/17172/1 "2021-04-19T13:55:20Z")

</div>

I am creating a module and have used Qt Designer to add in a checkbox.  
For now I have just added a print function to test the checkbox, later I want it to carry out a function when clicked on.

In the main PY file, under the “(modulename)widget” class. I have added the following code under  
def setup(self):  
self.ui.EnableFolderGrab.connect(‘clicked(bool)’, self.onEnableFolderGrabButton)

Then i have created another function in the same class.

def onButtonGrabber(self):  
print(“Hello this check box button is working”)

However this function runs (printing the text) every time I click the check box (whether checking or unchecking). When I’d only like it to print (activate the function) when the check box is clicked on, not when it is clicked off. Any help would be much appreciated.  
If there is a part of the Slicer Wiki that goes through this you could direct me to that would be great. I’ve gone through the documentation pdf and i believe most developer sections on the wiki but can’t find information regarding this area.

Many Thanks

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [April 19, 2021, 2:32pm UTC](https://discourse.slicer.org/t/module-qt-designer-check-box/17172/2 "2021-04-19T14:32:27Z")

</div>

Qt documentation will likely be the most helpful source when figuring out what signals are available for a QWidget object. You can see QCheckBox signals at [QCheckBox Class | Qt Widgets 5.15.3](https://doc.qt.io/qt-5/qcheckbox.html#signals). QCheckBox also inherits from QAbstractButton which is why the `clicked()` signal is available as well [QAbstractButton Class | Qt Widgets 5.15.3](https://doc.qt.io/qt-5/qabstractbutton.html#signals).

Based on what is available there isn’t a signal specifically for when it is checked versus unchecked, so you will have to handle it within your slot `onButtonGrabber`.

In the example below I use the signal `stateChanged` from the QCheckBox class which is a little more detailed in that it provides the [Qt CheckState](https://doc.qt.io/qt-5/qt.html#CheckState-enum) (checked, unchecked, partially checked) rather than just a simple boolean as provided by the `clicked` signal.

```python
def setup(self):
  self.ui.EnableFolderGrab.stateChanged.connect(self.onButtonGrabber)

def onButtonGrabber(self, checkState):
  if checkState == qt.Qt.Checked:
    print("This checkbox was checked")
  elif checkState == qt.Qt.Unchecked:
    print("This checkbox was unchecked")

```

---

<div class="post-metadata">

**Author:** ![Gabriel](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gabriel/32/10526_2.png) [@Gabriel](https://discourse.slicer.org/u/Gabriel)\
**Post date:** [April 19, 2021, 4:13pm UTC](https://discourse.slicer.org/t/module-qt-designer-check-box/17172/3 "2021-04-19T16:13:01Z")

</div>

Thank you very much, have been reading the links you’ve shared since your post and implemented the code and it works great!

(I think) I understand what you have done, but do you have a couple of Questions if you don’t mind.

1. Does including `.stateChanged` inside of  
` def setup(self):`  
` self.ui.EnableFolderGrab.stateChanged.connect(self.onButtonGrabber)`

2. How do you know to use `qt.Qt.`inside of  
`if checkState == qt.Qt.Checked:`

I understand both of these are needed and have played around with removing them to view the errors that causes.

P.S. For the original post I accidentally copied over code from a different button, hence the mix match between variable names. That’s a lesson to me on proper commenting and having unique and proper variable names.  
Also tried to copy you in using code separated text and will use hyperlinks too if needed!

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [April 19, 2021, 6:57pm UTC](https://discourse.slicer.org/t/module-qt-designer-check-box/17172/4 "2021-04-19T18:57:34Z")

</div>

1. Signals such as `stateChanged` send the `state` which is an `int` as an input to the slot (`onButtonGrabber`). It is `int` type because CheckState is an `int` enum where `qt.Qt.Checked` returns the value of 2. The documentation will tell you if there is an input it is passing to the slot or not. It will also tell you the type.

2. `qt.Qt`. as in `qt.Qt.Checked` is referencing the [Qt namespace](https://doc.qt.io/qt-5/qt.html) which is where the Qt CheckState is defined as linked in my above post. I think it is the syntax `qt.Qt` just based on how [PythonQt](https://github.com/MeVisLab/pythonqt) python bindings are generated. There isn’t really documentation saying to access the Qt namespace you can do `qt.Qt`, so just something to observe in examples or ask about on the forum as you have done.
