Qt World Summit: Submit your Presentation

ContextProperty QML Items reparenting context

  • Hello everyone,
    i'm stuck with a very easy topic I suppose.
    Here is my issue description:
    I have a class A, deriving Object and exposing a property through the Q_PROPERTY.
    I have a QQuickView.
    I call the setContextProperty of my class A instance.
    Then I do a setSource of a my parent QML File (qml file 1).
    I can easily access the A class property from within the QML code inside this qml file 1.
    Later on, I dynamicaly create a QQmlComponent to instanciate a second QML File (qml file 2) from C++.
    Then I dynamically "reparent", or change the parent property of my element to the root object in my view, which matches the qml file 1 reference. (I use the QMLProperty::write method to achieve this)
    I was expecting to get access to my class A instance and its properties, but the class A instance is not even known from within this second QML File.
    I thought a kind of inheritance of properties would happen from the parent to the child, just by setting the parent property of the child to the root object. But obviously I am missing something here.
    Any help is more than welcome :p

  • Moderators

    QQuickItems should be reparented with setParentItem().

    However, I'm not sure I understand you. Can you perhaps draw the inheritance tree before and after reparenting?

  • Thank you for the suggestion about SetParentItem. Sadly it doesn't solve my issue. let me try to give you another explanation of what I'm trying to achieve.

    A QML item is instantiated by a call to setSource of a View in the C++.
    One instance of a class A is declared to the root context of this view (setContextProperty).
    One can easily access or refer to this class instance from within the QML code of this Item.

    Later another QML item is instantiated from the C++ code. I don't know how exactly, but through a crete qmlcomponent I supporlse. I don't know this code. But I have the reference to this Item. Now my goals are:
    1- relate this new QML as a child of the previous existing one,
    2- display this element in the view, on top of the parent,
    3- refer to the class A from within the QML code of this new QML component.

    1 and 2 work ok. 3 doesn't work.
    I try to understand where is the A class instance, in which context. Do contexts inherit between parent and children. Do both Item have complete different contexts? Can we use the same context at some point, so that the new element can refer to my class instance? Should I declare my class in another context?

    Hopefully description is clearer now?


  • Qt Champions 2018

    You need to instantiate the second QML file in the same context (or a child context) as where your did the setContextProperty with your A instance.

    Alternatively, it seems weird that you instantiate a 2nd QML file from c++, why don't you do it in QML?

  • Moderators

    @BillouParis2 said in ContextProperty QML Items reparenting context:

    3- refer to the class A from within the QML code of this new QML component.

    OK thanks for explanation.

    I think what might work for you is this:

    1. In your "previous existing" item, add an alias property (or var property):
    property alias classA: yourContextPropertyName
    1. In your new QML component, do this:
    Component.onCompleted: {
      console.log("Class A prop: " + parent.classA)

    Or, just use setContextProperty on your main QML engine context, and set your class A there - then it will be available globally to all QML items.

Log in to reply