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

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

发布了245 篇原创文章 · 获赞 57 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/qq_16542775/article/details/103242957