QT 应用程序关闭某个窗口时,关闭打开的所有其他窗口并退出程序 【转】

原文:http://blog.csdn.net/yangyunfeizj/article/details/7398023#

项目中当关闭主窗口时,需要将同时打开的其他窗口关闭,并退出应用程序,实现方法如下:

在main函数中将QApplication::lastWindowClosed()信号和QApplication::quit()槽函数相关联,将主窗口的属性设置为QWidget::setAttribute(WA_QuitOnClose,true);其他窗口该属性设置为false。

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.connect( &a,
           SIGNAL(lastWindowClosed()),
           &a,
           SLOT(quit()));
    int ret = a.exec();
    return ret;
}

具体可参考qt助手中的解释:

void QApplication::lastWindowClosed () [signal]

This signal is emitted from QApplication::exec() when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed.

By default,

  • this attribute is set for all widgets except transient windows such as splash screens, tool windows, and popup menus
  • QApplication implicitly quits when this signal is emitted.

This feature can be turned off by setting quitOnLastWindowClosed to false.

原文地址:https://www.cnblogs.com/Leo-Forest/p/3303174.html