单个项目中有多个窗口 [英] Multiple windows in a single project
问题描述
我的项目要求在一个屏幕(一个发送器,一个接收器)上分别显示两个QML Window
.这两个.qml
都要求我在其中包含一些Cpp模型,因此,我正在使用QQmlApplicationEngine
注册Cpp模型.
I have a requirement for my project to display two QML Window
s 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 Window
s 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.
我的问题是:
- 我的方法是正确的还是对此有更好的解决方案?
- 我需要注意什么信号才能关闭窗户 事件?我似乎无法检测到信号时, 窗口关闭.当我想关闭两者时 检测到.
- Is my approach correct or is there a better solution for this?
- What signal do I need look out for to catch the window close event? I cant seem to be able to detect the signal when one of the window is closed. as I wanted to close the both when one has been detected.
推荐答案
可以轻松完成,例如:
main.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:
main.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屋!