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 pass an object ref from C++ to QML?
Forum Updated to NodeBB v4.3 + New Features

How to pass an object ref from C++ to QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 410 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.
  • H Offline
    H Offline
    Hit Tiger Tonight
    wrote on last edited by Hit Tiger Tonight
    #1

    Hello everyone,

    I got wondered about how to use some QML properties from C++.
    For example, I have got the pointer "pItem" of an QML item by "mainWindow->findChild()",
    and I'd like to set the "anchors.fill" property. So, I tried:

    ```
    
    pItem->setProperty("anchors.fill", mainWindow);
    
    pItem->setProperty("anchors.fill", *mainWindow);
    
    ```
    

    All cannot work.
    Dose anyone know how to do it?
    Thanks.

    GrecKoG 1 Reply Last reply
    0
    • H Hit Tiger Tonight

      Hello everyone,

      I got wondered about how to use some QML properties from C++.
      For example, I have got the pointer "pItem" of an QML item by "mainWindow->findChild()",
      and I'd like to set the "anchors.fill" property. So, I tried:

      ```
      
      pItem->setProperty("anchors.fill", mainWindow);
      
      pItem->setProperty("anchors.fill", *mainWindow);
      
      ```
      

      All cannot work.
      Dose anyone know how to do it?
      Thanks.

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      First, don't do that (interact with QML objects from C++).
      Here are some links explaining why:

      1. https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
      2. http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
      3. https://youtu.be/vzs5VPTf4QQ?t=23m20s

      As for why your code doesn't work, anchors.fill expects a QQuickItem*, which I doubt your mainWindow is.

      H 1 Reply Last reply
      3
      • GrecKoG GrecKo

        First, don't do that (interact with QML objects from C++).
        Here are some links explaining why:

        1. https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
        2. http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
        3. https://youtu.be/vzs5VPTf4QQ?t=23m20s

        As for why your code doesn't work, anchors.fill expects a QQuickItem*, which I doubt your mainWindow is.

        H Offline
        H Offline
        Hit Tiger Tonight
        wrote on last edited by
        #3

        Thanks, @GrecKo
        I found some persons have the same problem with me, they also want to control the anchors properties from C++.
        I continue to try, and finally found how to do it, here is the code:

            auto anchors = pItem->property("anchors").value<QObject*>();
            auto b = anchors->setProperty("fill", QVariant::fromValue(m_qtRootItem));
        
        1 Reply Last reply
        0
        • H Hit Tiger Tonight referenced this topic on
        • H Hit Tiger Tonight referenced this topic on
        • H Hit Tiger Tonight has marked this topic as solved on

        • Login

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