qDebug Qt console application to output to Qt Creator application output

不打扰是莪最后的温柔 提交于 2019-12-04 12:28:31

问题


How do I use qDebug in a Qt console application to output to the Qt Creator "application output" window? Currently qDebug writes to the console window which interferes with the non-debug output.

Using qDebug in a Qt GUI app outputs to application output window by default.


回答1:


To redirect QDebug to multiple places, you might have to write some code, maybe like this:

QList<QtMsgHandler> messageHandlers_;

static void messageDispatcher(QtMsgType type, const char *msg)
{
  foreach (QtMsgHandler callback, ::messageHandlers_)
    callback(type, msg);
}

static void messageLogger(QtMsgType type, const char *msg)
{
  QString output;
  switch (type) {
  case QtDebugMsg:    output = QString("mesage: %1\n").arg(msg); break;
  case QtWarningMsg:  output = QString("warning: %1\n").arg(msg); break;
  case QtCriticalMsg: output = QString("critical: %1\n").arg(msg); break;
  case QtFatalMsg:    output = QString("fatal: %1\n").arg(msg); break;
  default: return;
  }

  QFile file("log.txt");
  if (file.open(QIODevice::WriteOnly | QIODevice::Append))
    QTextStream(&file) << output;
}

int main()
{
  ...
  ::messageHandlers_.append(messageLogger)
  qInstallMsgHandler(messageDispatcher);
  ...
}



回答2:


You can either output everything to console or everything to Qt Creator's Application Output panel.

For sake of completeness: If you want to have all the output in the panel instead of console you can uncheck "Run in terminal" in Project->Run settings.



来源:https://stackoverflow.com/questions/11770211/qdebug-qt-console-application-to-output-to-qt-creator-application-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!