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

    JonBJ 1 Reply Last reply
    0
    • S Stefanoxjx

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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 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.

        JonBJ 1 Reply Last reply
        0
        • S Stefanoxjx

          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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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 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

            • Login

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