Binding loop detected



  • I have some controls in my QtQuick app that set parameters in my object. For example, this slider below. I want it to initialize it's property value from the underlying C++ object but from then on, any adjustments to the slider should modify the C++ object.

    This code seems to get the job done, except that I keep getting 'binding loop detected' errors. So I'm assuming this is not the correct way to do it. Can anyone tell me the "proper" way to do this or how to get rid of the binding loop errors?

    Slider {
            id: sizeSet
            height: 0.4*parent.height
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: sizeText.right
            anchors.leftMargin: 0.02*parent.width
            anchors.right: parent.right
            anchors.rightMargin: 0.02*parent.width
            from: 1
            to: 300
            value: modelData.size
            onValueChanged: modelData.size=sizeSet.value
            handle: Rectangle {
                    x: parent.leftPadding*.95
                    y: parent.topPadding * .95
                    implicitWidth: parent.height*.5
                    implicitHeight: parent.height*.5
                    radius: parent.height*.25
                    color: parent.pressed ? "#f0f0f0" : "#ffffff"
                    border.color: "#C0C0C0"
                }
        }
    }
    

  • Moderators

    @kgregory said in Binding loop detected:

    value: modelData.size
    onValueChanged: modelData.size=sizeSet.value

    there you have the obvious loop



  • @kgregory said in Binding loop detected:

    value: modelData.size
    onValueChanged: modelData.size=sizeSet.value

    This has a loop effect, slider value is dependent on modelData.size and modelData.size is dependent on slider value. Try to get rid of it.


  • Moderators

    @Yashpal
    and thats exactly the problem.
    The loop is already in your definition. The values can't be mutual dependent from each other.

    @Yashpal said in Binding loop detected:

    slider value is dependent on modelData.size and modelData.size is dependent on slider value

    Think about it it, this never can be (logically) correct.

    What are you actually trying to achieve?



  • @kgregory You can try

    Component.onCompleted: {
    sizeSet.value = modelData.size
    modelData.size = Qt.binding(function() { return sizeSet.value })
    }
    

    (Not tested.) See http://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html for further info about Qt.binding.


  • Moderators

    @Eeli-K
    writing the same (problematic) definition just in another way results in the same issue ;)
    Again, this mutual dependency will never be solved, no matter what different code you are trying.



  • @raven-worx @kgregory My code may be wrong, instead of "modelData.size =..." "onValueChanged: modelData.size = value" could be enough. Raven-worx, why do you say that? Of course "x: y; y: x" is circular, but setting one once in Component.onCompleted and leaving off the binding should solve it. Well, now I see I wasn't explicit about leaving off the two bindings and replacing them with the code, if that was the problem. Of course there should be only one binding at most.


  • Moderators

    @Eeli-K said in Binding loop detected:

    Raven-worx, why do you say that?

    because those 2 are still property bindings...

    sizeSet.value = modelData.size
    modelData.size = Qt.binding(function() { return sizeSet.value })



  • @raven-worx No, only the latter is. The first line isn't a binding, it's just setting the value. The one and only binding is made after that and there is no circularity.


  • Moderators

    @Eeli-K
    yep you are right. Stupid me, should have read it more precisely.
    Sorry.



  • Thanks, Eeli. I think that will work.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.