QT 應用程式關閉某個視窗時,關閉開啟的所有其他視窗並退出程式
阿新 • • 發佈:2019-01-24
專案中當關閉主視窗時,需要將同時開啟的其他視窗關閉,並退出應用程式,實現方法如下:
在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
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.