Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to set property into a sub context?
Forum Updated to NodeBB v4.3 + New Features

How to set property into a sub context?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 1 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VTiTux
    wrote on last edited by
    #1

    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 context

    Here, 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 QQuickItem

    QQmlContext *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 work

    context2->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.

    1 Reply Last reply
    1
    • V Offline
      V Offline
      VTiTux
      wrote on last edited by
      #2

      "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.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VTiTux
        wrote on last edited by
        #3

        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&#40;"myScene.qml"&#41;, this&#41;;
        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&#40;"myScene.qml"&#41;, this&#41;;
        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?

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved