此QML是否有效(绑定两次属性)?

时间:2012-07-04 11:26:30

标签: qt animation qml

因为它一直表现出奇怪的行为(并且导致QMLViewer崩溃)。

代码:

import QtQuick 1.1

Rectangle {
    width: 800; height: 600

    Rectangle {
        width: 60; height: 60
        x: rect1.x - 5; y: rect1.y - 5
        color: "green"

        property NumberAnimation anim: NumberAnimation { 
            id: animId; duration: 2000 
        }

        Behavior on x {
            animation: animId
        }
        Behavior on y {
            animation: animId
        }
    }

    Rectangle {
        id: rect1
        width: 50; height: 50
        color: "red"
    }

    focus: true

    Keys.onRightPressed: rect1.x = rect1.x + 100
    Keys.onLeftPressed:  rect1.x = rect1.x - 100
    Keys.onUpPressed:    rect1.y = rect1.y - 100
    Keys.onDownPressed:  rect1.y = rect1.y + 100
}

请注意anim属性,其值显然不是封闭元素的子元素。

这可能会让我们回到QML内存管理和所有权问题(再次)。

1 个答案:

答案 0 :(得分:2)

看起来QML Behavior元素不能与其他人共享animation个实例。如果您为每个NumberAnimation定义Behavior,它应该可以正常运行。

Rectangle {
    width: 60; height: 60
    x: rect1.x - 5; y: rect1.y - 5
    color: "green"

    Behavior on x {
        NumberAnimation {
            duration: 2000
        }
    }
    Behavior on y {
        NumberAnimation {
            duration: 2000
        }
    }
}