我正在从 Eclipse CDT(带有 Qt 集成插件)过渡到 QtCreator 2.0,但 QtCreator 仍有一件事困扰着我:
当我在 QtCreator 中调试时,我在应用程序输出选项卡
中看不到我的 qDebug 消息,直到我停止正在调试的应用程序...然后它们会立即全部显示,这是不是很有用。
使用 Eclipse,我不会遇到此问题:在单步执行源代码时遇到 qDebug 消息时,会正确显示。
我在 Windows 下同时使用 Eclipse CDT 和 Qt Creator。我没有在 Linux 下尝试(目前还没有这个选项)。
最佳答案
虽然不是完整的答案,但您可以安装 DebugView (如果您使用的是 XP 计算机)在尝试解决此问题时查看 qDebug 输出。
另一种解决方案可能被认为是黑客,但效果很好,就是简单地自己劫持调试消息:
#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 等发送到日志文件的一些代码的修改。
关于qt - 在 QtCreator 中调试时如何查看 qDebug 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3299981/