Search code examples
qtinheritanceqt4qt5qevent

Is it necessary to call the event handler of the base class in subclasses?


Say I have QWidget::keyPressEvent(QKeyEvent *e) reimplemented in my subclass.

Is it necessary to call the base class' implementation at the end of it?

Example:

MyWidget::keyPressEvent(QKeyEvent *e)
{
    // my event handler...

    // now call parent event handler, necessary?
    QWidget::keyPressEvent(e);
}

If so, what's the point of doing that?


Solution

  • If you do not act on the event you should always pass the event to the base class' implementation as it may act on it, or there may be an event filter installed for it. The default implementation of QWidget for instance closes popup widgets if the user presses Esc. So, to be sure not to break any event handling, always pass on events to the base class, unless you act on them.