【问题标题】:How does Qt5 redirect qDebug() statements to the Qt Creator 2.6 consoleQt5 如何将 qDebug() 语句重定向到 Qt Creator 2.6 控制台
【发布时间】:2013-02-01 09:35:12
【问题描述】:

在搜索了 qDebug() 语句在 Qt 的标准消息处理程序中正常工作但在切换到我自己的消息处理程序时失败的原因后,我在这里呼吁看看是否有其他人对此问题有任何经验。

我知道/尝试过的事情,但无济于事......

1) CONFIG += console

2) DEFINES -= QT_NO_WARNING_OUTPUT QT_NO_DEBUG_OUTPUT

3)::fprintf(stderr, "ERROR\n"); ::fflush(stderr);

4) ::fprintf(stdout, "OUTPUT\n"); ::fflush(stdout);

5)std::cerr << "CERROR" << std::endl; std::cerr.flush();

但是在使用内置处理程序时它可以正常工作(即,它将消息打印到 QtCreator 控制台)

int main(int argc, char *argv[]) {
    // Use my handler
    qInstallMessageHandler(MyCustomLogger);
    qDebug() << "Not Printed";

    // Use standard handler
    qInstallMessageHandler(0);
    qDebug() << "Correctly Printed";

    // Use my handler again
    qInstallMessageHandler(MyCustomLogger);
    qDebug() << "Not Printed Again...";
}

最近的测试是使用 WinAPI 命令为自己分配一个控制台,这会导致正确的行为,所有输出到 stderr 和 stdout 在我创建的控制台上都是可见的。但是,这不是我想要的行为,我希望能够在 QtCreator 中查看此输出。

关于标准消息处理程序如何打印到调试器的任何想法? 我还没有在 Qt 源代码中找到它。

【问题讨论】:

  • 在 windows 上,qDebug() 使用调试通道,而不是 stderr。
  • 我必须承认我以前从未听说过调试通道,请转至 google。

标签: c++ qt console qt-creator stderr


【解决方案1】:

正如 Frank Osterfeld 在他的评论中提到的:

在 windows 上,qDebug() 使用调试通道,而不是 stderr。

在深入研究 QDebug 代码和 QMessageLogger 之后,我找到了答案。好用的WinAPI函数OutputDebugString

用法(改编自peppe's):

#include <QApplication>
#include <QtDebug>
#include <QtGlobal>

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

void MyMessageOutput(QtMsgType Type, const QMessageLogContext& Context, const QString &Message)
{
    OutputDebugString(reinterpret_cast<const wchar_t *>(Message.utf16()));
}

int main(int argc, char **argv)
{
    // A GUI application
    QApplication app(argc, argv);

    // Custom handler
    qInstallMessageHandler(myMessageOutput);
    qDebug() << "Printed in the console using my message handler in a windows GUI application";

    // Default handler
    qInstallMessageHandler(0);
    qDebug() << "Also printed in the console!";

    // Show GUI here
    //MainForm *MF = new MainForm();
    //MF->show();

    return app.exec();
}

【讨论】:

  • 有趣的是,这也是让 Creator 在调试时打印自定义处理程序的消息的方法(否则进入处理程序,但在 Creator 控制台中什么也没有结束) .
【解决方案2】:

我无法重现您的问题:这对我来说是正确的。

#include <QCoreApplication>
#include <QtDebug>
#include <QtGlobal>

#include <stdio.h>
#include <stdlib.h>

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    fprintf(stderr, "MESSAGE (%s:%u %s): %s\n", context.file, context.line, context.function, localMsg.constData());
    fflush(stderr);
}

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    qInstallMessageHandler(myMessageOutput);
    qDebug() << "Printed in the console";
    qInstallMessageHandler(0);
    qDebug() << "Also printed in the console";
    return app.exec();
}

【讨论】:

  • 这很奇怪。可能与我正在使用的其他库有关吗?当我在我的代码中运行该处理程序时,我仍然只得到一行......
  • 啊,也许不是,因为我使用的是 QApplication 而不是 QCoreApplication,所以我可以有一个在你的示例中不起作用的 gui。
  • 问题已解决,使用 OutputDebugString(...) 我会在 5 小时内添加我的答案,当系统允许我...
猜你喜欢
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 2011-09-08
相关资源
最近更新 更多