日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt中qDebug()技巧初探

發布時間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt中qDebug()技巧初探 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • Qt中qDebug()技巧初探
    • 一、發布時屏蔽debug 消息
    • 二、debug 程序定位
    • 三、Qt利用qDebug輸出信息到文件

Qt中qDebug()技巧初探

一、發布時屏蔽debug 消息

發布程序時,去掉debug打印,可以加快程序執行速度,減小程序體積。
然而手動注釋很麻煩,在pro文件里加上一行預定義宏即可。
DEFINES QT_NO_DEBUG_OUTPUT

二、debug 程序定位

例如:qDebug() << FILE << LINE << message;
這樣debug小消息message就可以定位在程序文件中的某一行了。

三、Qt利用qDebug輸出信息到文件

  • 新建dlog.h
  • #ifndef DLOG_H #define DLOG_H #include <qapplication.h> #include <QDateTime> #include <QFile> #include <QTextStream> #include <QtMsgHandler> #include <QMessageLogContext> #include <QMutex>void myMsgOutput(QtMsgType type, const QMessageLogContext &context, const QString& msg) {static QMutex mutex;mutex.lock();QString time=QDateTime::currentDateTime().toString(QString("[ yyyy-MM-dd HH:mm:ss:zzz ]"));QString mmsg;switch(type){case QtDebugMsg:mmsg=QString("%1: Debug:\t%2 (file:%3, line:%4, func: %5)").arg(time).arg(msg).arg(QString(context.file)).arg(context.line).arg(QString(context.function));break;case QtInfoMsg:mmsg=QString("%1: Info:\t%2 (file:%3, line:%4, func: %5)").arg(time).arg(msg).arg(QString(context.file)).arg(context.line).arg(QString(context.function));break;case QtWarningMsg:mmsg=QString("%1: Warning:\t%2 (file:%3, line:%4, func: %5)").arg(time).arg(msg).arg(QString(context.file)).arg(context.line).arg(QString(context.function));break;case QtCriticalMsg:mmsg=QString("%1: Critical:\t%2 (file:%3, line:%4, func: %5)").arg(time).arg(msg).arg(QString(context.file)).arg(context.line).arg(QString(context.function));break;case QtFatalMsg:mmsg=QString("%1: Fatal:\t%2 (file:%3, line:%4, func: %5)").arg(time).arg(msg).arg(QString(context.file)).arg(context.line).arg(QString(context.function));abort();}QFile file("debug.txt");file.open(QIODevice::ReadWrite | QIODevice::Append);QTextStream stream(&file);stream << mmsg << "\r\n";file.flush();file.close();mutex.unlock(); }#endif // DLOG_H
  • 修改main.cpp
  • #include "tool/dlog.h" qInstallMessageHandler(myMsgOutput);
  • pro文件
  • DEFINES += QT_MESSAGELOGCONTEXT

    總結

    以上是生活随笔為你收集整理的Qt中qDebug()技巧初探的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。