Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Disconnect all slots in an object

    General and Desktop
    3
    5
    2511
    Loading More Posts
    • 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.
    • Perdrix
      Perdrix last edited by

      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

      jsulm JonB 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @Perdrix last edited by

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

        1 Reply Last reply Reply Quote 0
        • jsulm
          jsulm Lifetime Qt Champion @Perdrix last edited by

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

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

          1 Reply Last reply Reply Quote 1
          • JonB
            JonB @Perdrix last edited by 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.

            Perdrix 1 Reply Last reply Reply Quote 2
            • Perdrix
              Perdrix @JonB last edited by

              @JonB

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

              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @Perdrix last edited by

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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post