Qml runs javascript code after state change

I have several states that I use only to change some properties:

Item { id: props property int someProperty: 0 // ... states: [ State { name: "firstState" PropertyChange { target: props someProperty: 1 // ... } }, State { name: "secondState" PropertyChange { target: props someProperty: 1 // ... } } ] onStateChange: doSomething(someProperty) } 

Since different states may have the same value for someProperty , I cannot rely on the signal somePropertyChange , but I cannot even rely on onStateChange (as in the example), because when it runs the properties do not change.

So, how could I run doSomething() every time a state changes? Is there a better way to do such things with QML ?

+6
source share
2 answers

You can run the script using StateChangeScript .

 Item { id: props property int someProperty: 0 states: [ State { name: "firstState" PropertyChanges { target: props someProperty: 1 } StateChangeScript { name: "firstScript" script: console.log("entering first state") } }, State { name: "secondState" PropertyChanges { target: props someProperty: 1 } StateChangeScript { name: "secondScript" script: console.log("entering second state") } } ] } 
+6
source
 Item { id: props property int someProperty: 0 // ... states: [ State { name: "firstState" PropertyChange { target: props someProperty: 1 // ... } }, State { name: "secondState" PropertyChange { target: props someProperty: 1 // ... } } ] transitions: [ Transition { ScriptAction { script: console.log("transition to " + props.state) } } ] } 
+1
source

All Articles