why can't QProcess read all output?
hi guys!
I'm using QProcess start a program,but I'm come across a problem.the problem is when I read the QProcess,it doesn't read all of the output.eg,actually,the output should be two lines,but it shows only one line.what's wrong with it?If someone can help me?thanks!
Re: why can't QProcess read all output?
There are two possibilities:
1. the other line doesn't end with a \n so it wasn't flushed by the system into the output stream
2. the other line was sent to stderr and not stdout
Re: why can't QProcess read all output?
Quote:
Originally Posted by
wysota
There are two possibilities:
1. the other line doesn't end with a \n so it wasn't flushed by the system into the output stream
2. the other line was sent to stderr and not stdout
I have knew that the second line end with a \n.and it should be the second possibility,but,before I start the program,I set the process like this: inprocess.setReadChannelMode(QProcess::MergedChann els);
and in the connect,I write like this: connect(&inprocess,SIGNAL(readyReadStandardOutput( )),this,SLOT(iupdateTextEdit()));
connect(&inprocess,SIGNAL(readyReadStandardError() ),this,SLOT(iupdateTextEdit()));
the following show the iupdateTextEdit() fuction
void mainWindow::iupdateTextEdit()
{
QByteArray newData=inprocess.readAllStandardOutput();
QString text=textEdit->toPlainText()+QString::fromLocal8Bit(newData);
textEdit->setPlainText(text);
}
even so,it can't show the second line in the textEdit.
Re: why can't QProcess read all output?
Did you try simply readAll()?
Re: why can't QProcess read all output?
Disclaimer: I didn't use this... so i might be wrong, but
as far as i know the '\n' line end doesn't flush the stream, try to use std::endl to finish the line.
Re: why can't QProcess read all output?
Quote:
Originally Posted by
wysota
Did you try simply readAll()?
it doesn't work too.
Re: why can't QProcess read all output?
Quote:
Originally Posted by
Zlatomir
Disclaimer: I didn't use this... so i might be wrong, but
as far as i know the '\n' line end doesn't flush the stream, try to use std::endl to finish the line.
the program that i want to call was wrote in C not C++.
Re: why can't QProcess read all output?
In C you can use:
Code:
printf(...);
fflush(stdout);
to flush the stdout buffer
Re: why can't QProcess read all output?
Quote:
Originally Posted by
Zlatomir
In C you can use:
Code:
printf(...);
fflush(stdout);
to flush the stdout buffer
if like the following ?
void mainWindow::iupdateTextEdit()
{
QByteArray newData=inprocess.readAllStandardOutput();
QString text=textEdit->toPlainText()+QString::fromLocal8Bit(newData);
textEdit->setPlainText(text);
fflush(stdout);
}
Re: why can't QProcess read all output?
No, don't use that in Qt.
Use that in your C application (if you have the source of the C application)
Re: why can't QProcess read all output?
If you run your C program from a dos command line, do you get all output?
Re: why can't QProcess read all output?
Quote:
Originally Posted by
Zlatomir
No, don't use that in Qt.
Use that in your C application (if you have the source of the C application)
unfortunately,i haven't the soures of the C application.
Re: why can't QProcess read all output?
Quote:
Originally Posted by
fatjuicymole
If you run your C program from a dos command line, do you get all output?
If i set the Konsole to super Konsole(su -) ,i can get all output.but if i set it to "/bin/bash",it can't .
Re: why can't QProcess read all output?
Quote:
Originally Posted by
Zlatomir
Disclaimer: I didn't use this... so i might be wrong, but
as far as i know the '\n' line end doesn't flush the stream, try to use std::endl to finish the line.
Flushing the stream is done by the runtime (or even the operating system), not by the application hence there is no difference between \n and std::endl. And I'm sure \n causes the stream flush, at least in those systems that I know of.
Re: why can't QProcess read all output?
Quote:
Originally Posted by
Raul
hi guys!
I'm using QProcess start a program,but I'm come across a problem.the problem is when I read the QProcess,it doesn't read all of the output.eg,actually,the output should be two lines,but it shows only one line.what's wrong with it?If someone can help me?thanks!
to my suprise,it sometimes can read the second line.
Re: why can't QProcess read all output?
@Wysota: You are right, the actual flush is "done" by the OS, but "when" is done can be controlled from your code, and the new-line character '\n' doesn't "force" the OS to flush the output stream, while the endl does.
@Raul: your problem is within the C application
i don't think that you can force the stream to flush from QProcess (i might be wrong) maybe you can wait for the C application to finnish (that must cause a flush) and then see what you have in that byte-array (again i don't know if QProcess still "capture" the flush if it is closing the other process)
Re: why can't QProcess read all output?
Quote:
Originally Posted by
Raul
If i set the Konsole to super Konsole(su -) ,i can get all output.but if i set it to "/bin/bash",it can't .
Then it seems like you can't get the same behaviour from your Qt app without first requesting su?
Re: why can't QProcess read all output?
Quote:
Originally Posted by
Zlatomir
@Wysota: You are right, the actual flush is "done" by the OS, but "when" is done can be controlled from your code, and the new-line character '\n' doesn't "force" the OS to flush the output stream, while the endl does.
I don't know if this is the case in all basic operating systems but at least in Unices the default buffering for streams is "line buffering", so \n has to force a flush. Anyway take a look at any decent C application and check if every printf() is followed by fflush.
Quote:
i don't think that you can force the stream to flush from QProcess (i might be wrong)
You are not wrong. What one could do is to use stddup, disable buffering of the newly created stream and set that stream as stdout of the newly created process (of course you need to spawn it yourself using proper native means). At least in Unices :)
Anyway the "problem" is obviously in behaviour of the slave program.
Re: why can't QProcess read all output?
@Wysota: I didn't said you are wrong, and you are right about line-buffer in *nix, but this isn't a behavior that you can assume it's true (especially if you want multi-platform)
Quote:
Anyway take a look at any decent C application and check if every printf() is followed by fflush.
Correct again, it is highly not recommended to force flush on every printf()/cout, because the OS "usually" does a good job and flush the buffer whenever necessary, but this isn't a standard cross-platform "thing" (in some situations you can have different behaviors on same platform), so in some cases like the one we are talking wright now it is (or should be) necessary to "force" the flush.
Re: why can't QProcess read all output?
Quote:
Originally Posted by
fatjuicymole
Then it seems like you can't get the same behaviour from your Qt app without first requesting su?
I just use QProcess start the program.and read the process.I didn't know how to request su first.if you can tell me how to request su first.