⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Disconnect all slots in an object

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


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

  • Lifetime Qt Champion

  • @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.

  • @JonB

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

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

Log in to reply