您的位置:首页 > 编程语言 > Qt开发

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

2013-09-05 13:57 162 查看
原文:/article/2675396.html#

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

在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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: