@Erik
So, is_checked
is used to pass information ‘into’ the method the_button_was_checked
.
Yes, howerver nothing prevents you for doing:
def the_button_was_toggled(self, is_checked):
print("Checked")
Beside your IDE will complains is_checked
is pass but never used, code will working! But parameter is_checked
is crucial and required by Qt/Pyside, later explanation below
To make ever things even weirder, callback method can by just regular functions:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
...
button.clicked.connect(the_button_was_toggled)
...
def the_button_was_toggled(is_checked):
print("Checked?", is_checked)
And again, it will work!
But how on earth is this possible? This is the first time this parameter appears in the script. Its value is still unknown!
From static code analysis kind of yes, but during runtime value of is_checked
is provided by Qt/Pyside internals, when click event is triggered Qt will run callback the_button_was_toggled
it will check current state isChecked()
of button
and pass to callback method.
Similarity of spelling of is_checked
variable/parameter and isChecked()
method of QPushButton
is just accidental (or better, it just describes what is going to happened).
I also considered that is_checked
might be a Boolean attribute of the MainWindow
class, or one of its parent classes. It might be used to access one of the internal values of the object. However, in that case I would expect the dot notation to be used.
If I understand your idea correctly it not necessary or not needed complication. Value is_checked
and variable itself is kind of “temporary” or “relevant” only short time when event is triggered and callback executed.
Instead storing state of button as class attribute you can always just check with:
button = QPushButton("Press Me!")
button.setCheckable(True)
if button.isChecked():
print("Checked")
else:
print("Not checked")
I couldn’t find any reference to is_checked on the Qt for Python web site.
You will not find it, it is just name, as you noticed you can change name of parameter to anything like my_super_awesome_var
, so why you expect to find this name inside documentation . Exactly same situation is, when you:
my_button = QPushButton("Press Me!")
you don’t expect to find my_button
in documentation
Peace
Happy coding