Multiple windows in a single project
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
angelhalo
Updated on July 24, 2022Comments
-
angelhalo almost 2 years
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 usingQQmlApplicationEngine
to register the Cpp models.I found out that using
QWidget::createWindowContainer()
I'm able to display multipleWindow
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...
I decided to create a 2nd application engine for my
receive.qml
with a similar method. I soon realized that thereceive.qml
would never open even withcontainer2->show()
. Now, it is showing an empty page.My questions are:
- 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.
-
angelhalo almost 9 yearsnice ! 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 almost 9 yearsUpdate: from 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.
-
folibis almost 9 yearsYou can use Screen item to adjust size and position in QML
-
RnMss about 6 yearsIt's not working for me. I put a Window in an Item and the Window never shows up.
-
JustWe about 4 yearsIt's not working for me too in Windows. Nothing shows up.
-
folibis about 4 yearsThe answer is not about showing a
Window
but accessing multipleWindow
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. -
Tim Woocker about 3 yearsThis 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.
-
Vladimir over 2 yearsIt works on windows for me on Qt 5.12 if i replace Item with QtObject. See example: doc.qt.io/qt-5.12/qtquick-window-window-qml.html
-
Franky over 2 yearsHow to do it in Qt 6.2, please?