PDA

View Full Version : execute sth before program exits (user closes window)



donglebob
9th December 2008, 16:49
I need to write on a device sth like "bye bye" before my program exits. (User closes the window).

How can I do that?
There must be a signal which is sent before destroying all the instances of the app.
I must be sure that no instances of my objects are destroyed, so I can write on the device.

After writing my "bye bye" message on the device, the app can be closed (every instances of my objects can be destroyed).

wysota
9th December 2008, 17:11
You can either reimplement closeEvent of any of your windows, reimplement any of the destructors, reimplement the application and save the data in its destructor or save the data after QApplication::exec() returns in main() - the choice is yours.

rexi
9th December 2008, 18:03
You can either reimplement closeEvent of any of your windows, reimplement any of the destructors, reimplement the application and save the data in its destructor or save the data after QApplication::exec() returns in main() - the choice is yours.

Wow, what an exhaustive list. I guess the only thing you didn't mention is the QCoreApplication::aboutToQuit() signal ;) At least that's what I use for this purpose.

donglebob
9th December 2008, 18:56
Thanks both I will test all of your suggestions.

I thought about

myapp::closeEvent(..... *event)
{
emit sendcmd(); //here I write my "bye bye" on the device
closePort(); // here I close the device
event->accept() // here i finally exit the program
}

I found that this was not a good idea because:
Before I could write sth on the device "closePort" was called....

I think I should make the "emit sendcmd()" direct connected?? Right?
It will return when all the stuff is finished, so closePort can be executed.

Any other ideas to do that?
I think there is not "return value" for signals which i can check if I can proceed or not.

wysota
9th December 2008, 19:19
What exactly are you trying to achieve anyway? If you explain it to us, it'll be easier to suggest some concrete solution.

donglebob
9th December 2008, 21:37
I try to inform the remote side that my application will be closed and cannot receive anymore.

Instead of forcing the user to click on a "disconnect" button i would do this internal before exiting the program.

It must happen after pressing Alt+F4 or clicking on X and before all the destructors of my objects are called. The whole functionality of the program must still exist in this small time window.

wysota
9th December 2008, 23:24
In that case using QCoreApplication::aboutToQuit() or spinning a local event loop after QCoreApplication::exec() returns should suit your situation fine.

Remember that closing a window doesn't destroy it so you can still do everything with its objects until you call delete on it or the object runs out of scope.