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. Proper way to handling application quit/close with multiple windows
Forum Updated to NodeBB v4.3 + New Features

Proper way to handling application quit/close with multiple windows

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.5k Views 2 Watching
  • 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.
  • B Offline
    B Offline
    btse
    wrote on 2 Apr 2019, 21:06 last edited by
    #1

    My application consists of one main/parent QMainWindow and then lots of QMainWindows that are spawned as children of the main QMainWindow.

    In the parent QMainWindow I've overriden the closeEvent() to prompt the user if they want to close the app.

    However, when quitting the app using CMD+Q or Quit from the menu, the other child QMainWindows will be randomly closed before the parent QMainWindow even gets the close event.

    I want to prompt the user before any of the QMainWindows get closed. What's the proper way to do this? Is overriding QApplication::event() and then prompting the user there the correct way, or am I overlooking something?

    J 1 Reply Last reply 3 Apr 2019, 04:37
    0
    • B btse
      2 Apr 2019, 21:06

      My application consists of one main/parent QMainWindow and then lots of QMainWindows that are spawned as children of the main QMainWindow.

      In the parent QMainWindow I've overriden the closeEvent() to prompt the user if they want to close the app.

      However, when quitting the app using CMD+Q or Quit from the menu, the other child QMainWindows will be randomly closed before the parent QMainWindow even gets the close event.

      I want to prompt the user before any of the QMainWindows get closed. What's the proper way to do this? Is overriding QApplication::event() and then prompting the user there the correct way, or am I overlooking something?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 3 Apr 2019, 04:37 last edited by
      #2

      @btse Why do you have more than one main window?

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

      B 1 Reply Last reply 3 Apr 2019, 13:49
      0
      • J jsulm
        3 Apr 2019, 04:37

        @btse Why do you have more than one main window?

        B Offline
        B Offline
        btse
        wrote on 3 Apr 2019, 13:49 last edited by
        #3

        @jsulm Because we use them as a top level window. Our app consists of many top level windows, and having things like the built in toolbar and statusbar is useful.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kent-Dorfman
          wrote on 4 Apr 2019, 07:04 last edited by
          #4

          the situation seems like the OP is relying too much opon event overrides and not using the signal/slot mechanism to handle the actions in the other windows.

          when CMD+Q is entered trigger a slot that exec() a "really quit?" dialog. If dialog return state is accepted then emit signal to all children windows telling them to close, and have parent window do nothing until it receive proper notification from all children windows that they have closed.

          B 1 Reply Last reply 4 Apr 2019, 16:06
          0
          • K Kent-Dorfman
            4 Apr 2019, 07:04

            the situation seems like the OP is relying too much opon event overrides and not using the signal/slot mechanism to handle the actions in the other windows.

            when CMD+Q is entered trigger a slot that exec() a "really quit?" dialog. If dialog return state is accepted then emit signal to all children windows telling them to close, and have parent window do nothing until it receive proper notification from all children windows that they have closed.

            B Offline
            B Offline
            btse
            wrote on 4 Apr 2019, 16:06 last edited by
            #5

            @Kent-Dorfman After digging into this more, things get a lot more tricky.

            I've tested adding my own QAction to the menuBar in order to override the default "Quit app" menu item/action which by default connects to the QCoreApplication::quit() slot. I connect this to my own slot where I prompt the user if they really want to quit. This works as expected when using Quit from the menu or "CMD+Q" shortcut.

            However, this does not account for the quitting from the dock. If I quit the app from the dock, then my "Do you want to quit?" dialog does not get shown at all.

            So it doesn't seem like there's a clean solution to handle a quit dialog for applications with multiple top level windows without adding some kind of native code.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on 4 Apr 2019, 18:40 last edited by
              #6

              In my apps, i'm using this code in a QApplication subclass:

              bool Application::event(QEvent *event)
              {
              bool handled=false;
              ...
              case QEvent::Close:
              	 if(!confirmQuit())
              		event->ignore();
              
              	handled=true;
              	break;
              

              It handles the Dock as well on Mac.

              B 1 Reply Last reply 4 Apr 2019, 19:11
              2
              • M mpergand
                4 Apr 2019, 18:40

                In my apps, i'm using this code in a QApplication subclass:

                bool Application::event(QEvent *event)
                {
                bool handled=false;
                ...
                case QEvent::Close:
                	 if(!confirmQuit())
                		event->ignore();
                
                	handled=true;
                	break;
                

                It handles the Dock as well on Mac.

                B Offline
                B Offline
                btse
                wrote on 4 Apr 2019, 19:11 last edited by
                #7

                @mpergand Thanks! This seems to be pretty close to what I want. Have you set setQuitOnLastWindowClosed(false) on your app also?

                M 1 Reply Last reply 4 Apr 2019, 19:25
                0
                • B btse
                  4 Apr 2019, 19:11

                  @mpergand Thanks! This seems to be pretty close to what I want. Have you set setQuitOnLastWindowClosed(false) on your app also?

                  M Offline
                  M Offline
                  mpergand
                  wrote on 4 Apr 2019, 19:25 last edited by mpergand 4 Apr 2019, 19:26
                  #8

                  @btse said in Proper way to handling application quit/close with multiple windows:

                  Have you set setQuitOnLastWindowClosed(false) on your app also?

                  Only for MacOS:

                  #if defined(Q_OS_MAC)
                  setQuitOnLastWindowClosed(false);
                  
                  B 1 Reply Last reply 4 Apr 2019, 19:34
                  2
                  • M mpergand
                    4 Apr 2019, 19:25

                    @btse said in Proper way to handling application quit/close with multiple windows:

                    Have you set setQuitOnLastWindowClosed(false) on your app also?

                    Only for MacOS:

                    #if defined(Q_OS_MAC)
                    setQuitOnLastWindowClosed(false);
                    
                    B Offline
                    B Offline
                    btse
                    wrote on 4 Apr 2019, 19:34 last edited by
                    #9

                    @mpergand Awesome! thank you very much

                    1 Reply Last reply
                    0

                    1/9

                    2 Apr 2019, 21:06

                    • Login

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