In early Qt 5 versions I have to disconnect lambdas from signals as shown here: "Disconnecting lambda functions in Qt5".
Here I found the following statement: There is no automatic disconnection when the 'receiver' is destroyed because it's a functor with no QObject. However, since 5.2 there is an overload which adds a "context object". When that object is destroyed, the connection is broken (the context is also used for the thread affinity: the lambda will be called in the thread of the event loop of the object used as context).
Qt automatically removes all connections to or from an object when it is destroyed through QObject::~QObject(). So if you create a connection to a lambda, when the sending object is deleted, the connection is automatically cleaned up. You do not, and have not previously needed to, disconnect it yourself.
The context object that you are referring to is used when you require more fine grained control over the lifetime of the connection. This is used when you want the connection to be removed when another object is destroyed (the context object). This makes it easier to remove the connection if you need to do so before the sender is destroyed.
In summary: You do not need to manually disconnect lambdas, they are cleaned up automatically. You can use context objects to remove the connection before the sender object is destroyed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With