Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How does a QCloseEvent propagate?
Forum Updated to NodeBB v4.3 + New Features

How does a QCloseEvent propagate?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 726 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • gghavocG Offline
    gghavocG Offline
    gghavoc
    wrote on last edited by
    #1

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

    void MainWindow::closeEvent(QCloseEvent *event)
    {
        if (this->okToContinue()) {
            this->writeSettings();
            //event->accept();
            //QMainWindow::closeEvent(event);
        } else {
            event->ignore();
        }
    }
    

    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?

    jsulmJ 1 Reply Last reply
    0
    • gghavocG gghavoc

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

      void MainWindow::closeEvent(QCloseEvent *event)
      {
          if (this->okToContinue()) {
              this->writeSettings();
              //event->accept();
              //QMainWindow::closeEvent(event);
          } else {
              event->ignore();
          }
      }
      

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @gghavoc See https://doc.qt.io/qt-5/qcloseevent.html
      "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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • gghavocG Offline
        gghavocG Offline
        gghavoc
        wrote on last edited by
        #3

        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?

        jsulmJ 1 Reply Last reply
        0
        • gghavocG gghavoc

          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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          gghavocG 1 Reply Last reply
          0
          • jsulmJ jsulm

            @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.

            gghavocG Offline
            gghavocG Offline
            gghavoc
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              gghavocG 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

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

                gghavocG Offline
                gghavocG Offline
                gghavoc
                wrote on last edited by gghavoc
                #7

                @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?).

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved