Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Obtaining reference to dynamically added QQuickItems

    QML and Qt Quick
    3
    4
    944
    Loading More Posts
    • 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.
    • S
      SquimbellyToes last edited by

      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 Reply Quote 0
      • p3c0
        p3c0 Moderators last edited by

        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 Reply Quote 0
        • O
          onek24 last edited by

          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 Reply Quote 0
          • S
            SquimbellyToes last edited by

            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 Reply Quote 0
            • First post
              Last post