c++qtqt4signals-slots

Proper way to disconnect signals from two QObjects not being destroyed/deleted


What is the proper way to disconnect two QObject's from each other such that no more signals emitted from one object trigger slots in the other object? Neither object is going to be destroyed so disconnect() will not be called in QObject::~QObject(). I see two versions of the disconnect() method in the documentation, and neither one takes a pointer to two objects.

It is important that no more signals are received into the receiving objects slots after disconnect. A few triggers while the buffer clears unprocessed signals is OK, but the signals must stop within a very short time.

There seems to be some confusion about the proper way to do this.


Solution

  • From the Qt docs:

    [To] Disconnect a specific receiver:

    disconnect(myObject, 0, myReceiver, 0);
    

    Once this has been called, all connections from myObject's signals to myReceiver's slots will be disconnected.