Qt5.5 为什么不显示错误信息?
Qt5.5 why isn't error message displayed?
我用的是Qt5.5,写过一个应用程序,如果在应用程序启动过程中遇到错误,我想用QErrorMessage来显示。
我添加了:
QErrorMessage errmsg(this);
errmsg.showMessage("HELLO WORLD");
此代码已插入到我的主要 window 构造函数的末尾,但没有显示任何内容,为什么?
在文档中您可以阅读:
Shows the given message, message, and returns immediately. If the user
has requested for the message not to be shown again, this function
does nothing.
这意味着,一旦 errmsg
被销毁,将无法再显示任何消息。您必须使用 QErrorMessage
class 作为 member/global 变量:
this->errmsg = new QErrorMessage(this);
this->errmgs->showMessage("HELLO WORLD");
或者,您可以使用 QErrorMessage::qtHandler()
- 这个 returns 错误消息处理程序的全局实例:
QErrorMessage::qtHandler()->showMessage("HELLO WORLD");
但是如果你这样做,请注意 QDebug 也会使用这个:
The static qtHandler() function installs a message handler using
qInstallMessageHandler() and creates a QErrorMessage that displays
qDebug(), qWarning() and qFatal() messages. This is most useful in
environments where no console is available to display warnings and
error messages.
我用的是Qt5.5,写过一个应用程序,如果在应用程序启动过程中遇到错误,我想用QErrorMessage来显示。
我添加了:
QErrorMessage errmsg(this);
errmsg.showMessage("HELLO WORLD");
此代码已插入到我的主要 window 构造函数的末尾,但没有显示任何内容,为什么?
在文档中您可以阅读:
Shows the given message, message, and returns immediately. If the user has requested for the message not to be shown again, this function does nothing.
这意味着,一旦 errmsg
被销毁,将无法再显示任何消息。您必须使用 QErrorMessage
class 作为 member/global 变量:
this->errmsg = new QErrorMessage(this);
this->errmgs->showMessage("HELLO WORLD");
或者,您可以使用 QErrorMessage::qtHandler()
- 这个 returns 错误消息处理程序的全局实例:
QErrorMessage::qtHandler()->showMessage("HELLO WORLD");
但是如果你这样做,请注意 QDebug 也会使用这个:
The static qtHandler() function installs a message handler using qInstallMessageHandler() and creates a QErrorMessage that displays qDebug(), qWarning() and qFatal() messages. This is most useful in environments where no console is available to display warnings and error messages.