QProcess : force cout to be sent
Hello :)
I've two application A and B.
The first one - A - creates a QProcess in order to call the second one - B -.
The first one - A - listen the second one - B -, using readyRead() signal.
So, our B application send data to A in a loop :
Code:
int i = 0;
while (i<10)
{
std::cout << i << std::endl;
i++;
}
The A application detects that data is ready read, and display it :
Code:
message.append(myProcess->readAllStandardOutput());
std::cout << "." << message.toStdString() << "." << std::endl;
And the message is :
Quote:
.0
1
2
3
4
5
6
7
8
9.
I need to control the message content, in order to use correctly :(
So, it woul be nice if I could enforce the cout to be written immediately in the B application... Or enforce the A application to not append all the cout content.
Example :
Code:
int i = 0;
while (i<10)
{
std::cout << i << std::endl; // Enforce this to be submitted immediately
i++;
}
And the message would be :
I could have 10 messages, and not just one.
Have you got an idea ?
Re: QProcess : force cout to be sent
You can use "cout.flush()" to clear the output buffer, but this won't solve your problem. What you send to cout is a stream --- it doesn't contain any messages.
Application "A" should parse the data it receives and divide it into messages for example by looking for \n. Also you shouldn't assume that you will get a whole message at once --- you might get a part of it.
You need something like:
Code:
void Something::readMessages()
{
buffer.append( myProcess->readAllStandardOutput() ); // note that buffer might contain some data from previous calls
while( buffer.containsAtLestOneWholeMessage() ) {
QString msg
( buffer.
takeFirstMessage() );
processMessage( msg );
}
}
QString::endsWith() and QString::split() methods might be helpful.
Re: QProcess : force cout to be sent
Thanks, this is what I will do...
:)