How can I see qDebug messages while debugging in QtCreator

44,723

Solution 1

While not a complete answer, you can install DebugView (If you're on an XP machine) to view the qDebug output while you try to figure this out.

Another solution might be considered a hack, but works quite nicely, is to simply hijack debug messages yourself:

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <iostream>

void msgHandler( QtMsgType type, const char* msg )
{
    const char symbols[] = { 'I', 'E', '!', 'X' };
    QString output = QString("[%1] %2").arg( symbols[type] ).arg( msg );
    std::cerr << output.toStdString() << std::endl;
    if( type == QtFatalMsg ) abort();
}

int main(int argc, char *argv[])
{
    qInstallMsgHandler( msgHandler );
    QCoreApplication a(argc, argv);

    qDebug() << "Hello world.";
    qWarning() << "Uh, oh...";
    qCritical() << "Oh, noes!";
    qFatal( "AAAAAAAAAH!" );

    return a.exec();
}

Which would output:

[I] Hello world. 
[E] Uh, oh... 
[!] Oh, noes! 
[X] AAAAAAAAAH!

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

This is actually a modification of some code that I use myself to send qDebug, qWarning, etc., to a log file.

Solution 2

You don't have to close the application to see qDebug() messages.

There is a tab named 3 - Application output at the very bottom of the Qt Creator. Clicking that window will show you the Application output window at the bottom of Qt Creator.

That particular window will display the qDebug() messages as soon as they get called while the application is still running.

Hope it helps.

Edit:

I am not sure whether this is an answer but it might be a good valid cause.

From qDebug() documentation,

The Qt implementation of these functions prints the text to the stderr output under Unix/X11 and Mac OS X. With Windows, if it is a console application, the text is sent to console; otherwise, it is sent to the debugger.

Now Qt Creator doesn't have it's own debugger attached to it.

From Qt Creator documentation, we have to manually install the debugger. Since you are using Windows, you need to install Debugging tools for Windows manually.. More documentation can be found here...

Though am not used to Eclipse CDT, I assume there might be a Debugger attached to it and hence it displays the Debugging output correctly..

Since there isn't a Debugger attached to the Qt Creator, it might be behaving strangely..

Just give it a try..

Solution 3

To add to the above answers..

Always make sure to build in debug mode and not in release mode.. As qDebug() , only works with the debug build.

This was the silly mistake i made, before my search for the led me here, and i wanted to add this fine but important point, to the list of other answers.

Solution 4

Have you tried to add the following line to your .pro?

OUTPUT += Console

Then you can output on std::cout.

Solution 5

Let me tell you something:

Once I have developed a c++ console application on linux. During running the application it was reading a file and starting to process some logic by printing out some status messages. When I run that application the output was OK. So for convenient debugging I have decided to run the application like this:

./a.out |& tee log

This command redirects standard output (may be also standard error I don't remember) into a file named "log". So when I run with this option I saw that it writes in the log file exactly the same as in std out only there are some displacements like this:

in std out - desired output

A
operation 1 success
B
operation 2 success
C
operation 3 success
D
operation 4 success

in the log file - output with displacement (this is not correct)

A
B
C
D
operation 1 success
operation 2 success
operation 3 success
operation 4 success

I guess your problem is this kind of one... I have taken a look at QDebug and have not seen any function that frees the buffer (something like operation called flush). So I recommend you to not waist your time on this kind of small issue (I also consider that Qt Creator 2.0 is release as a beta version and it may appear a bug) and use one of the following:

qFatal()
qCritical()
qWarning()
QMessageBox

I personally use QMessageBox::about in order to debug as you can stop the screan and read the value in a very usefull places that with debugger you can't (I mean you can but you can't see the current state of you GUI application as debugger have taken the control).

Hope helps.

Share:
44,723
Etienne Savard
Author by

Etienne Savard

Who am I? I'm Étienne Savard, a Certified Professional Scrum Master (PSM), a pragmatic programmer, and an Open Source advocate. I've been a software developer for a long time, it all started on a TRS-80 Color Computer (when 64K of memory was a lot and 16 colors was enough). Since, I pursue my quest to boldly go where no developer has gone before! As an IT consultant, I'm helping companies (from startups to big corporations) to integrate best of breed tools and best practices into their software development process. Currently looking for new opportunities as an Android freelance consultant.

Updated on July 09, 2022

Comments

  • Etienne Savard
    Etienne Savard almost 2 years

    I'm making the transition from Eclipse CDT (with Qt integration plugin) to QtCreator 2.0 but there is still one thing that bother me with QtCreator :

    When I debug in QtCreator, I don't see my qDebug messages inside the Application output tab until I stop the application I'm debugging... Then they are all displayed at once which is not very useful.

    With Eclipse, I don't have this problem : the qDebug messages are displayed correctly when encountered while stepping through the source code.

    I'm using both Eclipse CDT and Qt Creator under Windows. I didn't try under Linux (which is not an option right now).