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



  • 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.


  • Moderators

    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.


  • Moderators

    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.



  • @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.



  • @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();
                     });
    });
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.