How to create a bidirectionnal binding



  • Hi,

    I've got two Qml element, each of them has a "test" property and this property must have always the same value. I can use a binding to link one property to another but this is not bidirectional.

    Here an example of what I need.

    @import QtQuick 1.0

    Rectangle{
    id:root

    MyItem{id:a}
    MyItem{id:b}
    

    }@

    Here I want a.test and b.test to to have always the same value.



  • Why don't you just have a property alias in a and b that refers to some root element property?



  • Because this will imply to know the root from my Item. And those items are suppose to be part of separate module (each of them ) so I can't force a use case like this.


  • Moderators

    Maybe use singal/ slots connections to connect "onChanged" of both objects? You may have to add some clever code to prevent looping, but it's not hard.

    Although I would personally opt for what grego said: a global property, managed by root. As far as I see, it is perfectly doable, even when those objects are separated (some signal/ slot magic might come handy in that case).

    Or use some more fancy stuff, like a shared C++ object, that all MyItems would be connected with. Or database, or a config file... right, I've got to stop right here, as I feel I am getting too abstract ;) I seems that you have to have some control from parent/ root regardless of what option you choose.



  • thank to you two for your suggestion.

    In fact I've got a solution, I use two binding element, because in my case those properties cannot change in the same time. So I play with the when property. But this doesn't seems right to my, it's look like makeshift job.

    My two modules aren't especially written for my project so I don't want to contaminate them.


  • Moderators

    Might be good to make this into a feature request on JIRA, then - or even to write your own solution and submit it to Qt-project.


Log in to reply
 

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