MarcinG
MarcinG

Reputation: 840

Is there a way to uninstall eventfilter in qt?

I need event filter only for some time, is there a way of uninstalling it later on?

Upvotes: 7

Views: 5410

Answers (3)

László Papp
László Papp

Reputation: 53165

Please read about how the event system works in Qt here. This is crucial for the basic understanding, particularly this paragraph:

The QObject::installEventFilter() function enables this by setting up an event filter, causing a nominated filter object to receive the events for a target object in its QObject::eventFilter() function. An event filter gets to process events before the target object does, allowing it to inspect and discard the events as required. An existing event filter can be removed using the QObject::removeEventFilter() function.

Having that read, you can see that there is a counter-part for installEventFilter, not surprisingly, it is called removeEventFilter. Here is the Qt 5 documentation to it:

void QObject::removeEventFilter(QObject * obj)

Removes an event filter object obj from this object. The request is ignored if such an event filter has not been installed.

All event filters for this object are automatically removed when this object is destroyed.

It is always safe to remove an event filter, even during event filter activation (i.e. from the eventFilter() function).

Upvotes: 8

Tim Meyer
Tim Meyer

Reputation: 12600

Yes there is. It's a function called QObject::removeEventFilter.

Upvotes: 5

TWE
TWE

Reputation: 421

From Qt Docu:

void QObject::removeEventFilter ( QObject * obj )

Removes an event filter object obj from this object. The request is ignored if such an event filter has not been installed.

All event filters for this object are automatically removed when this object is destroyed.

It is always safe to remove an event filter, even during event filter activation (i.e. from the eventFilter() function).

Upvotes: 4

Related Questions