1

因为它一直表现出奇怪的行为(并导致 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 内存管理和所有权的问题(再次)。

4

1 回答 1

2

看起来 QMLBehavior元素不能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
        }
    }
}
于 2012-07-04T15:55:17.900 回答