4

I can't access to delegated QML Component in Repeater from C++. Please find codes below. Thanks.

main.cpp

#include <QApplication>
#include <QDebug>
#include <QQuickView>
#include <QQuickItem>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QQuickView view;
    view.setSource(QUrl(QStringLiteral("qrc:/Main.qml")));
    view.show();
    QQuickItem *object = view.rootObject();
    QObject *rectangle = object->findChild<QObject*>("rect1");

    if (!rectangle)
    qDebug() << "MyError: rectangle was not found";

    app.exec();
}

Main.qml

import QtQuick 2.4

Row {
    Repeater {
        model: 3
        Rectangle {
            width: 50; height: 50
            color: index %2 ? "black" : "white"
            objectName: "rect" + index
        }
    }
}

Console output:

MyError: rectangle was not found
Oleksandr Kozlov
  • 697
  • 6
  • 11

2 Answers2

4

I have implemented own recursive function template 'findChild' function in C++.`

template <class T>
SearchType findChild(QQuickItem* object, const QString& objectName)
{
        QList<QQuickItem*> children = object->childItems();
        foreach (QQuickItem* item, children)
        {
            if (QQmlProperty::read(item, "objectName").toString() == objectName)
                return item;

            T child = findChild<QQuickItem*>(item, objectName);

            if (child)
                return child;
    }
    return nullptr;
}

And call it instead a default function.

QQuickItem *object = view.rootObject();
QQuickItem *rectangle = findChild<QQuickItem*>(object, "rect1");

if (rectangle)
{
    qDebug() << rectangle;
    qDebug() << rectangle->objectName();
} 

And get output:

QQuickRectangle(0x2222b40, name="rect1", parent=0x22245b0, geometry=50,0 50x50)
"rect1"
Oleksandr Kozlov
  • 697
  • 6
  • 11
0

If you invoke object->dumpObjectTree() you will see something like the following console output:

QQuickRow:: 
QQuickRepeater:: 
    QQmlComponent::

As you see, the object tree does not contain the rectangles as dedicated qobjects. In my limited understanding on the QML side there is another tree containing all visible qml items/components. These you can access at least through the 'children'-property from within qml. To show this, I changed your qml-file to:

Main.qml:

Row {
    id: row
    Repeater {
        model: 3
        Rectangle {
            width: 50; height: 50
            color: index %2 ? "black" : "white"
            objectName: "rect" + index
        }

    }
    Component.onCompleted: {
        console.log(row.children[0].objectName);
        console.log(row.children[1].objectName);
        console.log(row.children[2].objectName);
    }

}

With the resulting following output:

qml: rect0
qml: rect1
qml: rect2
MyError: rectangle was not found

Of course the direct accessibility and index depends on your additional items/components. From here on you could write your own recursive 'findChild' function and expose the result objects to C++. Here is described how: http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html

For example, you could implement a 'findChild' in qml like this:

function findChild(propertyName, propertyValue) {
    return findChildRecursivly(row, propertyName, propertyValue)
}

This qml function you can invoke from C++ like

QVariant result;
QMetaObject::invokeMethod(object, "findChild",
                          Q_RETURN_ARG(QVariant, result),
                          Q_ARG(QVariant, "objectName"),
                          Q_ARG(QVariant, "rect1"));
rectangle = result.value<QObject *>();

With the following additional lines of code:

if (!rectangle)
{
    qDebug() << "MyError: rectangle was not found";
}
else
{
    qDebug() << rectangle;
    qDebug() << rectangle->objectName();
}

you get the following output:

QQuickRectangle(0x1c1beef, name = "rect1")
"rect1"