Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Obtaining reference to dynamically added QQuickItems



  • 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


  • Moderators

    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 ?



  • 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/ .



  • 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


Log in to reply