Qt MainWindow CloseEvent Mac Cmd+Q

c++, macos, qt

Solution

By default on the Mac, Qt will create an Apple Menu | Quit if a menubar doesn't exist with either quit or exit entry. That default created entry will call `QApplication::quit()` which will not trigger your `MyWindow::closeEvent()`.

In your UI you should add a menu item named Exit (on the Mac it will be automagically renamed to Quit) and in the `MyWindow` class constructor you should connect that action to the `close()` slot (which is inherited from `QWidget`).

Update- To take a shot at your additional questions, no the destructor should probably only be used for deallocation of memory (releasing file locks, etc). Anything that could potentially involve user interaction (such prompting for a file location or alerting the user via a QMessageBox that something failed) will need to go in the `closeEvent` method. Saving window geometry (and other simple items using QSettings) should also be done via the `closeEvent` (though, I have seen code that saves geometry in the destructor work, there could be some edge cases where it does unexpected things).

Problem

On OS X 10.9 with Qt 5.2 and the following application code ``` #include "mywindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MyWindow w(); w.show(); return a.exec(); } ``` where `MyWindow` derives from `QMainWindow` and overwrites the `closeEvent(QCloseEvent*)` method, this method is called when I close the application window (e.g. click the windows (x) close button), but it is not invoked when I close the application via the main Menu -> "Quit " or via the "CMD + Q" key shortcut. From looking around the web and numerous question I got the impression that closing the application should invoke the closeEvent for all top level windows. Is this not the case, or is something going wrong here? Edit: In addition to the above ways of closing the application, are there any other instances that I in general should handle that would result in `QApplication::quit` rather than invoking the window's `close` event? What about a system shutdown for example? When handling a `close` event, I'm confirming that the user really wants to quit and I make sure cleanup like writing back changed settings is happening. Should I maybe move cleanup / saving settings to the destructor instead and do the confirmation query in `closeEvent`?

Original source