PDA

View Full Version : Dynamic QML Property Binding



EMCEE
29th August 2012, 12:05
Hi

Some QObject derived Classes in my Application are created and destroyed dynamicly during runtime.
Those Objects are "injected" into QML be using setContextProperty:



SomeObject* someObjectInstance = new SomeObject();
view.rootContext()->setContextProperty("someObjectInstance", someObjectInstance);

view is a QDeclarativeView here.


Well, basically this works. But it takes a lot of time and sometimes the Application even crashes. It seems like all bindings of the Context are reevaluate when i add a new Context Property.

So i think im doing something completely wrong here. Is there a way to achieve the desired dynamic binding, without all binding having to reevaluate? I searched the web and did read the Documentation, but didnt seem to find anything when it comes to more advanced interaction between QML and C++.

What happens to the binding if a QObject that was exported using QDeclarativeContext::setContextProperty is destoyed? will the Binding still be there?

EMCEE
30th August 2012, 11:51
I think i found that the bindings are not reevaluated if they are overwritten.

So for now i have to create some pseudo ContextProperties at the start of my application:



context->setContextProperty("someObjectInstance", NULL);
context->setContextProperty("otherProperty", NULL);


and later if a Object gets instantiated, i just overwrite these:



context->setContextProperty("someObjectInstance", someObjectInstance);
context->setContextProperty("otherProperty", otherQObject);


It works...but its ugly...

Is it even ok to overwrite a ContextProperty?