automatic call to removeEventFilter()

I want to handle events for an object (a) by another object (b). I call a->installEventFilter(b). If I delete object a, the documentation states that the event filter is automatically removed, i.e. object b receives no more events to filter for a. But I do not know what happens if I delete object b first, while a is kept alive. The documentation, if I understand it correctly, says nothing about this situation. Will it cause crashes because object a will send events to b, which is already dead? Or is it a safe thing to do because the event filtering is somehow 'automagically' removed? Or do I have to call a->removeEventFilter(b) explicitly, for example in the destructor of b?

Upvotes: 3

Views: 409

Answers (1)

bartop
bartop

Reputation: 10315

I took a look at Qt source code and here is what I found in QObject:

QList<QPointer<QObject> > eventFilters;

QPointers are smart pointers that are turned to null when pointed to object is deleted. So to the point - you do not have to call a->removeEventFilter(b) explicitly. The Qt manages this under the hood.

EDIT: There is one assumption in this answer - that Qt won't try to filter using null object (even though it knows for sure it was deleted) because I did not find source or documentation for this.

Upvotes: 3

Related Questions