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. Question about re-implementing closeEvent(QCloseEvent*)
QtWS25 Last Chance

Question about re-implementing closeEvent(QCloseEvent*)

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 595 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.
  • W Offline
    W Offline
    walle19
    wrote on 11 Mar 2021, 13:40 last edited by
    #1

    Hi All,
    I have a QWidget as the main window of my application (not a QMainWindow).
    As you know this widget has a 'x' button on the top right corner that the user can press to exit the application.

    For my understanding pressing the 'x' button does not emit a signal therefor we have no ability to connect a SLOT to the event of the user pressing it.

    My question is:
    Can I implement the closeEvent() method as follows:

    void Base::closeEvent(QCloseEvent*) {
        if (QMessageBox::question(this,"shutdown",tr("are you sure to shutdown?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
        close();
    }
    }
    

    And prevent the closing of the widget / app - if the user presses 'NO' ?

    To be more precise I performed a check and pressing the 'x' button will show the popup but pressing 'NO' will not avoid the termination of the application.

    Any suggestions how to solve this ?
    Thanks

    J J 2 Replies Last reply 11 Mar 2021, 13:44
    0
    • W walle19
      11 Mar 2021, 13:40

      Hi All,
      I have a QWidget as the main window of my application (not a QMainWindow).
      As you know this widget has a 'x' button on the top right corner that the user can press to exit the application.

      For my understanding pressing the 'x' button does not emit a signal therefor we have no ability to connect a SLOT to the event of the user pressing it.

      My question is:
      Can I implement the closeEvent() method as follows:

      void Base::closeEvent(QCloseEvent*) {
          if (QMessageBox::question(this,"shutdown",tr("are you sure to shutdown?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
          close();
      }
      }
      

      And prevent the closing of the widget / app - if the user presses 'NO' ?

      To be more precise I performed a check and pressing the 'x' button will show the popup but pressing 'NO' will not avoid the termination of the application.

      Any suggestions how to solve this ?
      Thanks

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 11 Mar 2021, 13:44 last edited by jsulm 3 Nov 2021, 13:45
      #2

      @walle19 You should call accept() on the event object if you do not want it to be propagated further (which happens now, so the widget is closed).
      See https://doc.qt.io/qt-5/qwidget.html#closeEvent

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

      1 Reply Last reply
      2
      • W walle19
        11 Mar 2021, 13:40

        Hi All,
        I have a QWidget as the main window of my application (not a QMainWindow).
        As you know this widget has a 'x' button on the top right corner that the user can press to exit the application.

        For my understanding pressing the 'x' button does not emit a signal therefor we have no ability to connect a SLOT to the event of the user pressing it.

        My question is:
        Can I implement the closeEvent() method as follows:

        void Base::closeEvent(QCloseEvent*) {
            if (QMessageBox::question(this,"shutdown",tr("are you sure to shutdown?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
            close();
        }
        }
        

        And prevent the closing of the widget / app - if the user presses 'NO' ?

        To be more precise I performed a check and pressing the 'x' button will show the popup but pressing 'NO' will not avoid the termination of the application.

        Any suggestions how to solve this ?
        Thanks

        J Offline
        J Offline
        JonB
        wrote on 11 Mar 2021, 13:47 last edited by
        #3

        @walle19
        Further to @jsulm . I suggest you look at the following 2 patterns:

        • https://stackoverflow.com/questions/13129380/show-a-dialog-before-closing-the-program-in-qt

        • https://forum.qt.io/topic/76053/proper-ask-on-close-implementation

        The Accepted Solution on the first one (https://stackoverflow.com/a/38894383/489865) should be all you need.

        1 Reply Last reply
        1
        • W Offline
          W Offline
          walle19
          wrote on 11 Mar 2021, 14:42 last edited by
          #4

          Nice, It works Thanks!

          1 Reply Last reply
          0

          1/4

          11 Mar 2021, 13:40

          • Login

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