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 ...
Thanks
David -
OK I think I can work round it by using image.disconnect() to disconnect all its outbound signals - is that correct?
-
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 -
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@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 theconnect()
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
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 theconnect()
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.
-
-
OK I think I can work round it by using image.disconnect() to disconnect all its outbound signals - is that correct?