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. The main window is hidden and the child window is not hidden
Qt 6.11 is out! See what's new in the release blog

The main window is hidden and the child window is not hidden

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 5.7k 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.
  • Qt_crazyerQ Offline
    Qt_crazyerQ Offline
    Qt_crazyer
    wrote on last edited by
    #1

    I have a main window, in which there is a button, one is to show my child window. When the main window is hidden, the child window should be hidden, too. But it doesn't. Some of my code is here:

    //main window
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        showChild = new QPushButton("Show Child");
    
        resize(showChild->sizeHint());
        setCentralWidget(showChild);
    
        connect(showChild, &QPushButton::clicked, [=](){
                                                        childWindow* m_child = new childWindow(this);
                                                        m_child->show();
                                                        QTimer::singleShot(2000, this, SLOT(hide()));
                                                       });
    }
    
    //child window
    childWindow::childWindow(QWidget *parent) : QWidget(parent)
    {
        setWindowFlags(Qt::Window);
        setWindowModality(Qt::WindowModal);
    }
    

    Qt Creator 5.9.1 on windows10
    MSVC2015 64bit compiler

    Thanks for any reply.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      I think (and could be wrong here) that it is because the child windows are modal.

      You can trying to force hide them when you hide mainwindow just iterate through child windows and call hide() on them as well.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ambershark
        wrote on last edited by ambershark
        #3

        Ok I tested it and I lied. It has nothing to do with modality. It seems like it is by design and children widgets that are their own windows do not get auto hidden with a call to hide.

        So you will need to hide all windows yourself. You can do this easily by iterating through this->children() and finding all the childWindows.

        Edit: a little more info... The test I used a QDialog, which it seems is treated a bit differently. Here is an excerpt from the docs. Is your childWindow a dialog as well?

        Note that QDialog (and any other widget that has type Qt::Dialog ) uses the parent widget slightly differently from other classes in Qt. A dialog is always a top- level widget, but if it has a parent, its default location is centered on top of the parent's top-level widget (if it is not top-level itself). It will also share the parent's taskbar

        Final edit (lol): Confirmed it is because you are calling setWindowFlags(Qt::Window) .. that forces Qt to consider it top level but with a parent for clean up reasons. This is why hide() does not hide it as well. Mystery solved! :) So you will just have to hide any top level items yourself including anything you set as a window.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        Qt_crazyerQ 1 Reply Last reply
        2
        • A ambershark

          Ok I tested it and I lied. It has nothing to do with modality. It seems like it is by design and children widgets that are their own windows do not get auto hidden with a call to hide.

          So you will need to hide all windows yourself. You can do this easily by iterating through this->children() and finding all the childWindows.

          Edit: a little more info... The test I used a QDialog, which it seems is treated a bit differently. Here is an excerpt from the docs. Is your childWindow a dialog as well?

          Note that QDialog (and any other widget that has type Qt::Dialog ) uses the parent widget slightly differently from other classes in Qt. A dialog is always a top- level widget, but if it has a parent, its default location is centered on top of the parent's top-level widget (if it is not top-level itself). It will also share the parent's taskbar

          Final edit (lol): Confirmed it is because you are calling setWindowFlags(Qt::Window) .. that forces Qt to consider it top level but with a parent for clean up reasons. This is why hide() does not hide it as well. Mystery solved! :) So you will just have to hide any top level items yourself including anything you set as a window.

          Qt_crazyerQ Offline
          Qt_crazyerQ Offline
          Qt_crazyer
          wrote on last edited by
          #4

          @ambershark
          Well. The childWindow is a widget, not dialog.
          I have tried to hide any top level items by myself. It's ok. It's a bit of trouble, though.
          I use the new slot function hideAll(), instead of the hide();

          void MainWindow::hideAll()
          {
          
              QList<QWidget *> allPWidgets = findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly);
              for(int i = 0; i < allPWidgets.length(); i++)
                  allPWidgets.at(i)->hide();
          
              hide();
          }
          

          Thank you for your reply.

          J.HilkJ 1 Reply Last reply
          0
          • Qt_crazyerQ Qt_crazyer

            @ambershark
            Well. The childWindow is a widget, not dialog.
            I have tried to hide any top level items by myself. It's ok. It's a bit of trouble, though.
            I use the new slot function hideAll(), instead of the hide();

            void MainWindow::hideAll()
            {
            
                QList<QWidget *> allPWidgets = findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly);
                for(int i = 0; i < allPWidgets.length(); i++)
                    allPWidgets.at(i)->hide();
            
                hide();
            }
            

            Thank you for your reply.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Qt_crazyer said in The main window is hidden and the child window is not hidden:

            @ambershark
            Well. The childWindow is a widget, not dialog.
            I have tried to hide any top level items by myself. It's ok. It's a bit of trouble, though.
            I use the new slot function hideAll(), instead of the hide();

            void MainWindow::hideAll()
            {
            
                QList<QWidget *> allPWidgets = findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly);
                for(int i = 0; i < allPWidgets.length(); i++)
                    allPWidgets.at(i)->hide();
            
                hide();
            }
            

            Thank you for your reply.

            uh
            You're using a sledgehammer where a screw driver would do the job better.
            For example, you can put lambdas into lambdas ;-)

            connect(showChild, &QPushButton::clicked, [=](){
                             childWindow* m_child = new childWindow(this);
                             m_child->show();
                             QTimer::singleShot(2000, this, [=]{
                                     this-> hide();
                                     showChild-> hide();
                             });
            });
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            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