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. Qt: parent-child widgets. Does closing of a single child widget imply closing of other children (siblings)?
Forum Updated to NodeBB v4.3 + New Features

Qt: parent-child widgets. Does closing of a single child widget imply closing of other children (siblings)?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 2.6k Views 1 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
    BorysL
    wrote on 18 Apr 2019, 10:19 last edited by BorysL
    #1

    I have the following problem with the test Qt application, which contains one parent widget and two child widgets, which are separate windows. If the parent widget is hidden, then closing a single child widget implies closing the second child as well as closing the whole application.

    Is it the normal behavior of the parent/child widgets in Qt? Is there a way to keep the second child widget visible and the application running?

    #include <QApplication>
    #include <QtWidgets>
    
    class MyWidget : public QWidget {
    public:
        MyWidget(const QString& title = "", QWidget *parent = nullptr) :
            QWidget(parent) {
            setWindowTitle(title);
            setWindowFlags(windowFlags() | Qt::Window);
            setVisible(true);        
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MyWidget parent("Parent");
        MyWidget *child1 = new MyWidget("Child1", &parent);
        MyWidget *child2 = new MyWidget("Child2", &parent);
        QTimer::singleShot(5000, [&](){parent.hide();});
    
        return a.exec();
    }
    

    There're three widgets of simple class MyWidget: the parent one 'parent' with two childs 'child1' and 'child2'. After 5 sec the parent widget is hidden by the QTimer::singleShot. After that, if I close, e.g. child1 window, the second window child2 is also automatically closed and the application is finished.

    I'd expect that child1 and child2 are independent, and closing one of them shouldn't close another one.

    R 1 Reply Last reply 18 Apr 2019, 10:44
    0
    • B BorysL
      18 Apr 2019, 10:19

      I have the following problem with the test Qt application, which contains one parent widget and two child widgets, which are separate windows. If the parent widget is hidden, then closing a single child widget implies closing the second child as well as closing the whole application.

      Is it the normal behavior of the parent/child widgets in Qt? Is there a way to keep the second child widget visible and the application running?

      #include <QApplication>
      #include <QtWidgets>
      
      class MyWidget : public QWidget {
      public:
          MyWidget(const QString& title = "", QWidget *parent = nullptr) :
              QWidget(parent) {
              setWindowTitle(title);
              setWindowFlags(windowFlags() | Qt::Window);
              setVisible(true);        
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MyWidget parent("Parent");
          MyWidget *child1 = new MyWidget("Child1", &parent);
          MyWidget *child2 = new MyWidget("Child2", &parent);
          QTimer::singleShot(5000, [&](){parent.hide();});
      
          return a.exec();
      }
      

      There're three widgets of simple class MyWidget: the parent one 'parent' with two childs 'child1' and 'child2'. After 5 sec the parent widget is hidden by the QTimer::singleShot. After that, if I close, e.g. child1 window, the second window child2 is also automatically closed and the application is finished.

      I'd expect that child1 and child2 are independent, and closing one of them shouldn't close another one.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 18 Apr 2019, 10:44 last edited by raven-worx
      #2

      @BorysL
      why creating a parent-child relationship then in the first place?
      If it is just for automatic deletion upon closing you can set set Qt::WA_DeleteOnClose widget attribute, or hold a pointer to the child widget and delete the child widget manually, but don't add it to the parent-child hierarchy.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      B 1 Reply Last reply 18 Apr 2019, 11:36
      0
      • R raven-worx
        18 Apr 2019, 10:44

        @BorysL
        why creating a parent-child relationship then in the first place?
        If it is just for automatic deletion upon closing you can set set Qt::WA_DeleteOnClose widget attribute, or hold a pointer to the child widget and delete the child widget manually, but don't add it to the parent-child hierarchy.

        B Offline
        B Offline
        BorysL
        wrote on 18 Apr 2019, 11:36 last edited by
        #3

        @raven-worx Actually, because the parent-child relationship theoretically :)) should be simpler. It's not necessary to worry about the memory leaks, about delete vs deleteLater :) things.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JonB
          wrote on 20 Apr 2019, 10:01 last edited by
          #4

          I'm about to investigate something akin to this in my code next week.

          I'm surprised that that hiding the parent causes the application to close (and only do so when one child is closed while the other is still open). Are you sure about this? Could any Qt expert explain why the application would be exited in this circumstance, because I cannot see why that would be?

          R 1 Reply Last reply 20 Apr 2019, 10:05
          0
          • J JonB
            20 Apr 2019, 10:01

            I'm about to investigate something akin to this in my code next week.

            I'm surprised that that hiding the parent causes the application to close (and only do so when one child is closed while the other is still open). Are you sure about this? Could any Qt expert explain why the application would be exited in this circumstance, because I cannot see why that would be?

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 20 Apr 2019, 10:05 last edited by raven-worx
            #5

            @JonB said in Qt: parent-child widgets. Does closing of a single child widget imply closing of other children (siblings)?:

            I'm surprised that that hiding the parent causes the application to close (and only do so when one child is closed while the other is still open). Are you sure about this?

            the question was that the parent got closed after some time which also closes the children. When all windows are closed the application quits.
            If you do not want this behavior, set the QGuiApplication::quitOnLastWindowClosed property accordingly.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            J 2 Replies Last reply 20 Apr 2019, 10:10
            2
            • R raven-worx
              20 Apr 2019, 10:05

              @JonB said in Qt: parent-child widgets. Does closing of a single child widget imply closing of other children (siblings)?:

              I'm surprised that that hiding the parent causes the application to close (and only do so when one child is closed while the other is still open). Are you sure about this?

              the question was that the parent got closed after some time which also closes the children. When all windows are closed the application quits.
              If you do not want this behavior, set the QGuiApplication::quitOnLastWindowClosed property accordingly.

              J Offline
              J Offline
              JonB
              wrote on 20 Apr 2019, 10:10 last edited by JonB
              #6

              @raven-worx
              QTimer::singleShot(5000, [&](){parent.hide();});

              The parent does not get close()d, it gets hide()den. Looking at the docs I'm not clear how hide is related to close, could you explain?

              [Background: in someone else's code I have to return to next work, he is hide()ing parent, not close()ing. I'm seeing different behaviour under my Linux vs his Windows (which I don't have) in just this situation (parent hiding, not closing). So I'd like to understand, I may have to raise my own query on this. Thanks.]

              1 Reply Last reply
              0
              • R raven-worx
                20 Apr 2019, 10:05

                @JonB said in Qt: parent-child widgets. Does closing of a single child widget imply closing of other children (siblings)?:

                I'm surprised that that hiding the parent causes the application to close (and only do so when one child is closed while the other is still open). Are you sure about this?

                the question was that the parent got closed after some time which also closes the children. When all windows are closed the application quits.
                If you do not want this behavior, set the QGuiApplication::quitOnLastWindowClosed property accordingly.

                J Offline
                J Offline
                JonB
                wrote on 20 Apr 2019, 10:14 last edited by JonB
                #7

                @raven-worx
                In a distinct issue (separate post by me here, as I'd like it if you kindly answered my post above separately), your answer does not address the behaviour the user claims. You say

                parent got closed after some time which also closes the children

                but the OP reports that after parent closure the app does not exit. He says:

                After 5 sec the parent widget is hidden by the QTimer::singleShot. After that [my italics], if I close, e.g. child1 window, the second window child2 is also automatically closed and the application is finished

                so app having two separate open child widgets only exits when one of them is later closed --- what's going on here?

                R 1 Reply Last reply 20 Apr 2019, 21:46
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 20 Apr 2019, 11:17 last edited by
                  #8

                  hide() doesn't trigger the check if the last window was closed - only close (as the wording suggests).
                  When you the close the child, the close check is triggered and since there is no toplevel widget the application quits.

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

                  J 1 Reply Last reply 21 Apr 2019, 07:58
                  1
                  • J JonB
                    20 Apr 2019, 10:14

                    @raven-worx
                    In a distinct issue (separate post by me here, as I'd like it if you kindly answered my post above separately), your answer does not address the behaviour the user claims. You say

                    parent got closed after some time which also closes the children

                    but the OP reports that after parent closure the app does not exit. He says:

                    After 5 sec the parent widget is hidden by the QTimer::singleShot. After that [my italics], if I close, e.g. child1 window, the second window child2 is also automatically closed and the application is finished

                    so app having two separate open child widgets only exits when one of them is later closed --- what's going on here?

                    R Offline
                    R Offline
                    raven-worx
                    Moderators
                    wrote on 20 Apr 2019, 21:46 last edited by
                    #9

                    @JonB said in Qt: parent-child widgets. Does closing of a single child widget imply closing of other children (siblings)?:

                    so app having two separate open child widgets only exits when one of them is later closed --- what's going on here?

                    hard to say, my guess is you implicitly/unintentionally connected them somehow

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    J 1 Reply Last reply 21 Apr 2019, 08:00
                    0
                    • C Christian Ehrlicher
                      20 Apr 2019, 11:17

                      hide() doesn't trigger the check if the last window was closed - only close (as the wording suggests).
                      When you the close the child, the close check is triggered and since there is no toplevel widget the application quits.

                      J Offline
                      J Offline
                      JonB
                      wrote on 21 Apr 2019, 07:58 last edited by JonB
                      #10

                      @Christian-Ehrlicher

                      and since there is no toplevel widget the application quits

                      But there is a top-level widget (OP's MyWidget parent). It just happens to have been subjected to hide(), but not to close(). Does your statement mean there is no visible top-level widget so the app closes? I thought the windows system (e.g. Windows) has a distinction between hiding vs closing a window (e.g. IIRC ShowWindow(hWnd, SW_HIDE) is not the same thing as CloseWindow(hWnd))?

                      1 Reply Last reply
                      0
                      • R raven-worx
                        20 Apr 2019, 21:46

                        @JonB said in Qt: parent-child widgets. Does closing of a single child widget imply closing of other children (siblings)?:

                        so app having two separate open child widgets only exits when one of them is later closed --- what's going on here?

                        hard to say, my guess is you implicitly/unintentionally connected them somehow

                        J Offline
                        J Offline
                        JonB
                        wrote on 21 Apr 2019, 08:00 last edited by
                        #11

                        @raven-worx

                        my guess is you implicitly/unintentionally connected them somehow

                        I can only say that I took the OP's code shown as complete code, hence his question, there are no connections.

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 21 Apr 2019, 08:21 last edited by
                          #12

                          @JonB said in Qt: parent-child widgets. Does closing of a single child widget imply closing of other children (siblings)?:

                          visible top-level widget

                          Yes - that's the check - if no tlw is visible setQuitOnLastWindowClsoed jumps in.

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

                          J 1 Reply Last reply 21 Apr 2019, 08:23
                          2
                          • C Christian Ehrlicher
                            21 Apr 2019, 08:21

                            @JonB said in Qt: parent-child widgets. Does closing of a single child widget imply closing of other children (siblings)?:

                            visible top-level widget

                            Yes - that's the check - if no tlw is visible setQuitOnLastWindowClsoed jumps in.

                            J Offline
                            J Offline
                            JonB
                            wrote on 21 Apr 2019, 08:23 last edited by
                            #13

                            @Christian-Ehrlicher
                            Thanks, Christian. OK then there is still some conflabration over "hiding" vs "closing". I thought they were distinct concepts, it seems they are being treated as the same sort of thing. Maybe my understanding is wrong and they are the same thing.

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 21 Apr 2019, 08:33 last edited by
                              #14

                              close() does a little bit more than hide() - first you can intercept it in closeEvent(), than it deletes the window when Qt::WA_DeleteOnClose is set and afterwards it checks for deleteOnClose: https://doc.qt.io/qt-5/qwidget.html#close

                              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
                              0

                              8/14

                              20 Apr 2019, 11:17

                              • Login

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