PDA

View Full Version : Cannot get stdout from QProcess



johnny_sparx
3rd March 2006, 07:30
I am having trouble to retrieve the console output from an external program. Below, I modified the program so that it is similar to what I have in my program - but with the external program changed to "dir /w".



QProcess System_Call(this);
QStringList Parameters;

Parameters<<"/w";

System_Call.start("dir",Parameters);
System_Call.waitForStarted();
QString X;
X = System_Call.readAll();
System_Call.waitForFinished();
Parameters.clear();


I cannot seem to retrieve the output from this program... what am I doing wrong?:confused:

zlatko
3rd March 2006, 09:33
What about readAllStandardOutput() ?

wysota
3rd March 2006, 10:25
I think you should read after the process has finished.

zlatko
3rd March 2006, 11:24
I think you should read after the process has finished.

but if app using flush(stdout) first method can be possible?

jacek
3rd March 2006, 11:26
but if app using flush(stdout) first method can be possible?
No, because you still might try to read the data before the process has written it.

jpn
3rd March 2006, 11:44
Hmm.. this works fine on Linux (Ubuntu, Qt 4.1.1, gcc 4.0.2) but not in Windows (XP+SP2, Qt4.1.1, VS2003).



QProcess proc;
proc.start(QString("dir"));
proc.waitForFinished(-1);
qDebug() << proc.readAll();


Edit: means that it outputs correct info in linux, and nothing in windows..

seneca
3rd March 2006, 12:08
Hmm.. this works fine on Linux (Ubuntu, Qt 4.1.1, gcc 4.0.2) but not in Windows (XP+SP2, Qt4.1.1, VS2003).



QProcess proc;
proc.start(QString("dir"));
proc.waitForFinished(-1);
qDebug() << proc.readAll();


Edit: means that it outputs correct info in linux, and nothing in windows..

Is the Qt application built as console, or are you checking the dDebug output with the debugger? Otherwise you wont see any qDebug output on windows.

jpn
3rd March 2006, 12:15
Is the Qt application built as console, or are you checking the dDebug output with the debugger? Otherwise you wont see any qDebug output on windows.

With a debugger... I can see qDebug() outputs fine, that is not the problem.

wysota
3rd March 2006, 12:27
Reading all output at once, even after the application is finished is not a very good idea. For short outputs it is fine, but if you expect a large amount of data to come through that pipe, you should use a signal/slot mechanism to read the data as it comes in. Why? Because it may happen that you fill the pipe buffer between those two processes and the child process will never finish, because it'll be waiting until some of the buffer is read so that it can write all the remaining data. So you should use readyReadStdout() and family.

jpn
3rd March 2006, 12:32
My point is, that I never get any output on windows with the above code.
Even when I set the working dir so that the output is expected to be extremely short.

wysota
3rd March 2006, 12:42
My point is, that I never get any output on windows with the above code.
Even when I set the working dir so that the output is expected to be extremely short.

Because there is no "dir" executable on Windows. "dir" is a command.com (or cmd.exe) command, so you have to run it from the shell. There are threads on this forum that cover the topic, maybe you should take a look at them.

jpn
3rd March 2006, 12:46
Because there is no "dir" executable on Windows. "dir" is a command.com (or cmd.exe) command, so you have to run it from the shell. There are threads on this forum that cover the topic, maybe you should take a look at them.

Ahh, now I get the point..