[Qt-interest] Correct way of connecting to the QCoreApplication::aboutToQuit() signal ?
Thiago Macieira
thiago at kde.org
Mon Feb 8 20:07:50 CET 2010
Em Segunda-feira 8. Fevereiro 2010, às 19.09.00, Julien Cugnière escreveu:
> The documentation mentions that one should connect to the
> QCoreApplication::aboutToQuit() signal in order to handle last minute
> cleanup when the application closes.
>
> However, can I connect to this signal a slot that doesn't live in the main
> thread ?
Why do you want to do that? I suggest you just place it in the main thread and
be done with it.
Anyway, you also have to ensure that the thread is running while the signal is
emitted. Otherwise, the signal will never be delivered to the slot.
> I'm basically afraid that if I connect with the default connection
> type (Qt::AutoConnection), Qt will not wait for my slot to finish before
> quitting the application...
You're right, it won't. If the target slot lives in a separate thread, the
AutoConnection decays to QueuedConnection. That means "fire-and-forget".
> So, given that I don't know from which thread
> this signal will be fired, do I need to connect to it using
> Qt::DirectConnection, or Qt::BlockingQueuedConnection ?
The signal will be fired from the main thread.
You choose which solution is best for you. If you use DirectConnection, your
slot will be called in the main thread, just as if the object lived in the
main thread. So to avoid any hassles, I recommend just placing the object in
the main thread once and for all.
As for BlockingQueuedConnection, the slot will be called in its proper thread,
which you must ensure to still be running. Emitting a signal that is connected
via BlockingQueuedConnection to a thread that isn't running results in
deadlock.
> NB : I know there are other way to achieve what I want, such as
> intercepting QCloseEvent, but I'm more interested in understanding the way
> aboutToQuit() is supposed to work.
aboutToQuit is just a signal. It doesn't work any differently from any other
signal.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Senior Product Manager - Nokia, Qt Development Frameworks
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100208/8f0732f9/attachment.bin
More information about the Qt-interest-old
mailing list