stdout/cout from application launched from qt creator?

10,820

Solution 1

qDebug() and related functions are handy for that sort of thing - will get sent to the debugger (if you're using Qt Creator, that will pick those up easily!)

#include <QDebug>

qDebug() << "x is: " << x;

Solution 2

You can always start your programs from the command line to see stdout output (cmd.exe). Also, like Paul Dixon said, by using qDebug() you should be able to see the output in the debugger.

#include <QDebug>
...
{
   ...
   int x = 5;
   qDebug() << "x is: " << x;
}

Solution 3

One cheap way is to simply reopen stdout/err (well atleast in win32, I'm assuming it'll work with Qt as well)

#include <stdio>

//add this at the beginning of your main
freopen("c:\\temp\\stdout.txt","w",stdout);
freopen("c:\\temp\\stderr.txt","w",stderr);

If you need some more serious tracing/logging consider e.g. log4cxx

Solution 4

I found a setting under Tools -> Options -> Build & Run -> [X] Merge stderr and stdout.

That will help things sent to cout be shown.

Share:
10,820
tim
Author by

tim

Updated on June 27, 2022

Comments

  • tim
    tim about 2 years

    I've been learning qt on windows for a little while (background in unix/embedded) and would like to have stderr/stdout dumped out somewhere (unit test/event logging/debug) from my win32 qt GUI app. That seems to be a tall order in windows and I found this post on stackoverflow which explains why.

    I find myself wondering why qt doesn't have a simple mechanism for performing some of the suggestions in the post for debug builds.

    Does such a facility already exist in qt or am I left to roll my own (or find a syslog lib)?

  • tim
    tim almost 15 years
    Thanks for the pointer, I have my own logging infrastructure I invented that looks a whole lot like log4cxx. I will have to have a closer look at that one to see if I should use that instead.