logtotext.h
#include <QTextStream>
#include <QFile>
void debugMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QString message;
QString newline = "\r\n";
switch( type )
{
// 调试信息
case QtDebugMsg:
message = QString("调试信息: ");
break;
// 警告信息
case QtWarningMsg:
message = QString("警告信息: ");
break;
// 严重错误
case QtCriticalMsg:
message = QString("严重错误: ");
break;
// 致命错误
case QtFatalMsg:
message = QString("致命错误: ");
break;
}
QFile log( "DebugLog.txt" );
log.open( QIODevice::WriteOnly | QIODevice::Append );
QTextStream cout( &log );
message.append( " [" ).append( QDateTime::currentDateTime().toString( "yyyy-MM-dd HH:mm:ss" ) ).append( "]" ).append( newline );
message.append( "文 件: " ).append( context.file ).append( " (line: " ).append( QString::number(context.line, 10) ).append( ")" ).append( newline );
message.append( "函 数: " ).append( context.function ).append( newline );
message.append( "内 容: " ).append( msg ).append( newline );
cout << message << newline;
log.flush();
log.close();
}
logbrowser.h
#include <QWidget>
#include <QTextBrowser>
#include <QPushButton>
namespace Ui {
class LogBrowser;
}
class LogBrowser : public QWidget
{
Q_OBJECT
public:
explicit LogBrowser(QWidget *parent = 0);
~LogBrowser();
void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg);
void start();
protected:
void closeEvent(QCloseEvent *event);
void keyPressEvent(QKeyEvent *event);
private:
Ui::LogBrowser *ui;
bool is_finished;
QTextBrowser *browser;
QPushButton *start_button;
QPushButton *clear_button;
};
logbrowser.cpp
#include "logbrowser.h"
#include "ui_logbrowser.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDebug>
#include <QMessageBox>
#include <QCloseEvent>
#include <QKeyEvent>
LogBrowser::LogBrowser(QWidget *parent) :
QWidget(parent),
ui(new Ui::LogBrowser)
{
ui->setupUi(this);
this->resize(400, 300);
is_finished = false;
browser = new QTextBrowser();
start_button = new QPushButton();
clear_button = new QPushButton();
start_button->setText("start");
clear_button->setText("clear");
QHBoxLayout *button_layout = new QHBoxLayout();
button_layout->addStretch();
button_layout->addWidget(start_button);
button_layout->addWidget(clear_button);
button_layout->setSpacing(10);
button_layout->setContentsMargins(0, 0, 10, 10);
QVBoxLayout *main_layout = new QVBoxLayout();
main_layout->addWidget(browser);
main_layout->addLayout(button_layout);
main_layout->setSpacing(10);
main_layout->setContentsMargins(0, 0, 0, 0);
this->setLayout(main_layout);
connect(start_button, &QPushButton::clicked, this, &LogBrowser::start);
connect(clear_button, &QPushButton::clicked, browser, &QTextBrowser::clear);
}
LogBrowser::~LogBrowser()
{
delete ui;
}
void LogBrowser::outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QString message;
switch(type)
{
case QtDebugMsg:
message = QString("Debug:");
break;
case QtWarningMsg:
message = QString("Warning:");
break;
case QtCriticalMsg:
message = QString("Critical:");
break;
case QtFatalMsg:
message = QString("Fatal:");
}
browser->append(message.append(msg));
}
void LogBrowser::start()
{
if(!is_finished)
{
for(int i=0; i<10000000; i++)
{
QCoreApplication::processEvents();
qDebug()<<QString("This is a Qt log browser").append(QString::number(i, 10));
}
is_finished = true;
}
}
void LogBrowser::closeEvent(QCloseEvent *event)
{
QMessageBox::StandardButton answer = QMessageBox::question(
this,
tr("Close Log Browser?"),
tr("Do you really want to close the log browser?"),
QMessageBox::Yes | QMessageBox::No
);
if (answer == QMessageBox::Yes)
event->accept();
else
event->ignore();
}
void LogBrowser::keyPressEvent(QKeyEvent *event)
{
event->ignore();
}
main.cpp
#include <QApplication>
#include <QDebug>
#include <QDateTime>
#include <QPointer>
#include "logtotext.h"
#include "logbrowser.h"
QPointer<LogBrowser> log_broswer;
void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
if(log_broswer)
log_broswer->outputMessage(type, context, msg);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// QString 和 QDebug 的编码和输出
// 一、编码:
// QString 内部保存按照 UTF-16 进行保存(网上搜索资料和实际输出调试得出此结论)。
// QDebug 输出按照 UTF-8 进行输出。
// 二、影响
QString path = "南厂区2号门标清"; // 调试器中查看变量值正常,qDebug() 输出十六进制.
qDebug() << path; // 输出:\u5357\u5382\u533A2\u53F7\u95E8\u6807\u6E05"
qDebug() << path.toStdString().c_str(); // 输出:南厂区2号门标清"
qDebug() << "南厂区2号门标清"; // 输出:南厂区2号门标清
// //调试信息重定向至文件
// qInstallMessageHandler(debugMessageHandler);
// //调试信息重定向至窗口
log_broswer = new LogBrowser();
log_broswer->show();
qInstallMessageHandler(outputMessage);
return a.exec();
}