Multiple windows in a single project

11,879

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

Share:
11,879
angelhalo
Author by

angelhalo

Updated on July 24, 2022

Comments

  • angelhalo
    angelhalo almost 2 years

    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.
  • angelhalo
    angelhalo almost 9 years
    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
    angelhalo almost 9 years
    Update: 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
    folibis almost 9 years
    You can use Screen item to adjust size and position in QML
  • RnMss
    RnMss about 6 years
    It's not working for me. I put a Window in an Item and the Window never shows up.
  • JustWe
    JustWe about 4 years
    It's not working for me too in Windows. Nothing shows up.
  • folibis
    folibis about 4 years
    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.
  • Tim Woocker
    Tim Woocker about 3 years
    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.
  • Vladimir
    Vladimir over 2 years
    It 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
    Franky over 2 years
    How to do it in Qt 6.2, please?