Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qml run javascript code after state change

Tags:

qml

qtquick2

I have several states which 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 could have the same value for someProperty I can't rely on the somePropertyChange signal, but I can't even rely on the onStateChange (as in the example) since when it runs the properties are unchanged.

So how could I run doSomething() every time the state change? There is a better way to do this kind of things with QML?

like image 384
lambdauser Avatar asked Nov 19 '15 08:11

lambdauser


2 Answers

You can run some 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")
            }
        }
    ]
}
like image 129
Meefte Avatar answered Oct 22 '22 13:10

Meefte


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) 
            }
        }
    ]
}
like image 26
Mark Ch Avatar answered Oct 22 '22 13:10

Mark Ch