PDA

View Full Version : Disconnect all SLOTS in QObject connected from all other SIGNALS



JasonCA
19th August 2015, 06:53
I've been struggling with this. I've not found a way to accomplish the following ...

I have a number of different signals connected to various slots in a single QObject. Is there any disconnect function that can be used to disconnect all signals connected to a all slots in a QObject?

For example, let's say I have a QObject (we'll call MyQObject for now) that I would simply like to take temporarily offline. I'd like all signals that are connected to the slots of the MyQObject to be essentially disconnected (but only temporarily). Then, when I am ready for MyQObject to go back online, for the signals that were once connected to the slots of MyQObject to be re-established.

For example: (assume MyQObject's variable is called myQObject)

QObject::connect(senderObject1, SIGNAL(doSomethingA()), myQObject, SLOT(slotDoA()));
QObject::connect(senderObject2, SIGNAL(doSomethingB()), myQObject, SLOT(slotDoB()));
QObject::connect(senderObject3, SIGNAL(doSomethingC()), myQObject, SLOT(slotDoC()));

Therefore, I'd like to temporarily disconnect all signals that are being emitted to myQObject's slots. MyQObject would then remain offline for a period of time (for whatever reason that may be). Then, to have the signals that ONCE were connected to MyQObject automagically re-connected simply by a single call.

Again, I want to disconnect ALL signals, without knowing the objects that are connected to the slot. Then, to call something that will automagically reconnect the signals back to the MyQObject slots that the other objects were once connected.

Any ideas? Thoughts on how to do this? Maybe something in QMetaObject i'm unaware of to do this?

One thought, was perhaps to suspend the QObject as if the QObject had no thread it belonged to and so wouldn't have the slots fire. But, I've not found a way to do this.

Thoughts? Ideas? Suggestions?

anda_skoa
19th August 2015, 10:59
I don't think there is any information inside the receiver object which signals are connected to it.

One thing you could do is not connect to the receiver directly but to a proxy, an object that has the same slots as the actual receiver and also signals with matching arguments.
The proxy's slots would just emit the signal again when its slots are activated.
The actual receiver would be connected to the proxy's signals.

Then you can simply block the forwarding by calling blockSignals() on the proxy.

Cheers,
_