mlvljr
mlvljr

Reputation: 4161

Is this QML valid (binding twice to a property)?

Because it keeps exhibiting strange behavior (and crashing QMLViewer).

The code:

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
}

Note the anim property, whose value is obviously not a child of the enclosing element.

This possibly brings us to a question of QML memory management and ownership (again).

Upvotes: 1

Views: 334

Answers (1)

sergk
sergk

Reputation: 3631

It doesn't looks like QML Behavior element can share it's animation instance with others. If you define NumberAnimation for each Behavior it should work properly.

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

Upvotes: 2

Related Questions