Search code examples
variablescountintegerqmlcycle

How to switch between multiple Values in a cycle?


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!


Solution

  • 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.