助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
[TOC] ## 概述 - 有可能需要捕获和定制这些日志消息的处理方式,例如将它们记录到文件、发送到远程服务器或以其他方式进行处理 - `qInstallMessageHandler` 允许您替换默认的消息处理程序,使用自己的消息处理函数来处理 Qt 应用程序的日志消息 ## 示例 ``` #include <QCoreApplication> #include <QDebug> void customMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg) { // 自定义消息处理函数,根据消息类型执行不同的操作 switch (type) { case QtDebugMsg: fprintf(stderr, "Debug: %s\n", msg.toUtf8().constData()); break; case QtInfoMsg: fprintf(stderr, "Info: %s\n", msg.toUtf8().constData()); break; case QtWarningMsg: fprintf(stderr, "Warning: %s\n", msg.toUtf8().constData()); break; case QtCriticalMsg: fprintf(stderr, "Critical: %s\n", msg.toUtf8().constData()); break; case QtFatalMsg: fprintf(stderr, "Fatal: %s\n", msg.toUtf8().constData()); abort(); // 程序终止 } } int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); // 安装自定义消息处理程序 qInstallMessageHandler(customMessageHandler); // 生成一些日志消息 qDebug() << "Debug message."; qInfo() << "Information message."; qWarning() << "Warning message."; qCritical() << "Critical message."; qFatal("Fatal error message."); return app.exec(); } ```