Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
strobejb
Posts: 282
Registered: ‎10-15-2012
My Device: bb10 developer

QML bindings - setContextProperty

I'm using setContextProperty to inject a C++ object into my QML:

 

MyObject myObject = new MyObject();

 

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

qml->setContextProperty("_myObject", myObject);

 

This is working absolutely fine, and the QML binds properly to the MyObject class instance.

 

My question is, what happens if I later call setContextProperty, with a differnet MyObject instance?

 

i.e. 
MyObject myObject2 = new MyObject();

qml->setContextProperty("_myObject", myObject2);

 

What happens to the existing QML bindings? Do they get automatically updated, or does the 2nd call to setContextProperty have no effect?

 

 

Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: QML bindings - setContextProperty

You cannot call setContextProperty after you have use the QmlDocument to create a root object (unless you want a new root object). The values on the qmldocument when you create it are what is available to your qml.

To update the bindings you either need to have a custom property in your qml so that you can do something like container->setProperty("myprop",myObject2) or you need to just update the values in myObject.

Dont forget the * for pointers when doing new :smileyhappy:
Like all of my posts
Developer
strobejb
Posts: 282
Registered: ‎10-15-2012
My Device: bb10 developer

Re: QML bindings - setContextProperty

thanks for the confirmation -

 

I solved the problem by exposing a MyObject * property from my main application instance - which can then change when needed and emits all the appropriate signals

 

cheers

james