Reputation: 6597
Using Qt (v5.15.0) in Visual Studio 2019 with the Qt VS Tools
extension installed. Simple console application. Below is a simple demonstration of the problem:
#include <iostream>
#include <QDebug>
void main()
{
std::cout << "cout: Line 1\n";
qDebug() << "qDebug: Line 2";
std::cout << "cout: Line 3\n";
QTextStream qts{stdout};
qts << "QTextStream: Line 4\n";
qts << "QTextStream: Line 5\n";
qts.flush();
qDebug() << "qDebug: Line 6";
std::cout << "cout: Line 7\n"; // Not printed!
qDebug() << "qDebug: Line 8";
}
Everything is printed except Line 7
. Comment out the four QTextStream
lines, and Line 7
is printed again.
This seems to be related to the QTextStream
object "capturing" the standard output. How do I get the object to release stdout
so that std::cout
starts working again?
Upvotes: 1
Views: 263
Reputation: 944
On my computer (visual studio community edition 2019 and QT 5.9.9) this compiles and prints every line including line 7. If you want to make sure the buffer is flushed at line 7 you can use :
std::cout << "cout: Line 7\n" << std::flush;
but you'd have to use it every line you want to be certain to be flushed.
Alternatively, if you want the buffer to be flushed every time you call std::cout
you could use unitbuf
:
std::cout << std::unitbuf
When the unitbuf flag is set, the associated buffer is flushed after each insertion operation.
On your system these 4 QTextStream
lines have the side effect of not having line 7 flushed by default. On my system these lines don't have this side effect. Because line 7 was never guaranteed to be flushed in the first place, this really isn't a problem.
Upvotes: 1