C++ 从C+创建单独的QML窗口+;代码

C++ 从C+创建单独的QML窗口+;代码,c++,qt,qml,qtquick2,qqmlcomponent,C++,Qt,Qml,Qtquick2,Qqmlcomponent,在我的应用程序中,我想用C++代码创建一个QML UI的窗口。 我知道使用QML窗口类型创建另一个窗口是可能的,但是我需要从C++代码中得到同样的东西。 到目前为止,我成功地将我的附加qml文件加载到QQmlComponent中: QQmlEngine engine; QQmlComponent component(&engine); component.loadUrl(QUrl(QStringLiteral("qrc:/testqml.qml"))); if ( component.i

在我的应用程序中,我想用C++代码创建一个QML UI的窗口。 我知道使用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();
QQmlEngine发动机;
QQML组件和发动机;
loadUrl(QUrl(QStringLiteral(“qrc:/testqml.qml”));
if(component.isReady())
component.create();
其他的

qWarning()您可以尝试创建新的QQmlEngine

您可以使用单个
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();
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();
import QtQuick 2.4

Item {
    MyMainWindow {
        visible: true
    }

    MyAuxiliaryWindow {
        visible: true
    }
}
我们也可以使用
QQuickView
QQuickView
只支持加载从
QQuickItem
派生的根对象,因此在这种情况下,我们的
qml
文件不能以qml类型
ApplicationWindow
Window
开始,就像上面的示例一样。因此在本例中,我们的
main
可以是这样的:

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();
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();
import QtQuick 2.4

Item {
    MyMainWindow {
        visible: true
    }

    MyAuxiliaryWindow {
        visible: true
    }
}

对于任何好奇的人来说,我最终用稍微不同的方法解决了这个问题

我的根QML文档现在如下所示:

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();
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();
import QtQuick 2.4

Item {
    MyMainWindow {
        visible: true
    }

    MyAuxiliaryWindow {
        visible: true
    }
}
其中,
main窗口
是一个具有根元素
ApplicationWindow的QML组件
辅助窗口
是一个具有根元素
窗口
的组件


工作正常,您不必担心加载两个单独的QML文件。

谢谢您的回答。因此,如果我使用“Window”或“ApplicationWindow”QML类型作为QML的根元素,我假设第二个解决方案可以工作。我说得对吗?我想这是我还没意识到的事情,我想我应该用C++来创建窗口。现在我明白了QML的规则。你是对的:)
QQmlApplicationEngine
不会自动创建根窗口。阅读完您的信息后,我认为最好使用另一个选项更新我的答案:使用
QQuickView
。在这种情况下,您知道,
QQuickView
只支持加载从
QQuickItem
派生的根对象。由于某种原因,您使用一个QQmlApplicationEngine和两个加载的文件的解决方案对我不起作用,第二个窗口只是拒绝显示。也许是我的代码中的错误。无论如何,我已经找到了一个更好的方法来解决这个问题(在我的例子中),将它作为另一个答案发布。无论如何,谢谢你的帮助!听到这个消息我很难过:(如果你想看到代码运行或者只是想玩它,我已经将代码上传到GitHub。不管怎样,我看到了你的新答案,非常棒!)