Search code examples
c++qteventsqtcoreqobject

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?


Solution

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