How to check if a checkbox is checked in pyqt

Joshua Strot picture Joshua Strot · Dec 8, 2013 · Viewed 56.3k times · Source

I'm trying to make a conditional statement based on whether a checkbox is checked or not. I've tried something like the following, but it always returns as true.

self.folderactive = QtGui.QCheckBox(self.folders)
self.folderactive.setGeometry(QtCore.QRect(50, 390, 71, 21))
self.folderactive.setObjectName(_fromUtf8("folderactive"))
if self.folderactive.isChecked:
    folders.createDir('Desktop')
    print "pass"
elif not self.folderactive.isChecked:
    folders.deleteDir('Desktop')
    print "nopass"

Is there a way to get a bool value of whether a checkbox is checked or not?

Answer

mata picture mata · Dec 8, 2013

self.folderactive.isChecked isn't a boolean, it's a method - which, in a boolean context, will always evaluate to True. If you want the state of the checkbox, just invoke the method:

if self.folderactive.isChecked():
    ...
else:
    ...