[Solved] How can I capture Command+Q in a Qt Quick application?



  • My application creates a QApplication, then a QQuickView, and then moves through the various screens by changing the "url" property of the QQuickView's rootObject().

    The application is supposed to appear standalone (the user doesn't need to know there's a Mac running it) so it has no menu and no way for the user to exit.

    However, on occasion there is a need to exit the application (and our users are allowed to do that), for which we normally just hit Command+Q. This does in fact exit the application, but also throws up a large panic screen about the program quitting unexpectedly.

    I've tried making my own classes deriving from QApplication and QQuickView and implementing a variety of signals and virtual overrides, but can't find anything that lets me jump in when a Command+Q gets hit and do some cleanup to avoid the panic screen.

    Any suggestions?

    Thanks,
    Chris


  • Moderators

    Use this "flag":http://qt-project.org/doc/qt-5/qt.html#WidgetAttribute-enum:
    @
    Qt::WA_DeleteOnClose
    @

    on your top level window. This will invoke the destructor when the window is being closed and allow your app to properly clear the memory. Also, make sure you don't create your QGuiApplication instance on heap to avoid problems there.



  • I'm confused ... my main window is a QQuickView which doesn't derive from QWidget, so I don't have any way to set a WidgetAttribute like WA_DeleteOnClose?


  • Moderators

    Hm, ok. I have been fooled by your mention of QApplication, instead of QGuiApplication.

    In such case, I think you could connect application's aboutToClose() signal, with your window's close() slot. Or, alternatively, the deleteLater() slot.
    @
    QGuiApplication app(a, c);
    QQuickView myView;

    QObject::connect(&app, SIGNAL(aboutToClose(), &myView, SLOT(close()));

    myView.setSource(QUrl("blah blah.qml"));

    return app.exec();
    @

    If that still does not fix the problem, then we will think on how you can intercept the Command+Q.



  • Since our application always runs full-screen, the only time it gets hidden is when it gets closed - so I subclassed QQuickView with my own MainView and wrote my own override hideEvent() that did the necessary shutdown.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.