在一个PC端的应用中,经常会出现关闭主窗体,进而需要把所有打开的窗体都随着关闭,结束程序的情况,那么具体怎么实现呢?
1、在main函数中将QApplication::lastWindowClosed()信号和QApplication::quit()槽函数相关联:
a.connect(&a,SIGNAL(lastWindowClosed()),&a,SLOT(quit()));
2、将主窗口的属性设置为QWidget::setAttribute(WA_QuitOnClose,true);其他窗口该属性设置为false:
注意:在初始化窗口时就要设置,不是在窗口的构造函数里设置
Princekin w;
w.setWindowFlags(Qt::WindowCloseButtonHint|Qt::WindowMinimizeButtonHint|Qt::FramelessWindowHint);
w.setAttribute(Qt::WA_QuitOnClose,true);
w.show();
具体可参考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.