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. Disconnect all slots in an object
Forum Update on Monday, May 27th 2025

Disconnect all slots in an object

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 8.5k Views
  • 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by
    #1

    Lets say I have two ImageObserver objects a and b, and that I have connected signals from another object called image. For example in image's mousePressEvent(QMouseEvent * e) I might call emit Image_mousePressedEvent(e); and similarly for mouseMoveEvent().

    Else where in the code depending on what the user has done I might write:

    QObject::connect(&image, &MyImageClass::Image_mousePressedEvent, 
                                      &a, &ImageObserver::mousePressEvent);
    QObject::connect(&image, &MyImageClass::Image_mouseMovedEvent, 
                                      &a, &ImageObserver::mouseMoveEvent);
    
    

    If later on I wish to redirect these events to the ImageObserver b, how to disconnect them all from ImageObserver a? I don't see a function signature for QObject::disconnect() that allows me to say disconnect all sending objects from the slots of this object?

    I'm sure this is easy and that I'm missing something ...

    Thanks
    David

    jsulmJ JonBJ 2 Replies Last reply
    0
    • PerdrixP Perdrix

      @JonB

      OK I think I can work round it by using image.disconnect() to disconnect all its outbound signals - is that correct?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #5

      @Perdrix
      Yes, that falls under "You can disconnect all slots from an object's signal".

      1 Reply Last reply
      0
      • PerdrixP Perdrix

        Lets say I have two ImageObserver objects a and b, and that I have connected signals from another object called image. For example in image's mousePressEvent(QMouseEvent * e) I might call emit Image_mousePressedEvent(e); and similarly for mouseMoveEvent().

        Else where in the code depending on what the user has done I might write:

        QObject::connect(&image, &MyImageClass::Image_mousePressedEvent, 
                                          &a, &ImageObserver::mousePressEvent);
        QObject::connect(&image, &MyImageClass::Image_mouseMovedEvent, 
                                          &a, &ImageObserver::mouseMoveEvent);
        
        

        If later on I wish to redirect these events to the ImageObserver b, how to disconnect them all from ImageObserver a? I don't see a function signature for QObject::disconnect() that allows me to say disconnect all sending objects from the slots of this object?

        I'm sure this is easy and that I'm missing something ...

        Thanks
        David

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Perdrix https://doc.qt.io/qt-5/qobject.html#disconnect

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • PerdrixP Perdrix

          Lets say I have two ImageObserver objects a and b, and that I have connected signals from another object called image. For example in image's mousePressEvent(QMouseEvent * e) I might call emit Image_mousePressedEvent(e); and similarly for mouseMoveEvent().

          Else where in the code depending on what the user has done I might write:

          QObject::connect(&image, &MyImageClass::Image_mousePressedEvent, 
                                            &a, &ImageObserver::mousePressEvent);
          QObject::connect(&image, &MyImageClass::Image_mouseMovedEvent, 
                                            &a, &ImageObserver::mouseMoveEvent);
          
          

          If later on I wish to redirect these events to the ImageObserver b, how to disconnect them all from ImageObserver a? I don't see a function signature for QObject::disconnect() that allows me to say disconnect all sending objects from the slots of this object?

          I'm sure this is easy and that I'm missing something ...

          Thanks
          David

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @Perdrix
          This was asked recently.

          You can disconnect all slots from an object's signal. You cannot disconnect all signals from a slot.

          If you want to achieve the latter, you would have to do one one of:

          • Save all the connect() return results, and iterate through them disconnecting. Does not work if you are not in charge of all the connect() statements.

          • Introduce a "proxy" signal/slot layer, so slots are connected to signals via a signal forwarder class. Then you can do your own actions in that proxy-signal-class to achieve what you want.

          PerdrixP 1 Reply Last reply
          2
          • JonBJ JonB

            @Perdrix
            This was asked recently.

            You can disconnect all slots from an object's signal. You cannot disconnect all signals from a slot.

            If you want to achieve the latter, you would have to do one one of:

            • Save all the connect() return results, and iterate through them disconnecting. Does not work if you are not in charge of all the connect() statements.

            • Introduce a "proxy" signal/slot layer, so slots are connected to signals via a signal forwarder class. Then you can do your own actions in that proxy-signal-class to achieve what you want.

            PerdrixP Offline
            PerdrixP Offline
            Perdrix
            wrote on last edited by
            #4

            @JonB

            OK I think I can work round it by using image.disconnect() to disconnect all its outbound signals - is that correct?

            JonBJ 1 Reply Last reply
            0
            • PerdrixP Perdrix

              @JonB

              OK I think I can work round it by using image.disconnect() to disconnect all its outbound signals - is that correct?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @Perdrix
              Yes, that falls under "You can disconnect all slots from an object's signal".

              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