Use QQuickView or QQmlApplicationEngine to switch between pages from ApplicationWindow

 ̄綄美尐妖づ 提交于 2019-12-24 01:19:16

问题


I'd like to use an ApplicationWindow as a main file and be able to switch to other QML files from C++ with QQuickView::setSource(const QUrl & url). Basically it would do this:

start-up => loads main.qml (ApplicationWindow) => click on help button => C++ loads help.qml file => etc.

int main(int argc, char *argv[])
{
    QApplication app{argc, argv};
    CustomQQuickView view;

    view.setSource(QUrl{"qrc:/main.qml"});
    view->show();

    return app.exec();
}

main.qml

ApplicationWindow
{
    visible: true
    width: 640
    height: 480

    Loader
    {
        anchors.fill: parent
        id: mainPageLoader
    }

    Button
    {
        text: "Help"
        onClicked: { mainPageLoader.source = "help.qml"}
    }
}

(I am wondering if the Loader here is really necessary here)

However QQuickView only supports loading of root objects that derive from QQuickItem. Therefore it doesn't work with ApplicationWindow.

I'm thinking about using QQmlApplicationEngine instead of QQuickView but the usage seems different, this class being only equipped with QQmlApplicationEngine::load(const QUrl & url)

What would be the best course of action for my purpose? Do I really need an ApplicationWindow in my main.qml file?


回答1:


Use QQmlApplicationEngine as you suggest, and with the main.qml as you say, but set a context property from C++ with the content page URL, e.g. help.qml - then bind to this context property in the Loader's source property.

This is the normal way of controlling QML from C++ - expose context properties or singleton objects with properties, drive them from C++, and have QML bindings respond to changes.



来源:https://stackoverflow.com/questions/35487237/use-qquickview-or-qqmlapplicationengine-to-switch-between-pages-from-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!