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. Children QWidgets always on top of parent
Qt 6.11 is out! See what's new in the release blog

Children QWidgets always on top of parent

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 5 Posters 11.6k Views 4 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.
  • T Tom.C Dev

    The main window holds the application data and the children widgets operate on these data. The lifetime of the application data and the lifetime of the children widgets are both handled by the main window. By doing this, it is not possible to have a widget pointing to the application data before it has been created or after it has been destroyed since both share the same lifetime.

    However, it seems the parent/child mechanism is responsible for the memory management AND the windows z-ordering.

    XardasX Offline
    XardasX Offline
    Xardas
    wrote on last edited by
    #8

    @Tom.C-Dev said:

    The main window holds the application data and the children widgets operate on these data. The lifetime of the application data and the lifetime of the children widgets are both handled by the main window.

    same problem.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #9

      So just delete the window when the data is destroyed. If you want the window to go away when mainwindow is destroyed you can link them like this:

      QWidget *w = new QWidget(); 
      w->show();
      connect(this, &QMainWindow::destroyed, w, &QWidget::deleteLater);
      
      XardasX 1 Reply Last reply
      2
      • XardasX Offline
        XardasX Offline
        Xardas
        wrote on last edited by
        #10

        Ok, thanks for reply.

        1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          So just delete the window when the data is destroyed. If you want the window to go away when mainwindow is destroyed you can link them like this:

          QWidget *w = new QWidget(); 
          w->show();
          connect(this, &QMainWindow::destroyed, w, &QWidget::deleteLater);
          
          XardasX Offline
          XardasX Offline
          Xardas
          wrote on last edited by
          #11

          @Chris-Kawa said:

          QWidget *w = new QWidget();
          w->show();
          connect(this, &QMainWindow::destroyed, w, &QWidget::deleteLater);

          doesn't work, after close main window widget stays.
          Any ideas why?

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #12

            doesn't work

            It works fine. Just not as you think it does.
            By default the application event loop exits when last top level window is closed ( see quitOnLastWindowClosed property).

            The main window is destroyed only after the event loop quits (as the main function scope ends). But the event loop does not quit since there's another top level window. You can force the main winodw to be destroyed when it closes, but then you can't put it on the stack.

            Here's a complete example:

            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
            
                QMainWindow* mainWindow = new QMainWindow();
                mainWindow->setAttribute(Qt::WA_DeleteOnClose);
            
                QWidget* otherWidget = new QWidget();
                QObject::connect(mainWindow, &QMainWindow::destroyed, otherWidget, &QWidget::deleteLater);
            
                mainWindow->show();
                otherWidget->show();
            
                return a.exec();
            }
            
            kshegunovK 1 Reply Last reply
            1
            • Chris KawaC Chris Kawa

              doesn't work

              It works fine. Just not as you think it does.
              By default the application event loop exits when last top level window is closed ( see quitOnLastWindowClosed property).

              The main window is destroyed only after the event loop quits (as the main function scope ends). But the event loop does not quit since there's another top level window. You can force the main winodw to be destroyed when it closes, but then you can't put it on the stack.

              Here's a complete example:

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  QMainWindow* mainWindow = new QMainWindow();
                  mainWindow->setAttribute(Qt::WA_DeleteOnClose);
              
                  QWidget* otherWidget = new QWidget();
                  QObject::connect(mainWindow, &QMainWindow::destroyed, otherWidget, &QWidget::deleteLater);
              
                  mainWindow->show();
                  otherWidget->show();
              
                  return a.exec();
              }
              
              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #13

              @Chris-Kawa said:

              The main window is destroyed only after the event loop quits (as the main function scope ends). But the event loop does not quit since there's another top level window. You can force the main winodw to be destroyed when it closes, but then you can't put it on the stack.

              Sorry for interjecting, couldn't one call the close slot on all the child widgets and still use the stack? I mean, instead of subscribing to the destroyed signal, possibly something like this?

              class Controller : public QObject
              {
                   Q_OBJECT
              
              signals:
                  void closing();
              
              protected:
                  virtual bool eventFilter(QObject * watched, QEvent * event)
                  {
                      if (event->type() == QEvent::Close)
                          emit closing();
                  }
              };
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  Controller ctrl;
              
                  QMainWindow mainWindow;
                  mainWindow.installEventFilter(&ctrl);
              
                  QWidget otherWidget;
                  QObject::connect(&ctrl, SIGNAL(closing()), &otherWidget, SLOT(close()));
              
                  mainWindow.show();
                  otherWidget.show();
              
                  return QApplication::exec();
              }
              

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #14

                @kshegunov Yup, would work too. Personally I try to trim classes when possible. Creating a specialized one when the same can be done in a one time 2 lines of connects/attribute settings seems too "Javish" to me :P
                You would also need a separate header/cpp for the Controller cause you can't put Q_OBJECT in a .cpp file. Not worth the effort IMO.

                But yeah, both solutions work fine and the choice is a matter of preference as to where to put responsibilities.

                kshegunovK 1 Reply Last reply
                1
                • Chris KawaC Chris Kawa

                  @kshegunov Yup, would work too. Personally I try to trim classes when possible. Creating a specialized one when the same can be done in a one time 2 lines of connects/attribute settings seems too "Javish" to me :P
                  You would also need a separate header/cpp for the Controller cause you can't put Q_OBJECT in a .cpp file. Not worth the effort IMO.

                  But yeah, both solutions work fine and the choice is a matter of preference as to where to put responsibilities.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #15

                  @Chris-Kawa said:

                  Creating a specialized one when the same can be done in a one time 2 lines of connects/attribute settings seems too "Javish" to me :P

                  Right, I agree in principle, however I'd make two points:

                  1. Usually people derive from QMainWindow and use it as a controller, then the whole creating a separate class will not be needed, as an override of closeEvent is going to be sufficient.
                  2. Ordinarily, I'd have a controller class to initialize my forms anyway (I tend not to derive from the widget classes), so in this case my proposal could also work.

                  In any case, I'm just putting it as an option. ;)

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1
                  • XardasX Offline
                    XardasX Offline
                    Xardas
                    wrote on last edited by
                    #16

                    Thanks for solutions, but seems "doesn't work" for me.
                    As I understand "Child" widgets always in main and create with QMainWindow, but i need something like:
                    MainWindow->button->clicked->tcpsocket(getSomeData)->MainWindow->readData->createWidgetWithSomeData.

                    kshegunovK 1 Reply Last reply
                    0
                    • XardasX Xardas

                      Thanks for solutions, but seems "doesn't work" for me.
                      As I understand "Child" widgets always in main and create with QMainWindow, but i need something like:
                      MainWindow->button->clicked->tcpsocket(getSomeData)->MainWindow->readData->createWidgetWithSomeData.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #17

                      @Xardas
                      Maybe there's a misunderstanding, no one said that creating the widgets in main() is the only way. You can create them anywhere in the code. I believe Chris' suggestion is your best bet. As an example:

                      class MainWindow : public QMainWindow
                      {
                          Q_OBJECT
                      
                          // ... some more code
                      
                      signals:
                          void closed();
                      
                      private slots:
                          void showDataWidget(QByteArray);
                      
                      protected:
                          virtual void closeEvent(QCloseEvent *);
                      };
                      
                      void MainWindow::showDataWidget(QByteArray data)
                      {
                          // Create the widget
                          QWidget * dataWidget = new QWidget(); //< NO PARENT!
                          dataWidget->setAttribute(Qt::WA_DeleteOnClose); //< Closing the window will free the memory
                      
                          // ... Fill up the widget's data ...
                          
                          QObject::connect(this, SIGNAL(closed()), dataWidget, SLOT(deleteLater()));    //< Connect the clean up routine
                      }
                      
                      void MainWindow::closeEvent(QCloseEvent * event)
                      {
                          emit closed();
                          QMainWindow::closeEvent(event);
                      }
                      

                      This should be easy to adapt to your case.

                      Kind regards.

                      Read and abide by the Qt Code of Conduct

                      XardasX 1 Reply Last reply
                      1
                      • kshegunovK kshegunov

                        @Xardas
                        Maybe there's a misunderstanding, no one said that creating the widgets in main() is the only way. You can create them anywhere in the code. I believe Chris' suggestion is your best bet. As an example:

                        class MainWindow : public QMainWindow
                        {
                            Q_OBJECT
                        
                            // ... some more code
                        
                        signals:
                            void closed();
                        
                        private slots:
                            void showDataWidget(QByteArray);
                        
                        protected:
                            virtual void closeEvent(QCloseEvent *);
                        };
                        
                        void MainWindow::showDataWidget(QByteArray data)
                        {
                            // Create the widget
                            QWidget * dataWidget = new QWidget(); //< NO PARENT!
                            dataWidget->setAttribute(Qt::WA_DeleteOnClose); //< Closing the window will free the memory
                        
                            // ... Fill up the widget's data ...
                            
                            QObject::connect(this, SIGNAL(closed()), dataWidget, SLOT(deleteLater()));    //< Connect the clean up routine
                        }
                        
                        void MainWindow::closeEvent(QCloseEvent * event)
                        {
                            emit closed();
                            QMainWindow::closeEvent(event);
                        }
                        

                        This should be easy to adapt to your case.

                        Kind regards.

                        XardasX Offline
                        XardasX Offline
                        Xardas
                        wrote on last edited by
                        #18

                        @kshegunov
                        Yes, misunderstanding. Thanks, work perfectly.

                        1 Reply Last reply
                        0

                        • Login

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