Qt中QMessageLogContext的context.file和context.line为空的解决方案

本文介绍了在Qt中,QMessageLogContext的日志文件和行号在Release版本下通常为空的问题,原因是Qt为提高性能默认去除了这些信息。解决方案是在.pro文件中添加宏并重新编译以恢复这些上下文信息。

Qt中QMessageLogContext的context.file和context.line为空的解决方案

 

QDebug版本是可以输出日志信息的,但在Release中输入日志的文件信息和行数为空。原因是,

Qt在Release版本默认舍弃了文件信息、行数等信息。

解决方案:

在.pro文件定义一个宏:

DEFINES += QT_MESSAGELOGCONTEXT

然后,qmake,重新编译,就可以了~

 

帮我详细解读下面的程序代码 #include "mainwindow.h" #include <QApplication> #include <QElapsedTimer> #include <QDebug> #include <JlCompress.h> #include <QSplashScreen> #include <QDateTime> #include <QDesktopWidget> // 重定向qdebug的打印 void log_out_put(QtMsgType type, const QMessageLogContext &context, const QString &msg); /** * @brief 重定向qdebug的打印 * @param type * @param context * @param msg */ void log_out_put(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QByteArray localMsg = msg.toLocal8Bit(); switch (type) { case QtDebugMsg: fprintf(stdout, "[Debug] %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtWarningMsg: fprintf(stdout, "[Warning] %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtCriticalMsg: fprintf(stdout, "[Critical] %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtFatalMsg: fprintf(stdout, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); abort(); break; default: fprintf(stdout, "[Debug] %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; } #ifndef QT_NO_DEBUG_OUTPUT fflush(stdout); #endif } int main(int argc, char *argv[]) { #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QApplication a(argc, argv); QDesktopWidget *desktopWidget = QApplication::desktop(); QRect screenRect = desktopWidget->screenGeometry(); QImage Image; Image.load(":/icon/icon/启动logo.png"); // 1200x900 QPixmap oldPixmap = QPixmap::fromImage(Image); QPixmap pixmap = oldPixmap.scaled(screenRect.width() / 2, screenRect.height() / 2, Qt::KeepAspectRatio, Qt::SmoothTransformation); QSplashScreen splash(pixmap); splash.show(); QDateTime time = QDateTime::currentDateTime(); QDateTime curTime = QDateTime::currentDateTime(); while (time.msecsTo(curTime) <= 500) { curTime = QDateTime::currentDateTime(); a.processEvents(); } QFont f = a.font(); f.setFamily("微软雅黑"); f.setPixelSize(16); a.setFont(f); QElapsedTimer cost; QTranslator SysTranslator; SysTranslator.load(QString(":/qss/resource/qt_zh_CN.qm")); qApp->installTranslator(&SysTranslator); cost.start(); MainWindow w; QRect screenGeometry = desktopWidget->availableGeometry(); int screenWidth = screenGeometry.width(); int screenHeight = screenGeometry.height(); // 设置窗口大小为屏幕的 80%,并设置最小最大尺寸 int windowWidth = screenWidth; int windowHeight = screenHeight; w.resize(windowWidth, windowHeight); // 设置窗口的最小尺寸 w.setMinimumSize(windowWidth * 0.8, windowHeight * 0.8); // 设置窗口的最大尺寸 w.setMaximumSize(windowWidth * 1.2, windowHeight * 1.2); // 将窗口居中 // w.move((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2); w.show(); splash.finish(&w); return (a.exec()); }
最新发布
11-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

季截

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值