9

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.
BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
angelhalo
  • 131
  • 1
  • 1
  • 8

1 Answers1

17

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

folibis
  • 12,048
  • 6
  • 54
  • 97
  • nice ! the solution works like a charm. it seems like we can only have 1 engine at anytime.. As for shifting the 2nd window to 2nd screen, I obtained the availableGeometry from qApplication::desktop and set the window2's x(), y() using wnd2->setX(..) & wnd2->setY(..) to get the desired position. Is it ok to set x, y directly via the QQuickWindow ? Or do I have to use QObject::setProperty ? – angelhalo Jul 09 '15 at 09:05
  • Update: from http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html it seems that it is recommended to use setProperty to set the X and Y. I shall adopt this method. – angelhalo Jul 09 '15 at 09:21
  • You can use [Screen](http://doc.qt.io/qt-5/qml-qtquick-window-screen.html) item to adjust size and position in QML – folibis Jul 09 '15 at 11:16
  • 3
    It's not working for me. I put a Window in an Item and the Window never shows up. – RnMss May 20 '18 at 08:09
  • 1
    It's not working for me too in Windows. Nothing shows up. – JustWe May 18 '20 at 08:26
  • The answer is not about showing a `Window` but accessing multiple `Window` instances from c++. So the code is just for illustrating the solution, for example windows have no size etc., you have to adopt it to your code. – folibis May 18 '20 at 08:41
  • This seems to be broken on the latest Qt5 version on windows. The windows won't show at all. My workaround was to place the second window inside a Loader so it's not associated with the root window. – Tim Woocker Jun 01 '21 at 17:30
  • It works on windows for me on Qt 5.12 if i replace Item with QtObject. See example: https://doc.qt.io/qt-5.12/qtquick-window-window-qml.html – Vladimir Oct 19 '21 at 18:40
  • How to do it in Qt 6.2, please? – Franky Jan 23 '22 at 09:45