Windows 10 1809 更新后,Qt Creator 不再在应用程序输出 window 中显示控制台输出

Qt Creator no longer displays console output in Application Output window after Windows 10 1809 update

我正在 运行使用 Qt 5.2.1 和 Qt Creator 4.5.0 调试一个应用程序。几个月来一切正常,控制台输出显示在应用程序输出 window 中。我今天早上将 1809 更新应用到 Windows,现在我只在输出 window 中看到异常详细信息,没有别的。

有趣的是,当我 "Run in Terminal" 时,输出显示在命令 window 中。如果我 "Run" (ctrl-R) 应用程序,它也会显示。当我 运行 使用更高版本的 Qt 框架(5.12.1,在我的例子中)对应用程序进行测试时,它似乎也可以。

有没有其他人遇到过这种情况?我什至不知道从哪里开始解决这个问题。

编辑:更多信息。我查看了 Qt 5.2.1 源代码,发现如果控制台 window 附加到进程,qDefaultMessageHandler 不会输出消息(如您​​所料,例如,如果您选择了 "Run in Terminal option").如果我在我的应用程序开始时调用 FreeConsole(),那么输出会像以前一样出现在应用程序输出 window 中。这表明 Windows 的更新导致控制台 window 被分配给调试进程。

这可能是 Qt 5.2 中的错误,特别是如果它适用于更高版本的 Qt。

正确答案是:升级到更高版本的Qt。 原因是 Qt 5.2 支持在 3 年多前就结束了。

您应该使用当前支持的版本之一:5.9.8、5.12.4 或 5.13.0。

请注意,5.9 和 5.12 是 LTS,但 5.9 支持将于明年结束。所以如果你不想每 6 个月升级一次,5.12 似乎是最合适的。

编辑

如果您出于任何原因无法更新 Qt 的生产版本,您至少可以在您的系统上更新 Qt Creator 并更新您的 Qt 开发版本。

假设您在编写和测试代码时在计算机上使用 Qt 5.9 或 5.12。但是为开发循环的任何其他部分(测试,CI,...)和生产保留 5.2.1。

Qt 提供了很好的跨版本兼容性,如果您编写为 5.2 编译的代码,它将在不改变任何 5.x 且 x >= 2 的情况下进行编译。

对于那些感兴趣的人,我通过在程序开始时有条件地调用 FreeConsole() 解决了这个问题,如下所示:

#ifdef Q_OS_WIN
#include <Windows.h>
#endif

...

#ifdef Q_OS_WIN
#if QT_VERSION <= QT_VERSION_CHECK(5, 2, 1)
    // Since the Windows 10 1809 update was pushed, the debug process has a
    // hidden console window allocated to it. This prevents the application output
    // reaching the console window in Qt 5.2.1 (see qlogging.cpp in the qt source for
    // reasons why). This detaches that console window, meaning the output is shown in the
    // application output window again. However, if "Run in terminal" is selected in the
    // Run options then the output will still be shown in the Application Output window.
    FreeConsole();
#endif
#endif

解决方法很简单:

添加

CONFIG   += console

到您的 .pro 文件