[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 -
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.
-
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.