Important: Please read the Qt Code of Conduct -

Disconnect all SLOTS in QObject connected from all other SIGNALS

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

  • Hi,

    why not simply add to MyQObject a private variable, say

    bool m_slotsEnabled{true};

    and the getter/setter functions and check in each slot if the value of m_slotsEnabled is true or false?
    For example:

        if (!m_slotsEnabled)

    Hope this helps


Log in to reply