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

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

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

[cpp]  view plain copy
  1. int main(int argc, char *argv[])  
  2. {  
  3.     QApplication a(argc, argv);  
  4.     a.connect( &a,  
  5.            SIGNAL(lastWindowClosed()),  
  6.            &a,  
  7.            SLOT(quit()));  
  8.     int ret = a.exec();  
  9.     return ret;  
  10. }  
     

具体可参考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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值