Add objects to QML layout from C++
问题 How can I, from the C++, add some more Rectangle s dynamically to the one with id root ? For example, two more Rectangle s colored red and green ? main.qml: Rectangle { id: root } Typical QML objects to add under 'root': Rectangle { color: "red"; width: 200; height: 200 } Rectangle { color: "green"; width: 200; height: 200 } Qt Creator generated main.cpp : int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral