Qt学习(七) QMessageBox弹出的对话框关闭后,整个程序退出的解决方法

3 篇文章 0 订阅

  这两天,将Qt的基础看了一下,决定尝试做个截图的小程序,稍后会单独写一篇文章,这里先将遇到的一个最大的问题以及解决方法写出来,以作备份之用。

    问题描述:使用QMessageBox弹出一个对话框后,点击这个对话框上的按钮,或是直接关闭这个对话框时,整个程序就退出了。

    具体情况:当在主窗体显示的时候,通过按钮弹出一个QMessageBox没问题,但是当主窗口隐藏的时候,通过热键呼出一个QMessageBox,当关闭这个QMessageBox后,整个程序就退出了。

    因为原来在VC下的MessageBox没有这个问题,我一直以为是我程序那里出错了,一遍遍的检查各个signal和slot,结果自然是什么都没查出来。今天早上重新调试,把QMessageBox的代码删了,发现问题竟然是在QMessageBox本身。

    查Assistant:

void QGuiApplication::lastWindowClosed() [signal]
This signal is emitted from exec() when the last visible primary window (i.e. window with no parent) is closed.

By default, QGuiApplication quits after this signal is emitted. This feature can be turned off by setting quitOnLastWindowClosed to false.

 

quitOnLastWindowClosed : bool
This property holds whether the application implicitly quits when the last window is closed.

The default is true.

If this property is true, the applications quits when the last visible primary window (i.e. window with no parent) is closed.

Access functions:

bool    quitOnLastWindowClosed()
void    setQuitOnLastWindowClosed(bool quit)
 

  解决方法:QMessageBox弹出前加一句:

QApplication::setQuitOnLastWindowClosed(false);

 

The Author : https://blog.csdn.net/wwkaven/article/details/37735329

 

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值