Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - How to intercept the application's close event (if there's one)

I need to do some work right before my application closes (note that I said my application and not it's main window). Is there an application close event or any other way to accomplish this?

The reason that I can't rely on the main window's close event is that my application runs on the background leaving a system tray icon.

like image 581
Raphael Avatar asked Dec 07 '10 21:12

Raphael


2 Answers

There is a signal from QCoreApplication (inherited by QApplication) called aboutToQuit that is fired immediately before the application terminates.

like image 191
sje397 Avatar answered Nov 15 '22 08:11

sje397


Your application is likely derived from QApplication. You could 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 do work after calling exec():

int main(int argc, char *argv[]) {
    MyApp a(argc, argv);
    int r = a.exec();
    // cleanup code here
    return r;
}
like image 37
Greg Hewgill Avatar answered Nov 15 '22 08:11

Greg Hewgill