Qt redirect

When we use the software, we mostly use the release version, so that the debugging information during use will not be displayed. In order to print the debugging information on the interface or save it in a file, we can use the redirection technology. 

In QT 's help document: qInstallMessageHandler, we can see a simple example of redirection .

1. Redirect debugging information to the application output of 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. The following is an example of redirecting debugging information to the interface:

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);
}

 

Guess you like

Origin blog.csdn.net/weixin_41882459/article/details/113726398