Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a slot on quit

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

I want something like connect(this, SIGNAL(quit()), this, SLOT(updateDatabase())) One way could be to introduce a quit button, but is it possible to achieve this functionality if user presses Alt+F4?

like image 745
sudeepdino008 Avatar asked Feb 17 '23 23:02

sudeepdino008


2 Answers

Use signal aboutToQuit() instead.

This signal is emitted when the application is about to quit the main event loop, e.g. when the event loop level drops to zero. This may happen either after a call to quit() from inside the application or when the users shuts down the entire desktop session.

The signal is particularly useful if your application has to do some last-second cleanup. Note that no user interaction is possible in this state.

For example :

connect(this, SIGNAL(aboutToQuit()), this, SLOT(updateDatabase()));
like image 58
masoud Avatar answered Feb 27 '23 13:02

masoud


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

like this:

void MainWindow::closeEvent(QCloseEvent *event)
{
    call_your_slot_here();
    // accept close event
    event->accept();
}
like image 26
helsinki Avatar answered Feb 27 '23 12:02

helsinki