本文介绍了如何在QtCreator中调试时看到qDebug消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Eclipse CDT(使用Qt集成插件)转换到QtCreator 2.0,但仍有一件事情让QtCreator感到困扰:



当我在QtCreator中调试,我没有看到我的qDebug消息在应用程序输出选项卡中,直到我停止正在调试的应用程序...然后它们全部显示



在Eclipse中,我没有这个问题:在执行源代码时遇到qDebug消息时正确显示。



我在Windows下使用Eclipse CDT和Qt Creator。我没有在Linux下尝试(这不是现在的选择)。 解决方案

虽然不是完整的答案,但可以安装(如果您在XP机器上)查看qbbug输出,而你试图弄清楚。



另一种解决方案可能被认为是黑客攻击,但工作得很好,只是简单地劫持调试信息:

  #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()<< 你好,世界。;
qWarning()<< 呃,哦......;
qCritical()<< 哦,没有!;
qFatal(AAAAAAAAAH!);

返回a.exec();
}

会输出:

  [I] Hello world。 
[E]呃,哦...
[!]哦,不!
[X] AAAAAAAAAH!

此应用程序已请求运行时以不寻常的方式终止它。
请联系应用程序的支持团队获取更多信息。

这实际上是我自己发送qDebug,qWarning等的一些代码的修改,到一个日志文件。


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

解决方案

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.

这篇关于如何在QtCreator中调试时看到qDebug消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 21:35