Search code examples
c++qtsignals-slots

When are Qt slots called?


For example, I have a function

void A::fun()
{
    do_1();
    emit signal_1();

    do_2();
    emit signal_2();

    do_3();
}

There are connections between signal_1 and slot_1; between signal_2 and slot_2. When are the slot_1 and slot_2 called? Some answer options:

  1. After the fun's return, call slot_1 before slot_2;
  2. slot_1 is called after do_1 and slot_2 is called after do_2

or others.


Solution

  • For Direct connection ( default when not connecting from different thread )

    Slot are called immediately, so result should be:

    do_1
    slot_1
    do_2
    slot_2
    do_3
    

    For Queued connection called from the same thread ( need to set manually )

    Execution of function must end, and then main loop can call slots

    do_1 
    do_2 
    do_3 
    slot_1    
    slot_2 
    

    For Queued connection called from different thread

    It is more complicated because of threading problem. Result can be like in first or second example ( or combination ). You have no guarantee what calling order it will be!

    do_1 
    slot_1    
    do_2 
    do_3 
    slot_2 
    

    Here you can see description of Qt::ConnectionType