// main.cpp #include#include #include #include int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QList rootObjects = engine.rootObjects(); QObject* mainComponent = rootObjects.at(0); qDebug() << "Main component object name: " << mainComponent->objectName(); return app.exec(); }
// main.qml import QtQuick 2.0 Rectangle { id: root width: 200 height: 200 color: "red" Text { id: textObj text: "Hello, World!" font.pixelSize: 20 anchors.centerIn: parent color: "white" } } // main.cpp #includeIn this example, the rootObjects property is used to retrieve a reference to the root object in the loaded QML file. The findChild() function is used to find the Text QML component with the id "textObj". The property() function is then used to access the "text" property of the Text component. This example also uses the Qt Core and Qt Qml libraries.#include #include int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QList rootObjects = engine.rootObjects(); QObject* root = rootObjects.at(0); QQuickItem* textObj = root->findChild ("textObj"); qDebug() << "Text object text: " << textObj->property("text"); return app.exec(); }