简体   繁体   中英

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.

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

This link might be helpful for simpler understanding

http://doc.qt.io/qt-5/signalsandslots.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM