Multiple windows in a single project

杀马特。学长 韩版系。学妹 提交于 2019-12-04 22:45:57

问题


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.

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...

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.

My questions are:

  1. Is my approach correct or is there a better solution for this?
  2. 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.

回答1:


That can be done easier, for example:

main.qml

import QtQuick 2.3
import QtQuick.Window 2.2

Item {

    Window {
        objectName: "wnd1"
        visible: true
    }

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

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");

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



来源:https://stackoverflow.com/questions/31298810/multiple-windows-in-a-single-project

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