tags:

views:

112

answers:

2

Qt - c++ Show message when user close program Thanks

+2  A: 

Most general is to connect the aboutToQuit signal to a slot -- but as the docs say

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.

so if you need to show your message in Qt (so the main event loop must still be running) this won't work. In that case, I'd connect lastWindowClosed (and maybe also set quitOnLastWindowClosed to false, depending on your exact needs). If your app also ends in other ways (buttons, menus, etc) you'll have to connect those too.

Alex Martelli
+2  A: 

If your aim is just to display a message you can follow the answer from Alex Martelli.

Else if you need some sort of execution based on close, (such as asking the user about Do you want to save the changes? kinda thing), then handling the QCloseEvent* will do the trick.

In such case what I will do is that, extend QWidget class and override its

void QWidget::closeEvent ( QCloseEvent * event )   [virtual protected]

Under the definition of closeEvent ( QCloseEvent * event ), I will have my corresponding execution.

Hope it helps..

liaK