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. ContextProperty QML Items reparenting context
QtWS25 Last Chance

ContextProperty QML Items reparenting context

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 4 Posters 1.1k Views
  • 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.
  • B Offline
    B Offline
    billouparis
    wrote on last edited by
    #1

    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
    Bill

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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?

      (Z(:^

      1 Reply Last reply
      2
      • B Offline
        B Offline
        BillouParis2
        wrote on last edited by BillouParis2
        #3

        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?

        Bill

        sierdzioS 1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • B BillouParis2

            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?

            Bill

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

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

            (Z(:^

            1 Reply Last reply
            2

            • Login

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