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. In which order are triggered the slots that are connected to the same signal?
Forum Updated to NodeBB v4.3 + New Features

In which order are triggered the slots that are connected to the same signal?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.7k Views 1 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.
  • mbruelM Offline
    mbruelM Offline
    mbruel
    wrote on last edited by
    #1

    Hi,
    I'm just wondering if Qt defines the order of trigger of the slots that are connected to the same signal?
    Especially if some slots lives in the same thread and are Queued.
    Would it be by order of the creation of the connection or is it something random?
    Is it the same with DirectConnection?
    Cheers

    1 Reply Last reply
    0
    • W Offline
      W Offline
      wrosecrans
      wrote on last edited by
      #2

      If you are depending on strict ordering when you have threads, it's sort of the wrong question to be asking. But if all connections are the same type then they should all be invoked in the same order they were connected.

      1 Reply Last reply
      1
      • mbruelM Offline
        mbruelM Offline
        mbruel
        wrote on last edited by mbruel
        #3

        Well I'm talking within the same thread, I'd like to know if there is a strict ordering.
        I know it could be some kind of bad design to rely on that but I'm wondering internally how it is working and if we could use it.
        I've two active objects running on a Main thread that are connected to a same signal received from other threads.
        Would Qt insure that one would be triggered before the other? Could we modify this order?

        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          I don't think it's safe to rely upon the behaviour you seek. You should strive to think of the signals as asyncronous, even if they are not totally so.

          1 Reply Last reply
          0
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            Although I tend to agree with @Kent-Dorfman that it may not be "advisable" to rely on this behaviour, nonetheless Qt docs do explicitly state https://doc.qt.io/qt-5/signalsandslots.html#signals:

            If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.

            and later on:

            If on the other hand you want to call two different error functions when the number overflows, simply connect the signal to two different slots. Qt will call both (in the order they were connected).

            I would guess that having been designed and written like this, Qt will likely to stick to it.

            And, no, you cannot change that order --- other than by disconnecting and reconnecting slots into desired order. Or, you'd have to attach just one "super-slot" to the signal, and have that run as a "dispatcher" for other "slots" in desired order --- which would mean altering your calling code for setting up slots.

            mbruelM 1 Reply Last reply
            2
            • JonBJ JonB

              Although I tend to agree with @Kent-Dorfman that it may not be "advisable" to rely on this behaviour, nonetheless Qt docs do explicitly state https://doc.qt.io/qt-5/signalsandslots.html#signals:

              If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.

              and later on:

              If on the other hand you want to call two different error functions when the number overflows, simply connect the signal to two different slots. Qt will call both (in the order they were connected).

              I would guess that having been designed and written like this, Qt will likely to stick to it.

              And, no, you cannot change that order --- other than by disconnecting and reconnecting slots into desired order. Or, you'd have to attach just one "super-slot" to the signal, and have that run as a "dispatcher" for other "slots" in desired order --- which would mean altering your calling code for setting up slots.

              mbruelM Offline
              mbruelM Offline
              mbruel
              wrote on last edited by
              #6

              @JonB
              ok thanks, good to know. I didn't go read again the signal/slot page before posting my question, I guess I should have...

              I also agree it is bad design to rely on that but you might do it by mistake. On my app this was the case, I've a signal connected to 2 slots, the main one is created in the constructor of my object so will be triggered first and in some condition it might trigger the destruction of the object.
              The second one was added later on for GUI purposes, to update a value, and I was getting a segfault sometimes, mainly by my customers, never on my environment (would have been too easy...) as I could use a slot using a dangling sender.

              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