Multiple windows in one project

I have a requirement for my project to display two QML Windowon each of them (one sender, one receiver). Both of them .qmlrequire me to include some Cpp models inside, I use QQmlApplicationEngineCpp for registration of models.

I found out that with the help QWidget::createWindowContainer()I can display several Windowfor one project. This works great for the first QML file. The code snippets are as follows:

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 second application engine for mine receive.qmlusing a similar method. I soon realized that it receive.qmlwould never open even with container2->show(). Now it shows a blank page.

My questions:

  • Is my approach right or is there a better solution for this?
  • , ? , , . , .
+4
1

, :

main.qml

import QtQuick 2.3
import QtQuick.Window 2.2

Item {

    Window {
        objectName: "wnd1"
        visible: true
    }

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

, ++:

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:: closed event

+6

All Articles