Qt自定義qDebug輸出

2021-10-01 07:20:33 字數 790 閱讀 9913

qdebug預設輸出字串+換行

想要輸出檔名,函式名,行號需要自定義,

如下是輸出到檔案,並且列印到控制台

//自定義訊息處理函式

void mymessageoutput(qtmsgtype type, const qmessagelogcontext &context, const qstring &msg)

strmessage = strmessage + strmsg + msg;

if(type>qtdebugmsg)

//同時用系統原來的函式完成輸出列印

file* device = stdout;

if(type>qtinfomsg)

qtextstream outputstream(device);

outputstream.setcodec(qtextcodec::codecforname("system"));

outputstream《注意:列印到控制台如果有中文,可能會亂碼,所以一定要加上

outputstream.setcodec(qtextcodec::codecforname("system"));

不寫system的話寫gbk也可以的,但是system看起來偉大一點,

用法:main函式中呼叫

qinstallmessagehandler(mymessageoutput);
需要引用

#include #include #include 。。。。

Qt重定向QDebug,自定義乙個簡易的日誌管理類

相對於第三方的日誌庫,在 qt 中使用 qdebug 列印更便捷,有時候也需要對 qdebug 輸出進行重定向,如寫入檔案等。在 qt4 中使用 qinstallmsghandler 函式設定重定向的函式指標 typedef void qtmsghandler qtmsgtype,const cha...

Qt 自定義事件

最近做的專案,是用qt的完成的,在用到事件派發的時候,要用自己自定義的事件型別來滿足需要。具體就是按照qt的官方文件說明,做了乙個簡單的例子,以免忘記,就先寫下來儲存。首先有個customevent 類,繼承自qevent ifndef customevent h define customeven...

Qt 自定義事件

關於qt的自定義事件也是看了幾個大牛的部落格。總結下心得,如有錯誤請指出。一起成長。先給原始碼。也是第一次原創啊,不知道怎麼寫,呵呵。include include class mywidget public qwidget static const int mycustomeventtype 10...