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"