cout在QtCreator中不打印

cout does no print in QtCreator

本文关键字:打印 QtCreator cout      更新时间:2023-10-16

我已经在这个论坛上看到了这个问题,但我不知道为什么提出的答案在我的情况下不起作用。所以我试着要求其他荡妇。

我刚刚让我的Qt创建者在Linux下运行。

我不明白为什么我的:

cout << "This does not appear";

qdebug进行时不在控制台中打印

qDebug() << "This appears";

这就是我的.pro文件中包含的内容:

QT       += core gui
TARGET = aaa
TEMPLATE = app

SOURCES += main.cpp
        mainwindow.cpp 
    IeplcModule.cpp
HEADERS  += mainwindow.h 
    IeplcModule.h
FORMS    += mainwindow.ui
#enable console
CONFIG += console

知道吗?

尝试使用:

cout << "asdf" << endl;

可能Qt设置iostream是为了只在新行刷新。

当使用CDB(Windows调试器(进行调试并不是在专用终端窗口中而是在QtCreator输出面板中运行应用程序时,std::cout/std::cerr出现问题。qDebug之所以有效,是因为它在这种情况下有一个技巧。因此,在这种情况下,唯一的解决方案是启用"在终端中运行"选项。欲了解更多信息,请点击上面的Qt bug跟踪器链接。

STDOUT有可能重定向吗?qDebug默认打印到STDERR。

#include <iostream>了吗?我在代码中没有看到任何包含。我假设qdebugcout非常相似。

确保在.pro文件中启用了console配置。即:

CONFIG += console

您可以从CMD运行此程序,它将向控制台打印一些消息:

/* Create a .pro file with this content:
QT += core gui widgets
SOURCES += main.cpp
TARGET = app
-------------------------------
Build and run commands for CMD:
> qmake -makefile
> mingw32-make
> "release/app"
*/
#ifdef _WIN32
#include <windows.h>
#endif
#include <QtCore/QFile>
#include <QtCore/QString>
#include <QtCore/QIODevice>
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <iostream>
class Widget : public QWidget
{
public:
    Widget()
    {
        setWindowTitle("My Title");
        QString path("assets/text.txt");
        std::cout << std::endl;
        std::cout << "hello1" << std::endl;
        std::cout << path.toStdString() << std::endl;
        std::cout << "hello2" << std::endl;
    }
};
int main(int argc, char *argv[])
{
#ifdef _WIN32
    if (AttachConsole(ATTACH_PARENT_PROCESS))
    {
        freopen("CONOUT$", "w", stdout);
        freopen("CONOUT$", "w", stderr);
    }
#endif
    QApplication app(argc, argv);
    Widget w;
    w.show();
    return app.exec();
}