Module Qt Designer Check Box

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

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. QCheckBox also inherits from QAbstractButton which is why the clicked() signal is available as well QAbstractButton Class | Qt Widgets 5.15.3.

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 (checked, unchecked, partially checked) rather than just a simple boolean as provided by the clicked signal.

def setup(self):

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

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):

    introduce that branch of code, like an import, and allow the ability to use checkState later on (in def 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!

  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 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 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.

1 Like