//Using QQmlComponent
//QQmlEngine engine;
//QQmlComponent component(&engine,
// QUrl::fromLocalFile("main.qml"));
//QObject *object = component.create();
// delete object;
//qml 根对象 Window
QQuickView view(QUrl::fromLocalFile("main.qml"));
view.show();
QObject *object = view.rootObject();
//object = view.rootContext();
//qml 根对象 Rectangle 或 Item
//auto test_txt1 = object->findChild<QObject*>("test_text1");
//test_txt1->setProperty("text", "00000000");
object->findChild<QObject*>("test_text1"); 返回null
解决方法 qml 中的 控件名要使用 objectName 而不是 id
Text {
objectName:"test_text1" // 注意 必须使用 ""
id: element
x: 292
y: 271
text: qsTr("Text")
font.pixelSize: 28
}