Wednesday, April 24, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 2445  / 2 Years ago, thu, june 30, 2022, 10:50:50

In QML, how do you toggle boolean non explicitly?



For example:



Whenever a button is pressed, a boolean property will simply switch to its other value.



If false, it will be come true.



If true, it will become false.


More From » ubuntu-touch

 Answers
7

I don't know if there's a magic method for QML/Qt, but I usually just use a "toggle_it" function in python that looks something like:



def toggle_it_cb(the_bool):
the_bool = not the_bool
return the_bool

my_bool = False
my_bool = toggle_it_cb(my_bool)
print(my_bool)
my_bool = toggle_it_cb(my_bool)
print(my_bool)


It could be done better if it were a class attribute. No need to return to assign, etc.



Almost every language ever has a negation operator (usually !() or similar).


[#25869] Saturday, July 2, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dersol

Total Points: 78
Total Questions: 100
Total Answers: 124

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
;