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. Get instance of the "attacher" in an attached object.

Get instance of the "attacher" in an attached object.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 491 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.
  • T Offline
    T Offline
    Tannz0rz
    wrote on last edited by
    #1

    I have an attached properties object declared thusly:

    QML_DECLARE_TYPEINFO(QAttacher, QML_HAS_ATTACHED_PROPERTIES)
    // ...
    static QAttachee *QAttacher::qmlAttachedProperties(QObject *object)
    {
        return new QAttachee(object);
    }
    

    Within the scope of the QAttachee, how do I receive the instance of it's parenting QAttacher?

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

      Isn't that the object parameter you're getting in this function?

      (Z(:^

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tannz0rz
        wrote on last edited by
        #3

        No sir. Implemented in QML, assuming the QML type is registered with the same type names used above, you would have something like this:

        QAttacher
        {
            id: attacher
        
            Item
            {
                id: attachee
        
                QAttacher.property: some_value
            }
        }
        

        Attached properties are provided to children of QAttacher, in our case the Item with id: attachee, which is what the object parameter points to. A call to object->metaObject()->className() will simply yield QQuickItem.

        What I want to know is how to get the pointer to the QAttacher with id: attacher. How do I retrieve this information?

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

          There is no attacher instance from the QML engine perspective, only an attaching type. Your code would work the same without your outer attacher.
          To be able to reference the outer parent in one of your attached object, you could simply check the parent object of the attachee. Alternatively force the creation of the attached object from c++ ( with https://doc.qt.io/qt-5/qqmlengine.html#qmlAttachedPropertiesObject) when you add an item in the default list property of your QAttacher, you could then call a setter on it and pass your QAttacher instance to it, and potentially do some more initialization.

          T 1 Reply Last reply
          2
          • GrecKoG GrecKo

            There is no attacher instance from the QML engine perspective, only an attaching type. Your code would work the same without your outer attacher.
            To be able to reference the outer parent in one of your attached object, you could simply check the parent object of the attachee. Alternatively force the creation of the attached object from c++ ( with https://doc.qt.io/qt-5/qqmlengine.html#qmlAttachedPropertiesObject) when you add an item in the default list property of your QAttacher, you could then call a setter on it and pass your QAttacher instance to it, and potentially do some more initialization.

            T Offline
            T Offline
            Tannz0rz
            wrote on last edited by Tannz0rz
            #5

            @GrecKo Thank you for this clarification and alternative approach, marked as solved.

            1 Reply Last reply
            0

            • Login

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