qDebug Qt console application to output to Qt Creator application output
Asked Answered
B

2

8

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.

Application output

Bairam answered 2/8, 2012 at 2:22 Comment(0)
G
2

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);
  ...
}
Gooseherd answered 23/10, 2013 at 21:35 Comment(1)
This code doesn't work. Not sure if it's because of typos in the code sample (of which there seem to be at least one) or because Qt has progressed since 2013 and the answer uses deprecated calls.Halftimbered
C
7

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.

Counterblast answered 13/9, 2012 at 17:25 Comment(3)
I know that...but I want output to two different places...that's why its debug output.Bairam
Unfortunately you can not do that.Counterblast
I have "Run in terminal" unchecked and still don't see any output from my app. std::cout, qDebug doesn't output anythingRussellrusset
G
2

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);
  ...
}
Gooseherd answered 23/10, 2013 at 21:35 Comment(1)
This code doesn't work. Not sure if it's because of typos in the code sample (of which there seem to be at least one) or because Qt has progressed since 2013 and the answer uses deprecated calls.Halftimbered

© 2022 - 2024 — McMap. All rights reserved.