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. One signal, multiple slots
Forum Updated to NodeBB v4.3 + New Features

One signal, multiple slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 3.5k 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.
  • W Offline
    W Offline
    WhatIf
    wrote on last edited by
    #1

    Hi,

    I have a text edit that emits textChanged signal, which I use to change the color of text if it was modified. But I also want to validate the input.

    Should I create another slot for the same signal textChanged? Or combine the code for changing the color and validation in one slot?

    kshegunovK 1 Reply Last reply
    0
    • W WhatIf

      Hi,

      I have a text edit that emits textChanged signal, which I use to change the color of text if it was modified. But I also want to validate the input.

      Should I create another slot for the same signal textChanged? Or combine the code for changing the color and validation in one slot?

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

      @WhatIf
      You may want to use a validator instead?

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • W Offline
        W Offline
        WhatIf
        wrote on last edited by
        #3

        I will check QValidator but which is the better approach.

        1. one signal, all unrelated code in one slot (in my case, changing color + validation)
        2. one signal, multiple slots

        Put my problem aside, which is better design?

        kshegunovK A 2 Replies Last reply
        0
        • W WhatIf

          I will check QValidator but which is the better approach.

          1. one signal, all unrelated code in one slot (in my case, changing color + validation)
          2. one signal, multiple slots

          Put my problem aside, which is better design?

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

          @WhatIf
          Don't take this the wrong way, but the question sounds like: "Which is better apples or oranges?". Well, it depends.

          On the one hand it depends on the semantics - is the color change independent of the data validation, that is: will the color be different if data is changed no matter the data may be invalid? If the answer is yes, then that suggests two slots connected to a single signal.
          On the two hand, it's also a matter of taste.

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • W WhatIf

            I will check QValidator but which is the better approach.

            1. one signal, all unrelated code in one slot (in my case, changing color + validation)
            2. one signal, multiple slots

            Put my problem aside, which is better design?

            A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            @WhatIf said:

            I will check QValidator but which is the better approach.

            1. one signal, all unrelated code in one slot (in my case, changing color + validation)
            2. one signal, multiple slots

            Put my problem aside, which is better design?

            Another option is to have one slot, let's call it "reactOnTextChanged", which in turn calls the separate methods. Makes the code easy to read, too. You see exactly what "reactOnTextChanges" does if the methods it calls are well-named.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              WhatIf
              wrote on last edited by
              #6

              One last question, is the order of calling the slots guaranteed?

              For example, if the first connect calls changeColor() and the second connect calls validate(). Am I guaranteed to have changecolor() slot called and complete first before validate() slot is called?

              I'm asking because the behavior I'm getting it's as if mid-way through the first slot, the second slot begins to execute.

              Would greatly appreciate your clarification!

              kshegunovK A 2 Replies Last reply
              0
              • W WhatIf

                One last question, is the order of calling the slots guaranteed?

                For example, if the first connect calls changeColor() and the second connect calls validate(). Am I guaranteed to have changecolor() slot called and complete first before validate() slot is called?

                I'm asking because the behavior I'm getting it's as if mid-way through the first slot, the second slot begins to execute.

                Would greatly appreciate your clarification!

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

                @WhatIf

                is the order of calling the slots guaranteed?

                When the connection is inside a single thread (Qt::AutoConnection is equal to Qt::DirectConnection) then yes, the order of slot invocations is in the order the connects were made. Order is undefined when the connections are across threads. However, bear in mind that the order of the slots is an implementation detail and you shouldn't depend on it.

                For example, if the first connect calls changeColor() and the second connect calls validate(). Am I guaranteed to have changecolor() slot called and complete first before validate() slot is called?

                Yes, if you're connecting objects with the same thread affinity ("living" in the same thread).

                I'm asking because the behavior I'm getting it's as if mid-way through the first slot, the second slot begins to execute.

                The only way this may happen is if you have the slots executed directly from two different threads, in all other cases the slots' execution is serialized.

                Kind regards.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • W WhatIf

                  One last question, is the order of calling the slots guaranteed?

                  For example, if the first connect calls changeColor() and the second connect calls validate(). Am I guaranteed to have changecolor() slot called and complete first before validate() slot is called?

                  I'm asking because the behavior I'm getting it's as if mid-way through the first slot, the second slot begins to execute.

                  Would greatly appreciate your clarification!

                  A Offline
                  A Offline
                  Asperamanca
                  wrote on last edited by
                  #8

                  @WhatIf said:

                  I'm asking because the behavior I'm getting it's as if mid-way through the first slot, the second slot begins to execute.

                  Have you checked the call stack? It is possible that the 1st slot makes some change that causes the related signals to fire again. That would explain it.

                  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