Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I still have to disconnect a lambda from a signal in Qt5.5?

Tags:

c++

lambda

qt

qt5.5

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).

  • Does it mean I have no longer to disconnect lambdas with Qt5.2 or later?
  • Do I have to pass that context or is that done automatically?
like image 975
Horst Walter Avatar asked Oct 26 '15 23:10

Horst Walter


Video Answer


1 Answers

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.

like image 125
ajshort Avatar answered Sep 20 '22 15:09

ajshort