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. Multiple call to QObject::connect
Forum Updated to NodeBB v4.3 + New Features

Multiple call to QObject::connect

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 337 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.
  • S Offline
    S Offline
    Stefanoxjx
    wrote on 31 Jan 2021, 18:13 last edited by
    #1

    Hi, I asked me from a time.
    What's appens if I call multiple time QObject::connect with same signal but with different slot?
    For example:

    QPushButton *btn = new QPushButton("Hello World");
    connect(btn, SIGNAL(released()), this, SLOT(slot1));
    connect(btn, SIGNAL(released()), this, SLOT(slot2));
    connect(btn, SIGNAL(released()), this, SLOT(slot3));
    connect(btn, SIGNAL(released()), this, SLOT(slot4));
    connect(btn, SIGNAL(released()), this, SLOT(slot1));
    

    My intent may be to change slot for same button based on the current context of the program.

    Can this solution lead to problems?
    Thanks.

    J 1 Reply Last reply 31 Jan 2021, 18:19
    0
    • S Stefanoxjx
      31 Jan 2021, 18:13

      Hi, I asked me from a time.
      What's appens if I call multiple time QObject::connect with same signal but with different slot?
      For example:

      QPushButton *btn = new QPushButton("Hello World");
      connect(btn, SIGNAL(released()), this, SLOT(slot1));
      connect(btn, SIGNAL(released()), this, SLOT(slot2));
      connect(btn, SIGNAL(released()), this, SLOT(slot3));
      connect(btn, SIGNAL(released()), this, SLOT(slot4));
      connect(btn, SIGNAL(released()), this, SLOT(slot1));
      

      My intent may be to change slot for same button based on the current context of the program.

      Can this solution lead to problems?
      Thanks.

      J Offline
      J Offline
      JonB
      wrote on 31 Jan 2021, 18:19 last edited by
      #2

      @Stefanoxjx
      You may connect as many slots as you wish to a signal.

      My intent may be to change slot for same button based on the current context of the program.

      Whether that's a good idea is a different matter. Don't forget you would also want to disconnect the previous slot if you did this, if you want to change slot.

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Stefanoxjx
        wrote on 31 Jan 2021, 18:25 last edited by
        #3

        Thanks for answer.
        You are right, I don't think to disconnect.
        For disconnect I need to call QObject::disconnect with same parameters to connect:

        //To connect
        connect(btn, SIGNAL(released()), this, SLOT(slot1));
        
        //To disconnect
        disconnect(btn, SIGNAL(released()), this, SLOT(slot1));
        

        It's correct?
        Thanks.

        J 1 Reply Last reply 31 Jan 2021, 18:31
        0
        • S Stefanoxjx
          31 Jan 2021, 18:25

          Thanks for answer.
          You are right, I don't think to disconnect.
          For disconnect I need to call QObject::disconnect with same parameters to connect:

          //To connect
          connect(btn, SIGNAL(released()), this, SLOT(slot1));
          
          //To disconnect
          disconnect(btn, SIGNAL(released()), this, SLOT(slot1));
          

          It's correct?
          Thanks.

          J Offline
          J Offline
          JonB
          wrote on 31 Jan 2021, 18:31 last edited by JonB
          #4

          @Stefanoxjx
          Yes that is correct.

          The alternative is to keep just one slot, maintain information about state in whatever shape or form, and call different methods from the one slot based on the state instead. Avoids disconnecting.

          Since you are working with signals/slots: My personal bug-bear/advice is to switch to use New Signal Slot Syntax. Nothing to do with your issue/choice though.

          1 Reply Last reply
          3
          • S Offline
            S Offline
            Stefanoxjx
            wrote on 31 Jan 2021, 18:35 last edited by
            #5

            Many thanks for clarifications.
            I think that I will proceed with one slot that process status and calls multiple methods.

            Thanks.

            Stefano

            1 Reply Last reply
            1

            1/5

            31 Jan 2021, 18:13

            • Login

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