0

I have a rather strange problem that I've never encountered before and I have no idea what is causing it. I'm working in Qt 4.7, and I have a lot of things in my project that print to the terminal. These prints are all triggered by one button press. However, when I press the button, they don't all print. Instead, all the print statements that end with << std::endl; will print, but the first one that ends with "random text...\n"; won't, and none of the ones after that will either regardless of which style they use. If I hit the button again to run the entire thing again, it will then print the remaining statements, followed by the same initial output as last time. I could just make them all std::endl, but there's a lot of them, so I'd really rather not. I've never seen anything like this before, does anyone have any advice for how to fix it? Thanks!

EDIT: As per request, this is the syntax (assuming there's a button call mybutton on the UI)

void on_mybutton_pressed()
{
std::cout << "A" << std::endl;
std::cout << "B" << std::endl;
std::cout << "C\n";
}

Output after first time button is pressed:

A
B

Output after second press:
C
A
B

4

2 回答 2

2

字符输出被缓冲。缓冲区满时会被刷新,但通常对于您不想等待的终端;std::endl特殊之处在于它会立即刷新缓冲区。

于 2013-10-01T19:37:33.713 回答
1

cout如果您真的想要,您可以在程序开始时启用单元缓冲:

std::cout << std::unitbuf;

这将在每次输出操作后刷新缓冲区。

在您的情况下,我认为这有点像黑客而不是解决方案。有与冲洗相关的性能损失(尽管如果您不以高频率打印它们并不重要)。

于 2013-10-01T19:41:18.470 回答