将qDebug中打印的信息,输出到界面的控件plainTextEdit中;
头文件:
static ZynqSlaveInfo *m_mainWin; static void setText(QString str);
cpp文件:
#include <QMutex> #include <QDateTime> void myMsgOutput(QtMsgType type, const QMessageLogContext &context, const QString& msg); ZynqSlaveInfo *ZynqSlaveInfo::m_mainWin = nullptr;
ZynqSlaveInfo::ZynqSlaveInfo(QWidget *parent) : QWidget(parent) , ui(new Ui::ZynqSlaveInfo) { ui->setupUi(this); InitSlaveInfo(); } ZynqSlaveInfo::~ZynqSlaveInfo() { delete ui; } void ZynqSlaveInfo::InitSlaveInfo() { m_mainWin = this; qInstallMessageHandler(myMsgOutput); }
void ZynqSlaveInfo::setText(QString str) { m_mainWin->ui->plainTextEdit_logShow->insertPlainText(str); m_mainWin->ui->plainTextEdit_logShow->insertPlainText("\n"); } void myMsgOutput(QtMsgType type, const QMessageLogContext &context, const QString& msg) { static QMutex mutex; Q_UNUSED(type); Q_UNUSED(context); mutex.lock(); #if 0 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();*/ #endif ZynqSlaveInfo::setText(msg); mutex.unlock(); }