How to call an output slot

I want to update my database before the Qt application closes.

I need something like connect(this, SIGNAL(quit()), this, SLOT(updateDatabase())) One way could be to enter the exit button, but is it possible to achieve this functionality if the user press Alt+F4 ?

+4
source share
2 answers

Use the aboutToQuit() signal instead.

This signal is emitted when the application is about to exit the main event loop, for example. when the event loop level drops to zero. This can happen either after calling the quit () function from the application, or when users close the entire desktop session.

A signal is especially useful if your application needs a last-second cleanup. Please note that state interaction is not possible in this.

For instance:

 connect(this, SIGNAL(aboutToQuit()), this, SLOT(updateDatabase())); 
+6
source

There is another way to do this, not aboutToQuit() , but reinstall closeEvent(QCloseEvent *event) . You can call the slot before the event->accept() statement;

like this:

 void MainWindow::closeEvent(QCloseEvent *event) { call_your_slot_here(); // accept close event event->accept(); } 
+2
source

All Articles