ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

qt 窗体隐藏时,点击messageBox直接导致程序退出

2021-03-10 10:01:02  阅读:260  来源: 互联网

标签:messageBox qt 对话框 signal window 窗体 QMessageBox quitOnLastWindowClosed closed


这两天,将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);

标签:messageBox,qt,对话框,signal,window,窗体,QMessageBox,quitOnLastWindowClosed,closed
来源: https://blog.csdn.net/c1149884598/article/details/114626687

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有