PDA

View Full Version : QProcess : force cout to be sent



Nyphel
5th April 2007, 17:10
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 :


int i = 0;
while (i<10)
{
std::cout << i << std::endl;
i++;
}


The A application detects that data is ready read, and display it :


QString message = "";
message.append(myProcess->readAllStandardOutput());
std::cout << "." << message.toStdString() << "." << std::endl;

And the message is :


.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 :


int i = 0;
while (i<10)
{
std::cout << i << std::endl; // Enforce this to be submitted immediately
i++;
}


And the message would be :


.0.

I could have 10 messages, and not just one.

Have you got an idea ?

jacek
5th April 2007, 18:40
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:

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.

Nyphel
6th April 2007, 00:05
Thanks, this is what I will do...

:)