How to set property into a sub context?
-
Hello,
I try to pass an object (ListModel) into a sub-context of an component that I have created, but I have this error:
QQmlContext: Cannot set property on internal contextHere, my attempts:
@
QQmlComponent component(engine, QUrl::fromLocalFile("myScene.qml"));
QObject object = component.beginCreate( engine->rootContext() );
qobject_cast<QQuickItem>(object)->setParentItem(window2); //assign my scene to a qml window. window2 is a QQuickItemQQmlContext *context0 = engine->rootContext(); //works but conflict
QQmlContext *context1 = engine->contextForObject( window2 ); //doesn't work
QQmlContext *context2 = engine->contextForObject( object ); //doesn't work
QObject object2 = object->findChild<QQuickItem>("Rect"); //Rect is the first child in myScene
QQmlContext *context3 = engine->contextForObject( object2 ); //doesn't work
QObject object3 = object->findChild<QQuickItem>("myRepeter"); //myRepeter is the final child which uses my property
QQmlContext *context4 = engine->contextForObject( object3 ); //doesn't workcontext2->setContextProperty("myList", myModelList);
component.completeCreate();@
Why is it not possible to set a property elsewhere than the rootcontext?
I can't use the rootcontext because I will create several scenes with several differents models.
With the root context, there is a name conflit.I also tried to use:
@
object3->setProperty("myList", myModelList);
@but i have a lot of compilation errors.
Probably, I will try to use a table of models and set an index into my "myScene" to solve my problem.
But I want to know why is it not possible to set a object to the context of an child item, or how to avoid conflicts of names in this situation?Thanks.
-
"Here, ":http://qt-project.org/doc/qt-5.1/qtqml/qqmlengine.html
I read that:
@
QQmlContext * QQmlEngine::rootContext() const
...
Additional data that should only be available to a subset of component instances should be added to sub-contexts parented to the root context.
@
So, it is possible to create data to a sub-context.
How I have to do?
A property isn't data?I solved my problem by using a ListModel main which contains several sub-ListModels and by assigning an ID to each scene and a invokable method wich return the corresponding model.
But I want to know for the futur.Thanks.
-
Ok, I found:
@
QQmlContext *context1 = new QQmlContext(engine, qApp);
context1->setContextProperty("myList", modelList_1 );
QQmlComponent component1(engine, QUrl::fromLocalFile("myScene.qml"), this);
QObject object1 = component1.beginCreate( context1 );
qobject_cast<QQuickItem>(object1)->setParentItem( window1 );
component1.completeCreate();QQmlContext *context2 = new QQmlContext(engine, qApp); context2->setContextProperty("myList", modeList_2 ); QQmlComponent component2(engine, QUrl::fromLocalFile("myScene.qml"), this); QObject *object2 = component2.beginCreate( context2 ); qobject_cast<QQuickItem*>(object2)->setParentItem( window2 ); component2.completeCreate(); QQmlContext *context3 = new QQmlContext(engine, qApp); context3->setContextProperty("myList", modelList_3 ); QQmlComponent component3(engine, QUrl::fromLocalFile("myScene.qml"), this); QObject *object3 = component3.beginCreate( context3 ); qobject_cast<QQuickItem*>(object3)->setParentItem( window3 ); component3.completeCreate();
@
So, I don't have of name conflict (each scene uses "myList" model name, but shows differents data).
But why it isn't possible to set property into internal context? There is an explication?