PDA

View Full Version : Binding properties of dynamically created QML objects



Obi-Wan
5th August 2017, 11:06
Hi all!

I have some objects that I create dynamically in QML, and I would like to bind some of these objects' properties to properties of static objects.

I have used a ListModel to manage the dynamically created objects as suggested here (https://qmlbook.github.io/en/ch13/index.html#tracking-dynamic-objects).

Using this ListModel I have no problem looping through all the objects and accessing them, but I can't figure out how to bind their properties to other objects.

The same site suggests using the Binding component (https://qmlbook.github.io/en/ch13/index.html#binding-indirectly), but I do not understand how to use it.

Without the Binding component, I have tried this:


for (var i = 0; i < listModel.count; ++i)
{
var dynObj = listModel.get(i).obj // Gives me access to the object
dynObj.someProperty = Qt.binding(function() {return staticObject.otherProperty}) // I want this to work
}

This sets the value of dynObj.someProperty to staticObject.otherProperty at the time this call is made, but when otherProperty changes later on QML complains about "Something related to the property not being available, I dont' remember of the top of my head, beacuse I'm writing this at home and I do not have access to the code at this moment."


Any ideas?

john_god
15th August 2017, 21:32
I do it just like you did, and it works fine for me. It would help if you could post the exact message and the snippet of code. Is that a error or warning message, the bindind still works after that ?

high_flyer
16th August 2017, 13:02
As I am a C++ guy, I might be barking on the wrong tree here but what happens if you do this:


for (var i = 0; i < listModel.count; ++i)
{
listModel.get(i).obj.someProperty = Qt.binding(function() {return staticObject.otherProperty})
}


The reason for this is that I think that your local 'dynObj' might be destroyed at the end of the for loop.
Again, I don't know if this is true for JS, but I'd at least check it out.