I want to switch between multiple values in a cycled kind of way. For example my variable "Counter" is initialised with 1 and by mouseclick i want it to switch to 2. Another mouseclick will change it to 3 and the next click will repeat the cycle by 1.
How do i do it by using QML?
Thank you in advance!
CodePudding user response:
onClicked{
switch(counter) {
case 1:
counter=2
break;
case 2:
counter=3
break;
case 3:
counter=1
break;
default:
console.log("counter out of rotation)
}
}
A switch-case statement should do the trick inside an "onClicked" handler of a button. You can also do it in many different other ways. Like incrementing counter by 1 each time you press the button and add a if statement that sets counter to 1 again when it reaches the value of 4.
CodePudding user response:
Button {
id: button
readonly property int counterStart: 1
readonly property int counterEnd: 3
property int counter: button.counterStart
onClicked: {
console.log(button.counter)
button.counter = Math.max(button.counterStart, button.counter % (button.counterEnd 1))
}
}