35

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?

2 Answers 2

59

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:
    ...
Sign up to request clarification or add additional context in comments.

3 Comments

to avoid the if-then-else construct and keep track of the state myself, programatically, is there anything that emits a signal when a checkBox is checked (another when unchecked), like ``` self.checkBox.toggled.connect(self.calculate)``` ?
@Echeban Did you find out if there is such a thing, I was looking for it
4
x = self.folderactive.isChecked()

x will be True or False—a Boolean value.

(It's the brackets at the end that make the difference.)

2 Comments

but wont be dinamically updated in a script, or would it ??
It won't be. Should use .itemChanged for events

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.