Important: Please read the Qt Code of Conduct -

How does a QCloseEvent propagate?

  • I have a MainWindow derived from QMainWindow and I re-implemented the closeEvent() handler.

    void MainWindow::closeEvent(QCloseEvent *event)
        if (this->okToContinue()) {
        } else {

    I've commented out the QMainWindow::closeEvent() to test the app will exit without the event propagating to the base implementation. Weirdly enough, it does exit.

    Other event handlers such as keyPressEvent() don't work properly without the base implementation in their overrides but the closeEvent() does work without the base implementation.

    What makes this happen? Does it propagate to other widgets even after it was already handled?

  • Lifetime Qt Champion

    @gghavoc See
    "If you do not want your widget to be hidden, or want some special handling, you should reimplement the event handler and ignore() the event."
    Since you did not call ignore() on the event your window is closed.

  • Sure, but what I wanted to know is how come an override of keyPressEvent() needs a base implementation to work properly while an override of closeEvent() doesn't require the base implementation to work? Does QCloseEvent propagate further event if it was handled already?

  • Lifetime Qt Champion

    @gghavoc I don't have exact answer. I guess close event works differently than other events. You can take a look at Qt source code to get more insight.

  • @jsulm That's my last resort. Thanks for answering, though.

  • Lifetime Qt Champion

    CloseEvent event is just a notification event similar to showEvent - it just informs the widget what will happen. a keyPressEvent also informs the widget that a key was pressed, then the widget is doing something with this information (e.g. displaying the text).

  • @Christian-Ehrlicher So if I don't invoke QMainWindow::closeEvent in the subclass MainWindow::closeEvent, that would mean that QMainWindow wouldn't get notified and the application will not close, right? Unless maybe closing the application gets handled somewhere else (maybe QApplication?).

  • Lifetime Qt Champion

    It's a notification that the window will be closed, the window is not responsible to close itself - it just gets informed (as I already wrote) about this circumstance and is allowed to reject it.

Log in to reply