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. Obtaining reference to dynamically added QQuickItems
Qt 6.11 is out! See what's new in the release blog

Obtaining reference to dynamically added QQuickItems

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.2k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello Folks;

    I'm having trouble figuring out how to obtain a reference to QQuickItems dynamically added to a QQuickItem parent.

    Here's the code I'm working with:
    @
    QQmlComponent graphContainer(m_Engine, QUrl("qrc:/graphContainer.qml"));
    QObject graphContainerObject = graphContainer.create();
    QQuickItem
    graphContainerItem = qobject_cast<QQuickItem*>(graphContainerObject);
    QQmlComponent newComponent(m_Engine, QUrl("qrc:/NewComponent.qml"));
    QObject * qObject = newComponent.create();
    QQuickItem theObject = qobject_cast<QQuickItem*>(qObject);
    theObject->setProperty("objectName","TheObject");
    theObject->setParentItem(graphContainer);
    ...
    QQuickItem theObjectReference = m_GraphContainer->findChild<QQuickItem>("TheObject");

    // theObjectReference == 0 in this case. How do I obtain a reference to qQI once it's set?
    @

    Thank you,
    ST

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Few questions:

      1. What is m_GraphContainer ? Where is it ? As I don't see it in above code.
      2. Why are you finding it ? As TheObject is the QQuickItem theObject.

      May be I didn't understood it correctly. Can you elaborate more ?

      157

      1 Reply Last reply
      0
      • O Offline
        O Offline
        onek24
        wrote on last edited by
        #3

        Well, simple example using *QObject graphContainerObject :

        @
        ...
        QObject *graphContainerObject = graphContainer.create();
        QObject rect = graphContainerObject->findChild<QObject>("rectangle");
        ...
        @

        Your QML-Object should have set the objectName property to "rectangle"

        @Rectangle {
        anchors.fill: parent
        objectName: "rectangle"
        }@

        So basically i don't see a problem there expept for that i dont know what the m_GraphContainer is.

        For further informations you could check out "this":http://developer.ubuntu.com/api/qml/sdk-1.0/QtQml.qtqml-cppintegration-interactqmlfromcpp/ .

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Hello Folks;

          My apologies for the tardiness of my reply. I also apologize for not providing a complete code example.

          is the graphContainer object - I neglected to rename it in my code snippet. Here's the graphContainer.qml file:

          @
          // QML
          import QtQuick 2.2
          import GraphUI 1.0

          Rectangle {
          anchors.centerIn: parent
          id: graphContainer
          objectName: "graphContainer"
          height:0;width:0
          x:0;y:0
          /*
          DEBUG
          border.color: "green"
          border.width: 2
          */
          }
          @

          As to onek24's marvelous suggestion, I've attempted such, but with a small difference: the objectName is programmatically set to a GUID. But I'm coming up empty when I:

          @
          QQuickItem qqEdge = m_GraphContainer->findChild<QQuickItem>(RID);
          @

          The QtCreator debugger shows all children collections to be empty, but my UI displays all edges and nodes correctly.

          Thank you again,
          ST

          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