我在将新 QML 对象添加到现有场景时遇到问题。
我的main.qml
来源:
ApplicationWindow
{
id:background
visible: true
width: 640
height: 480
}
MyItem.qml
资源:
Rectangle
{
width: 100
height: 62
color: "red"
anchors.centerIn: parent
}
最后,这是我的main.cpp
来源:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QQmlComponent *component = new QQmlComponent(&engine);
component->loadUrl(QUrl("qrc:/MyItem.qml"));
qDebug() << "component.status(): "<< component->status();
QObject *dynamicObject = component->create();
if (dynamicObject == NULL) {
qDebug()<<"error: "<<component->errorString();;
}
return app.exec();
}
main.qml
正确显示MyItem.qml
但未出现在main.qml
. Component.status()
返回状态Ready
,没有错误dynamicObject
。我究竟做错了什么?