Mark
Mark

Reputation: 5028

QML: Bind loop detected without double assignment

As far as I know the bind loop happens when I try to assign two properties each other. Example:

CheckBox {
    checked: Settings.someSetting                         
    onCheckedChanged: {
        Settings.someSetting = checked;
    }
}

but in my scenario I can't see such a "double assignment". I report here the full code:

import QtQuick 2.7
import QtQuick.Window 2.3

Window {
    visible: true;
    width: 500
    height: 500

    Rectangle {
        id: main
        anchors.fill: parent
        color: "black"

        property bool spinning: true
        property bool stopping: false

        Rectangle {
            x: 0.5 * parent.width
            y: 0.5 * parent.height
            width: 10
            height: 200
            radius: 5
            color: 'red'

            transformOrigin: Item.Top
            rotation: {
                if (main.stopping)
                {
                    main.spinning = false;
                    main.stopping = false;
                }

                return timer.angle
            }
        }

        Timer {
            id: timer
            interval: 5
            repeat: true
            running: true
            onTriggered: {
                if (main.spinning) angle += 1;
            }
            property real angle
        }

        MouseArea {
            id: control
            anchors.fill: parent
            onClicked: {
                main.stopping = true;
            }
        }
    }
}

When you click with the mouse you will get the warning:

qrc:/main.qml:17:9: QML Rectangle: Binding loop detected for property "rotation"

I don't see my mistake. I'm using flags (bool variables) to control the execution of my code. I know in this case I can just stopping the timer directly, but the actual program is more complex than this example.

Upvotes: 0

Views: 733

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

The binding is in the following lines:

rotation: {
    if (main.stopping)
    {
        main.spinning = false;
        main.stopping = false;
    }

    return timer.angle
}

The change of rotation is triggered by the change of main.stopping: let's say that change main.stopping is given by the mouseArea, then it will be called a rotation, but inside this there is an if, and in this you are changing back to main.stopping , where he will call rotation back.

If a property in QML changes all the elements that depend on it will change

Upvotes: 2

Related Questions