項目中當關閉主視窗時,需要將同時開啟的其他視窗關閉,並退出應用程式,實現方法如下:
在main函數中將QApplication::lastWindowClosed()訊號和QApplication::quit()槽函數相關聯,將主視窗的屬性設定為QWidget::setAttribute(WA_QuitOnClose,true);其他視窗該屬性設定為false。
[cpp] view plain copy 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.