sashaaero
sashaaero

Reputation: 2898

Why messages printed after closing main window in Qt C++?

With this code

#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <QDir>
#include <QTextStream>

int main(int argc, char *argv[]){
    QApplication a(argc, argv);

    QTextStream out(stdout);
    out << QDir::currentPath();
    std::cout << "Why is that?";

    MainWindow mainWindow;
    mainWindow.show();
    return a.exec();
}

Both messages printed only after closing Main Window of my app, why is this? I tried to debug, debugger thinks that he done with this line, but I see no messages.

Upvotes: 0

Views: 234

Answers (1)

Ramon
Ramon

Reputation: 1299

extern std::ostream cout; is buffered, so it may choose when to flush its buffer to stdout. In your case, it is doing it when your program is terminating.

You can tell std::ostream to flush using std::flush, as such:

std::cout << "Why is that?" << std::flush;

Upvotes: 2

Related Questions