在我的应用程序中,我希望使用C++代码创建另一个具有QML的窗口。
我知道可以使用QML窗口类型创建另一个窗口,但我需要从C++代码中获得同样的信息。
到目前为止,我设法将我的附加qml文件加载到QQmlComponent中:
QQmlEngine engine;
QQmlComponent component(&engine);
component.loadUrl(QUrl(QStringLiteral("qrc:/testqml.qml")));
if ( component.isReady() )
component.create();
else
qWarning() << component.errorString();如何将其显示在单独的窗口中?
发布于 2016-02-03 07:57:46
对于任何好奇的人来说,我以稍微不同的方式解决了这个问题。
我的根QML文档现在如下所示:
import QtQuick 2.4
Item {
MyMainWindow {
visible: true
}
MyAuxiliaryWindow {
visible: true
}
}其中,MainWindow是具有根元素ApplicationWindow的QML组件,而AuxiliaryWindow是具有根元素Window的组件。
工作正常,您不必担心加载两个单独的QML文件。
发布于 2016-02-01 13:06:08
您可以使用单个QQmlEngine来实现这一点。按照您的代码,您可以这样做:
QQmlEngine engine;
QQmlComponent component(&engine);
component.loadUrl(QUrl(QStringLiteral("qrc:/main.qml")));
if ( component.isReady() )
component.create();
else
qWarning() << component.errorString();
component.loadUrl(QUrl(QStringLiteral("qrc:/main2.qml")));
if ( component.isReady() )
component.create();
else
qWarning() << component.errorString();不过我更喜欢QQmlApplicationEngine。这个类结合了一个QQmlEngine和QQmlComponent来提供一种加载单个QML文件的方便方法。因此,如果您有机会使用QQmlApplicationEngine,那么代码行就会更少。
示例:
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.load(QUrl(QStringLiteral("qrc:/main2.qml")));
return app.exec();我们也可以使用QQuickView。QQuickView只支持加载从QQuickItem派生的根对象,因此在本例中,我们的qml文件不能像上面的示例那样以QML类型ApplicationWindow或Window开头。所以在这种情况下,我们的main可能是这样的:
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl("qrc:/main.qml"));
view.show();
QQuickView view2;
view2.setSource(QUrl("qrc:/main2.qml"));
view2.show();
return app.exec();发布于 2016-02-01 10:13:51
您可以尝试创建新的QQmlEngine
https://stackoverflow.com/questions/35127772
复制相似问题