Qt World Summit: Submit your Presentation

installEventFilter a bit differently

  • Hi, I was wondering how to process multiple events of one QObject without a need to subclass it and override some method. I know, there are generally 5 ways how to process events (described here), but in each one, I need to subclass either an object whose messages I want to process, or the object who is going to filter the events before they get to the receiver (eventFilter). Why is there no method like QObject::installEventFilter which takes a functor/function pointer (taking a pointer to a QEvent and returning bool)? Are there any problems that would arise from this type of event-handling? Thanks in advance.

    I even add the code, so that there's no confusion

    // this would be added into the QObject class
    class QObject
        void installEventFilter(std::function<bool(QEvent*)> functor);
    // -------------------------------
    // USER'S CODE
    // this can be some global or static member function - doesn't really matter
    bool MyEventFilteringFunction(QEvent* ev)
        if (ev->type() == QEvent::Paint)
            // do something
            return true;
        return false;
    // somewhere in the code
    // no need to subclass and override button, nor it's parent dialog or anything
    QPushButton b;

  • Lifetime Qt Champion



    • usually because nobody requested nor provided that feature
    • not feasible for technical reasons at that time
    • not possible to integrate it for some reasons

    All in all, you can open a feature request for that to get the discutions started. Even better if you can submit a patch that implements what you are asking for.

  • Thanks for your answer. All I'm asking is - would this be a sane thing to implement, or am I missing something obvious? If the rest of the event-system allowed such functionality, would it be able to e.g. replace the original installEventFilter method? Obviously, my version would need to have 1 additional parameter besides QEvent - that would be QObject representing the receiver of the event. I don't have any specific requirement, this is just an off-topic, I guess.

Log in to reply