windows Qt:父/子链中的多个窗口,父不关闭子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4456252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:53:34  来源:igfitidea点击:

Qt: Multiple windows in a parent/child chain, parent does not close children?

windowsqtparent

提问by ishmael

I am trying to create multiple windows in a chain: window 1 is the parent of window 2, window 2 is the parent of window 3, etc. When I close one window, I would like all its children to close as well. Currently, if I close the top level window, all others close, as hoped, but closing, for example, window 2, only closes window 2, not window 3, etc. How should I be doing this? Thanks for your help!

我试图在链中创建多个窗口:窗口 1 是窗口 2 的父窗口,窗口 2 是窗口 3 的父窗口,等等。当我关闭一个窗口时,我希望它的所有子窗口也关闭。目前,如果我关闭顶层窗口,所有其他窗口都会按预期关闭,但是关闭,例如,窗口 2,只关闭窗口 2,而不是窗口 3,等等。我应该怎么做?谢谢你的帮助!

main_window.cpp

main_window.cpp

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    QPushButton* button = new QPushButton("Open 1", this);
    connect(button, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
}

void MainWindow::on_button_clicked() {
    window1 *w = new window1(this);
    w->show();
}

window1.cpp

窗口1.cpp

window1::window1(QWidget *parent) : QWidget(parent)
{
    this->setWindowFlags(Qt::Window); // in order to have a free-standing window

    QPushButton* button = new QPushButton("Open 2", this);
    connect(button, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
}

void window1::on_button_clicked() {
    window2 *w = new window2(this);
    w->show();
}

window2.cpp

窗口2.cpp

window2::window2(QWidget *parent) : QWidget(parent)
{
    this->setWindowFlags(Qt::Window);

    QLabel* label = new QLabel("Window 2", this);
}

采纳答案by Leiaz

By default QApplication quits when the last primary window (window with no parent) is closed (see QApplication::lastWindowClosed signal), that is why closing your MainWindow closes everything.

默认情况下,QApplication 在最后一个主窗口(没有父窗口的窗口)关闭时退出(请参阅QApplication::lastWindowClosed 信号),这就是关闭 MainWindow 会关闭所有内容的原因。

Closing a widget doesn't delete it, unless the attribute Qt::WA_DeleteOnClose is set (see QWidget::close()). If you just want your windows to be closed, I think you have to reimplement closeEvent() to call close() on the children.

关闭小部件不会删除它,除非设置了属性 Qt::WA_DeleteOnClose (请参阅QWidget::close())。如果您只想关闭您的窗户,我认为您必须重新实现 closeEvent() 以在孩子上调用 close() 。

But if you want to delete them when closed, then set the attribute Qt::WA_DeleteOnClose. The children are automatically deleted when the parent is deleted.

但是如果你想在关闭时删除它们,那么设置属性 Qt::WA_DeleteOnClose。删除父项时,子项会自动删除。

回答by Septagram

You can overload closeEvent()in every widget that's supposed to have children. Then, either keep a list of your widgets to close in closeEvent(), or just call there deleteLater, which would delete both widget in question and its children.

您可以在每个应该有孩子的小部件中重载closeEvent()。然后,要么保留要在closeEvent() 中关闭的小部件列表,要么只调用 deleteLater,这将删除有问题的小部件及其子部件。

回答by IceFire

Leiaz has already pointed out why the child-mainWindows' closeEvent(.) is not called. If you need to overload closeEvent(.) of the parent mainWindow to call closeEvent at each child because you do something in there (like storing window settings), you can insert this snippet:

Leiaz 已经指出为什么不调用 child-mainWindows 的 closeEvent(.)。如果您需要重载父 mainWindow 的 closeEvent(.) 以在每个子窗口调用 closeEvent 因为您在其中执行某些操作(例如存储窗口设置),则可以插入以下代码段:

auto childList = findChildren<QMainWindow*>();
for (auto child : childList)
{
    child->close();
}

Note that the children's QMainWindow children will be called, as well, so there is no need to overload the child-mainWindows' closeEvent, too. In case that you want to only close QMainWindows that are direct children, use:

请注意,子级的 QMainWindow 子级也将被调用,因此也无需重载子级主窗口的 closeEvent。如果您只想关闭直接子级的 QMainWindows,请使用:

auto childList = findChildren<QMainWindow*>(QString(), Qt::FindDirectChildOnly);
for (auto child : childList)
{
    child->close();
}