将qDebug中打印的信息,输出到界面的控件plainTextEdit中;
头文件:文章来源:https://www.toymoban.com/news/detail-508867.html
static ZynqSlaveInfo *m_mainWin;
static void setText(QString str);
cpp文件:文章来源地址https://www.toymoban.com/news/detail-508867.html
#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();
}
到了这里,关于QDebug 信息输出到控件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!