【1】如果QML中的根是Window以及其子类,则使用如下代码进行加载:
QQmlEngine engine;
QQmlComponent component(&engine);
component.loadUrl(QUrl::fromLocalFile("qml file...."));
if ( component.isReady() ){
QQuickWindow* w = qobject_cast<QQuickWindow*>(component.create());
w->show();
}
else{
qWarning() << "warning:" << component.errorString();
}
其中w就你需要加载的qml中的根。
【2】如果QML中的根是非Window以及其子类的,则使用如下代码进行加载:
QQuickView view;
view.setSource(QUrl::fromLocalFile("qml file..."));
view.show();
其中view的rootObject()可以返回所加载的qml组件的根。