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

发布于 2024-10-08 00:35:54 字数 1114 浏览 0 评论 0原文

我正在尝试创建一个链中的多个窗口:窗口 1 是窗口 2 的父窗口,窗口 2 是窗口 3 的父窗口,等等。当我关闭一个窗口时,我希望它的所有子窗口也关闭。目前,如果我关闭顶层窗口,所有其他窗口都会按照希望关闭,但关闭窗口(例如窗口 2)只会关闭窗口 2,而不是窗口 3 等。我应该如何执行此操作?感谢您的帮助!

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

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

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

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

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!

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乞讨 2024-10-15 00:35:54

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

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

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

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.

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.

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.

默嘫て 2024-10-15 00:35:54

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

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.

弱骨蛰伏 2024-10-15 00:35:54

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

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

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

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

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:

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:

auto childList = findChildren<QMainWindow*>(QString(), Qt::FindDirectChildOnly);
for (auto child : childList)
{
    child->close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文