Search code examples
c++qtqmlqtquick2qt-quick

QML: closing QQuickWindow closes my application


When QML engine creates window it implicitly connects QQuickWindow to application's closing event so if I close QML windows application quits too. Is there a way to avoid this behavior? I want periodically to load and destroy instances of QQuickWindow so that will not close my application.


Solution

  • You can use setQuitOnLastWindowClosed in QGuiApplication. The property indicates whether the application should quit when the last window is closed or not. The default value is true, you can change it to false.

    Your main can be like :

    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        qApp->setQuitOnLastWindowClosed(false);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    
        return app.exec();
    }