PDA

View Full Version : QCoreApplication::aboutToQuit() problem



high_flyer
29th December 2007, 01:37
Hi All,

I have this very simple console application, which basically runs with a polling timer, reads data, and saves it into a file.
Since its a console program, it can be terminated (only) by Ctrl+C, which is no problem.
However, I want to gracefully end my application in such a case, and write a closing tag in my data file.
From the docs, I thought that QCoreApplication::aboutToQuit() is my friend.

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.
But the slot connected to it does not run (or not being called) when the application is terminated with Ctrl+C.

I checked for mistyping signal/slot names, all seems to be "kosher" during build time.
Any idea how to achieve what I want, or what I might be missing?

Thanks.
Oh, this one is on Windows, with MinGW, Qt4.3.1

marcel
29th December 2007, 01:54
Any warnings in debug mode?
What happens if your close the application in any other way, such as closing the console window?

high_flyer
29th December 2007, 16:41
I don't have the debug libs under windows :o (used the exe installer)
So I tried this on linux (debug).
Nope, no warning, or anything.


What happens if your close the application in any other way, such as closing the console window?
nothing, behaves the same...

Do you know any other way of achieving this?

Thanks.

wysota
29th December 2007, 17:14
Do you reimplement the SIGINT signal handler? If not, you could do that and close your application in the handler. Similar for SIGTERM. If you didn't catch the signal, the application gets sent SIGTERM and exits immediately, so aboutToQuit() won't be executed. You can call quit() from within the handler and it should work fine then.

high_flyer
29th December 2007, 17:33
so you mean the system signals?
I am a bit confused to what you mean and not sure in which direction to search...
Everything I found so far dealt with lowlevel system code...
Is there a Qt handler (public/protected) for those signals?

wysota
29th December 2007, 17:47
Yes, UNIX signals. They are ANSI-C compatible so they should work on non-unix systems as well. Without it when you press CTRL+C the default SIGINT handler will be called and your application will call ::exit() immediately, thus preventing the QApplication object from leaving the event loop, calling destructors or anything. The process will simply die.

man signal is your friend.

high_flyer
29th December 2007, 20:10
Ok, I see now, thanks.
Lerned something new :)

Oh, and another question:
for which cases is aboutToQuit() signal?
From the docs I thought the application manages to emit this signal before it gets closed by the system, but your explanation makes sense as to why it doesn't...

wysota
29th December 2007, 22:20
Never heard of it not (of course) used it. It is probably used when you exit the desktop session (i.e. click "logout") when the session manager kicks in. It's just a guess though. In your case the application gets killed, so no code is executed (you could probably use atexit() to perform some cleanup, but it wouldn't work for aborts anyway).

katrina
30th December 2007, 00:05
I am pretty sure QCoreApplication::aboutToQuit() is normally only emitted after a call to QCoreApplication::exit(int returnCode)
(QCoreApplication::quit() is an alias for QCoreApplication::exit(0), btw)

when you call QCoreApplication::exit(), it sets aboutToQuit in the thread data class to true and tells all event loops to stop, once the main event loop has stopped, QCoreApplication emits aboutToQuit() right before returning from QCoreApplication::exec()

high_flyer
30th December 2007, 17:43
Do you reimplement the SIGINT signal handler? If not, you could do that and close your application in the handler.
Just to say it works like a charm both for Linux and Windows, thanks again! :)