ソフトウェアを使用する場合、ほとんどの場合リリースバージョンを使用するため、使用中のデバッグ情報は表示されません。デバッグ情報をインターフェイスに印刷したり、ファイルに保存したりするには、リダイレクトテクノロジを使用できます。 でQTのヘルプドキュメント:qInstallMessageHandler、我々は見ることができるの簡単な例リダイレクトを。
1.デバッグ情報をQtCreatorのアプリケーション出力にリダイレクトします。
#include <qapplication.h>
#include <stdio.h>
#include <stdlib.h>
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtInfoMsg:
fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
abort();
}
}
int main(int argc, char **argv)
{
qInstallMessageHandler(myMessageOutput);
//QT4: qInstallMsgHandler();
//QT5: qInstallMessageHandler();
QApplication app(argc, argv);
...
return app.exec();
}
2.以下は、デバッグ情報をインターフェイスにリダイレクトする例です。
void myMessageOutput::OutPutMessage(QtMsgType type, const QMessageLogContext &context,
const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QDateTime time = QDateTime::currentDateTime();
QString strTime = time.toString("hh:mm:ss ");
QString text;
QString htmlText;
switch (type) {
case QtDebugMsg:
text = QString::fromLocal8Bit("%1 D: %2 \n").arg(strTime).arg(QString::fromLocal8Bit(
localMsg.constData()));
htmlText = formatHtml(text, "green");
break;
case QtInfoMsg:
text = QString::fromLocal8Bit("%1 I: %2 \n").arg(strTime).arg(QString::fromLocal8Bit(
localMsg.constData()));
htmlText = formatHtml(text, "green");
break;
case QtWarningMsg:
text = QString::fromLocal8Bit("%1 W: %2 \n").arg(strTime).arg(QString::fromLocal8Bit(
localMsg.constData()));
htmlText = formatHtml(text, "rgb(255, 170, 0)");
break;
case QtCriticalMsg:
text = QString::fromLocal8Bit("%1 C: %2 \n").arg(strTime).arg(QString::fromLocal8Bit(
localMsg.constData()));
htmlText = formatHtml(text, "red");
break;
case QtFatalMsg:
text = QString::fromLocal8Bit("%1 F: %2 \n").arg(strTime).arg(QString::fromLocal8Bit(
localMsg.constData()));
htmlText = formatHtml(text, "red");
break;
default:
text = QString::fromLocal8Bit("Time: %1 Default: %2 (%3:%4, %5)\n").arg(strTime).arg(
QString::fromLocal8Bit(localMsg.constData())).arg(context.file).arg(context.line).arg(
context.function);
htmlText = formatHtml(text, "black");
}
gOutStream << QDateTime::currentDateTime().toString("[yyyy-MM-dd hh.mm.ss]\t") + text;
gOutStream .flush();
ui->log_tbrowser->append(htmlText);
ui->log_tbrowser->moveCursor(QTextCursor::End);
}
const QString myMessageOutput::formatHtml(const QString &qText, QString color)
{
//设置相关的字体颜色
return QString("<font style='font-size:36px; background-color:white; color:%2;'> %1 </font><br/>").arg(
qText).arg(color);
}