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

时间:2015-11-19 08:36:15

标签: 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)
}

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

那么每次状态改变时我怎么能运行doSomething()?使用QML

可以更好地完成此类操作

1 个答案:

答案 0 :(得分:1)

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) 
            }
        }
    ]
}