How can I see qDebug messages while debugging in QtCreator

前端 未结 7 1447
感动是毒
感动是毒 2021-02-04 06:38

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 Qt

相关标签:
7条回答
  • 2021-02-04 07:00

    This is a workaround, but you could probably install your own message handler. Take a look at qInstallMsgHandler in the documentation if you have not done so already.

    I have a similar issue on Ubuntu 9.10 with Qt Creator 2.0 and Qt 4.7.0, though I don't see stdout output until the application is closed. It is as if the buffer isn't flushed. Printing to stderr shows the output in the Application output window immediately though.

    fprintf(stderr, "Test1 \n"); // Prints immediately
    fprintf(stderr, "Test2 \n\r"); // Prints immediately
    fprintf(stdout, "Test3 \n"); // Delayed until app termination
    fprintf(stdout, "Test4 \n\r"); // Delayed until app termination
    qDebug() << "Test6 \n\r"; // Does not print at all
    
    0 讨论(0)
  • 2021-02-04 07:11

    None of these answers were right for me (Arch linux user). Rather than try to explain my troubles, here is a .pro file that worked. I'm not using the QT thread loop, purely just a simple main() that does stuff and exists. I'm using cout for my output:

    QT += core
    QT -= gui
    
    CONFIG += c++14
    
    TARGET = evo
    #CONFIG += console
    CONFIG -= app_bundle
    #CONFIG += qt
    
    #OUTPUT += console
    
    TEMPLATE = app
    
    SOURCES += main.cpp \
        individual.cpp \
        node.cpp \
        tree.cpp
    
    HEADERS += \
        individual.h \
        node.h \
        tree.h
    
    0 讨论(0)
  • 2021-02-04 07:12

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

    0 讨论(0)
  • 2021-02-04 07:16

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

    OUTPUT += Console

    Then you can output on std::cout.

    0 讨论(0)
  • 2021-02-04 07:16

    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.

    0 讨论(0)
  • 2021-02-04 07:17

    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.

    0 讨论(0)
提交回复
热议问题