qml在状态更改后运行javascript代码

lam*_*ser 7 qml qtquick2

我有几个状态,我只使用它来改变一些属性:

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)
}
Run Code Online (Sandbox Code Playgroud)

由于不同的状态可以具有相同的值,因为someProperty我不能依赖于somePropertyChange信号,但我甚至不能依赖onStateChange(如示例中),因为它运行时属性不变.

那么doSomething()每次状态改变我怎么能跑?有更好的方法来做这种事情QML吗?

Mee*_*fte 7

您可以使用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")
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)