Qt redirects cout output to qDebug

Ok, I am using MSVS 2013 and Qt (msvc2013 + opengl 32bit)

I have a main application window written in Qt and extension dll. The main application writes qDebug (). But the dll extension doesn't know anything about qDebug (), so I am using std :: cout. I found an ugly solution:

void Console()
{
    AllocConsole();
    FILE *pFileCon = NULL;
    pFileCon = freopen("CONOUT$", "w", stdout);

    COORD coordInfo;
    coordInfo.X = 130;
    coordInfo.Y = 900;

    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordInfo);
   SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE),ENABLE_QUICK_EDIT_MODE| ENABLE_EXTENDED_FLAGS);
}

      

And basically I call a function Console()

, it gives me a black console near my window. It's not very neat and I also want to see events sequentially, but I can see them separately in the Output app and the console.

So the question is: is there a way to capture all std :: cout output from a dll and throw it to the application output?

+3


source to share


1 answer


You need to redirect the output given by qDebug ().

static void customMsgHandler(QtMsgType type, const QMessageLogContext &, const QString &message)
{
  QString txt;
  switch (type) {
  case QtDebugMsg:
    txt = QString("Debug: %1").arg(message);
    break;
  case QtWarningMsg:
    txt = QString("Warning: %1").arg(message);
    break;
  case QtCriticalMsg:
    txt = QString("Critical: %1").arg(message);
    break;
  case QtFatalMsg:
    txt = QString("Fatal: %1").arg(message);
    abort();
  }

  QFile outFile("log.txt");
  outFile.open(QIODevice::WriteOnly | QIODevice::Append);
  QTextStream ts(&outFile);
  ts << txt << endl;
}

      

Then in the main () function after the QAplication declaration



qInstallMessageHandler(customMsgHandler);

      

This will create a trace file for you to log all calls to qDebug ().

Hope this helps

-2


source







All Articles