【问题标题】:qDebug and cout don't workqDebug 和 cout 不起作用
【发布时间】:2014-05-22 11:45:43
【问题描述】:

我有这个简单的代码

#include <QtCore/qdebug.h>
#include <QtCore/qcoreapplication.h>
#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
    cout << "pluto" << endl;
    QCoreApplication app(argc, argv);
    qDebug() << "pippo" << endl;
    return app.exec();
    //return 0;
}

我在 Eclipse 中使用 MinGw 编译它没有错误,但是当我运行代码时,控制台上没有出现任何字符串消息。怎么了?谢谢。

卢卡

【问题讨论】:

  • 在 Windows 上,qDebug 使用调试通道。 qInstallMessageHandler 是关键。

标签: eclipse qt cout qdebug


【解决方案1】:

要使cout 在 Windows 上工作,您需要在 .pro 文件中包含 CONFIG+=console。它不应该对任何其他平台有任何影响,所以你可以在那里添加它。如果您只希望它用于调试构建或其他东西,您可以使用 qmake 条件。或者如果它更方便您的工作流程,您可以将其作为命令行选项传递给 qmake:

qmake ...other args... CONFIG+=console

在 Windows 下,qDebug() 默认输出到 Windows 调试日志。您可以通过两种方式获得它:

【讨论】:

    【解决方案2】:

    如果你真的需要输出,你可以试试 QTextSteam:

    #include <QTextStream>
    
    QTextStream cout(stdout);
    cout << "string\n";
    
    QTextSteam cerr(stderr);
    cerr << "error!\n";
    

    【讨论】:

    • 这不会改变事情。无论您使用哪个 API,这一切都将发送到 stdout/stderr。
    • @FrankOsterfeld 是的,但至少它会在控制台上打印消息。
    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 2017-04-20
    • 2012-11-06
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多