Qt - how to intercept the application closing event (if any)

I need to do some work right before closing my application (note that I said my application and this is not the main window). Is there an application closure or some other way to do this?

The reason I cannot rely on the event of closing the main window is because my application starts in the background, leaving the icon in the system tray.

+4
source share
2 answers

Your application is most likely derived from QApplication . You can put your cleanup code in the destructor of your application:

 class MyApp: public QApplication { public: ~MyApp() { // cleanup code here } }; 

Or, in your main() , you probably have something like this:

 int main(int argc, char *argv[]) { MyApp a(argc, argv); return a.exec(); } 

You can work after calling exec() :

 int main(int argc, char *argv[]) { MyApp a(argc, argv); int r = a.exec(); // cleanup code here return r; } 
+8
source

There is a signal from QCoreApplication (an inherited QApplication ) called aboutToQuit , which starts immediately before the application terminates.

+16
source

All Articles