Sequence of Slot Execution



  • I am emitting to signals like

    emit SendSignal1();
    emit SendSignal2();

    I understand that with direct connection they will be executed in order (and immediately). However, it's not clear to me what the behavior is if it's not a direct connection. Is it still guaranteed that the slot for signal 1 will be executed before the slot for signal 2? I don't mind if another slot comes between them but I need to have signal 1 execute before signal 2.

    The main reason why I am not using direct connections is because the sender and receiver live on different threads and the receiver (which is the GUI thread) needs to do some GUI stuff. With a direct connection the slot would be executed by the sender (the non-GUI thread).


  • Qt Champions 2016

    @qt27 said in Sequence of Slot Execution:

    Is it still guaranteed that the slot for signal 1 will be executed before the slot for signal 2?

    No!

    I don't mind if another slot comes between them but I need to have signal 1 execute before signal 2.

    The signals will be raised as you've written them, the slots connected to those signals may or may not execute in the order you expect. If you need to guarantee the order, then chain the signal-slot connections.

    The main reason why I am not using direct connections is because the sender and receiver live on different threads and the receiver (which is the GUI thread) needs to do some GUI stuff.

    You should be using Qt::AutoConnection (the default) and leave Qt deal with what object is in what thread.

    With a direct connection the slot would be executed by the sender (the non-GUI thread).

    Yes, as with any direct function call.

    Kind regards.



  • So it appears the slots are not necessarily executed in the same sequence as the signals have been emitted. That's pretty surprising. Good I asked. So how do I chain signals?


  • Moderators

    @qt27 said in Sequence of Slot Execution:

    So it appears the slots are not necessarily executed in the same sequence as the signals have been emitted. That's pretty surprising. Good I asked.

    In the current implementation of Qt, if all the signals are emitted from the same thread and all the receivers live in the same thread (even if signal thread != receiver thread), and if all of the connections use AutoConnection/QueuedConnection, then the sequence will be as you expect.

    However, I'm wondering if it's possible to make your sender emit only one signal, and let your receiver run both functions in the order you want?


  • Qt Champions 2016

    @qt27 said in Sequence of Slot Execution:

    So how do I chain signals?

    Sorry for the delay, I missed the notification somehow.
    By chain them I meant like this:

    class MyObject : class QObject
    {
        Q_OBJECT
    
    signals:
        void signal1();
        void signal2();
    
    public slots:
        void slot1()
        {
            // ... Do things and only then emit the signal
            emit signal2();
        }
    
        void slot2();
    };
    

    And then:

    MyObject obj;
    QObject::connect(&obj, &MyObject::signal1, &obj, &MyObject::slot1);
    QObject::connect(&obj, &MyObject::signal2, &obj, &MyObject::slot2); //< Now, because signal2() is raised from within the slot, we know that slot2() will be executed AFTER slot1()
    

    I hope that helps.
    Kind regards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.