Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sequence of Slot Execution
Forum Updated to NodeBB v4.3 + New Features

Sequence of Slot Execution

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 5.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • qt27Q Offline
    qt27Q Offline
    qt27
    wrote on last edited by
    #1

    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).

    kshegunovK 1 Reply Last reply
    0
    • qt27Q qt27

      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).

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • qt27Q Offline
        qt27Q Offline
        qt27
        wrote on last edited by
        #3

        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?

        JKSHJ kshegunovK 2 Replies Last reply
        0
        • qt27Q qt27

          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?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by JKSH
          #4

          @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 Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • qt27Q qt27

            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?

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @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.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved