tags:

views:

213

answers:

1

If I had a class A, where one of its functions does:

void A::func()
{
    emit first_signal();
    emit second_signal();
}

Assuming that a class B has 2 slots, one connected to first_signal, and the other to second_signal, is it guaranteed that the slot that is connected to first_signal will always be processed before the second_signal slot?

+3  A: 

If you use default connection type between signals and slots (Qt::DirectConnection) then the answer is yes.

From Qt help system:

When a signal is emitted, the slots connected to it are usually executed immediately, just like a normal function call. When this happens, the signals and slots mechanism is totally independent of any GUI event loop. Execution of the code following the emit statement will occur once all slots have returned. The situation is slightly different when using queued connections; in such a case, the code following the emit keyword will continue immediately, and the slots will be executed later.

You can change default connection type to any of enum Qt::ConnectionType in QObject::connect method.

kemiisto
I think that it would be guaranteed as long as they have the same type, even if it isn't default. The queued one would create two events, each with the same priority, and those events would then be processed in order. Also, the default connection type is Qt::AutoConnection, which resolves to direct as long as both emitter and receiver are in the same thread.
Caleb Huitt - cjhuitt