cout does no print in QtCreator
Asked Answered
C

6

15

I saw this question already on this forum but I do not know why the proposed answer does not work in my case. So I try to ask for other slution.

I just got my Qt creator running under Linux.

I do not understand why my:

cout << "This does not appear";

Does not print in console while qdebug does

qDebug() << "This appears";

This is what is contained in my .pro file:

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

Any idea?

Capillaceous answered 27/9, 2011 at 8:4 Comment(3)
cout << "This does not appear" << flush; Maybe this one?Cooperate
Does the output appear when the program stops? If yes, it's probably just a missing flush (add a std::endl)Nellanellda
Debug output is shown directly. The <iostream> equivalent of qDebug() is cerr <<, that doesn't need flushing either.Cowden
F
30

Try with:

cout << "asdf" << endl;

Possibly Qt sets up iostream in order to flush only at new line.

Fatshan answered 27/9, 2011 at 8:39 Comment(2)
+1 For the correct answer and explanation, by the way you didn't suggest using qDebug() as everyone did.Tetralogy
Actually, endl is a newline plus flush command. Similar to "\n" << flushSeptum
S
3

When debugging with CDB (Windows debugger) and running application not in the dedicated terminal window, but within QtCreator output panel, there is an issue with std::cout/std::cerr. qDebug works because it has a trick for this case. So, the only solution in this case is enable the "run in terminal" option. For more infor please follow the link above to the Qt bug tracker.

Soot answered 1/3, 2019 at 0:21 Comment(0)
S
1

Is it possible that STDOUT is redirecting? qDebug prints to STDERR by default.

Sprage answered 27/9, 2011 at 8:20 Comment(0)
M
1

Did you #include <iostream>? I did not see any includes in the code. I assume that qdebug and cout are very similar.

Moradabad answered 11/9, 2012 at 19:25 Comment(1)
This question has already been solved by the accepted answer.Nocturn
E
0

Make sure you have console config enabled in your .pro file. I.e. :

CONFIG += console
Echikson answered 21/2, 2020 at 8:11 Comment(0)
T
0

You can run this program from CMD and it will print some messages to the console:

/* 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();
}
Tonsillotomy answered 30/1, 2023 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.