Important: Please read the 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();
    graphContainerItem = qobject_cast<QQuickItem*>(graphContainerObject);
    QQmlComponent newComponent(m_Engine, QUrl("qrc:/NewComponent.qml"));
    QObject * qObject = newComponent.create();
    QQuickItem theObject = qobject_cast<QQuickItem*>(qObject);
    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,

  • Moderators


    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": .

  • 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"
    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,

Log in to reply