单个项目中的多个窗口 [英] Multiple windows in a single project

查看:23
本文介绍了单个项目中的多个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目需要在一个屏幕上显示两个 QML Window(一个发送者,一个接收者).这两个 .qml 都要求我在其中包含一些 Cpp 模型,因此,我使用 QQmlApplicationEngine 来注册 Cpp 模型.

I have a requirement for my project to display two QML Windows each on one of the screen (one sender, one receiver). Both of the .qml requires me to include some Cpp models inside hence, I'm using QQmlApplicationEngine to register the Cpp models.

我发现使用 QWidget::createWindowContainer() 我可以为单个项目显示多个 Window.这对于第一个 QML 文件非常有效.代码片段如下所示:

I found out that using QWidget::createWindowContainer() I'm able to display multiple Windows for a single project. This works perfectly fine for the first QML file. The code snippets looks like this:

QQmlApplicationEngine* engine = new QQmlApplicationEngine(Qurl("main.qml"));
QmlContext* context = engine.getContextProperty();

//do some Cpp models registering...

QQuickview *view = new QQuickview(engine,0);
QWidget* container = widget::createWindowContainer(view);  
//I realized I dont need to do container->show(); for the main.qml to appear..

//use desktop widget to move the 2nd container to the 2nd screen...

我决定用类似的方法为我的 receive.qml 创建第二个应用程序引擎.我很快意识到即使使用 container2->show()receive.qml 也永远不会打开.现在,它显示的是一个空白页面.

I decided to create a 2nd application engine for my receive.qml with a similar method. I soon realized that the receive.qml would never open even with container2->show(). Now, it is showing an empty page.

我的问题是:

  1. 我的方法是正确的还是有更好的解决方案?
  2. 我需要注意什么信号才能赶上窗户关闭事件?当其中一个时,我似乎无法检测到信号窗口关闭.因为我想在一个已经关闭时关闭两者检测到.

推荐答案

这样可以更简单,例如:

That can be done easier, for example:

ma​​in.qml

import QtQuick 2.3
import QtQuick.Window 2.2

Item {

    Window {
        objectName: "wnd1"
        visible: true
    }

    Window {
        objectName: "wnd2"
        visible: true
    }
}

因此您可以从 C++ 代码访问这些窗口:

And so you can access these windows from C++ code:

ma​​in.cpp

QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QQuickWindow *wnd1 = engine.rootObjects()[0]->findChild<QQuickWindow *>("wnd1");
    if(wnd1)
        wnd1->setTitle("Server");
    QQuickWindow *wnd2 = engine.rootObjects()[0]->findChild<QQuickWindow *>("wnd2");
    if(wnd2)
        wnd2->setTitle("Client");

要捕获关闭事件,您应该使用 QQuickWindow::closing事件

To catch a closing event you should use QQuickWindow::closing event

这篇关于单个项目中的多个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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