It depends on the type of the signal-slot connection. Connections are direct by default if the sender and the receiver belong to the same thread. In this case the slot will be called immediately when you emit the signal. When the signal function is finished, you can be sure that the slot has also finished.
When the sender and the receiver belong to different threads, the connection is queued by default. In this case your code is not safe. The slot can be called much later than the signal was called. Even deleteLater
will not save the situation because it's processed by the sender's thread's event loop and doesn't depend on the other thread's event loop.
So if you want to write such code, ensure that your objects are in the same thread. You can pass Qt::DirectConnection
option to connect
function to make connection type more clear.
If you want to use queued connection, you can emit e.g. aboutToBeDeleted
signal in the sender. The receiver will receive that signal, process it somehow and respond with cleanUpCompleted
signal that will trigger the actual object deletion.
Also consider using standard QObject::destroyed
signal. It's called immediately before the object is destroyed but can be useful in many cases.