使用QQuickView或QQmlApplicationEngine在ApplicationWindow中的页面之间切换 [英] Use QQuickView or QQmlApplicationEngine to switch between pages from ApplicationWindow

查看:753
本文介绍了使用QQuickView或QQmlApplicationEngine在ApplicationWindow中的页面之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ApplicationWindow作为主文件,并能够使用QQuickView::setSource(const QUrl & url)C++切换到其他QML文件.基本上可以做到这一点:

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:

启动=>加载 main.qml (ApplicationWindow)=>单击帮助按钮=> C++加载 help.qml 文件=>等

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"}
    }
}

(我想知道这里的Loader是否真的必要)

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

但是QQuickView仅支持加载从QQuickItem派生的根对象.因此,它不适用于ApplicationWindow.

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

我正在考虑使用QQmlApplicationEngine代替QQuickView,但是用法似乎有所不同,该类仅配备了QQmlApplicationEngine::load(const QUrl & url)

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)

对我而言,最好的行动方案是什么?我真的在我的 main.qml 文件中需要一个ApplicationWindow吗?

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

推荐答案

按照您的建议使用QQmlApplicationEngine,并使用main.qml进行设置,但是使用内容页面URL从C ++设置上下文属性,例如help.qml-然后绑定到Loader的source属性中的此上下文属性.

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.

这是从C ++控制QML的正常方法-公开上下文属性或具有属性的单例对象,从C ++驱动它们,并使QML绑定响应更改.

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.

这篇关于使用QQuickView或QQmlApplicationEngine在ApplicationWindow中的页面之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆