我正在从Eclipse CDT(带有Qt集成插件)过渡到QtCreator 2.0,但是QtCreator仍然困扰着我一件事:

当我在QtCreator中进行调试时,直到停止正在调试的应用程序时,我才会在Application output tab中看不到qDebug消息。然后将它们全部一次显示出来,这不是很有用。

使用Eclipse,我没有这个问题:单步执行源代码时遇到qDebug消息即可正确显示。

我在Windows下同时使用Eclipse CDT和Qt Creator。我没有在Linux下尝试过(目前还不能选择)。

最佳答案

虽然不是一个完整的答案,但您可以安装DebugView(如果您在XP计算机上)以查看qDebug输出,同时尝试找出答案。

另一个解决方案可能被认为是一种hack,但效果很好,它只是自己劫持调试消息:

#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();
}


哪个会输出:

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


这实际上是对我用来将qDebug,qWarning等发送到日志文件的一些代码的修改。

07-24 09:44
查看更多