Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see qDebug messages while debugging in QtCreator

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).

like image 217
Etienne Savard Avatar asked Jul 21 '10 13:07

Etienne Savard


People also ask

What is QDebug function?

QDebug is used whenever the developer needs to write out debugging or tracing information to a device, file, string or console.


1 Answers

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.

like image 134
kurige Avatar answered Sep 29 '22 12:09

kurige