PDA

View Full Version : Can't get QProcess output



croscato
17th January 2010, 22:44
Hi all!

I have the following code and can't figure out why I can't get the
QProcess output:



#include <QApplication>
#include <QProcess>
#include <QDebug>

int main(int argc, char **argv)
{
QApplication application(argc, argv);

QProcess proc;

proc.start("c:\\windows\\system32\\cmd.exe", QIODevice::ReadWrite);

if (proc.waitForStarted() == false) {
qDebug() << "Error starting cmd.exe process";
qDebug() << proc.errorString();

return (-1);
}

// Show process output
qDebug() << proc.readAllStandardOutput();

proc.write("dir");

qDebug() << proc.readAllStandardOutput();

proc.close();

return 0;
}


Any ideas?

Thanks!

boudie
17th January 2010, 22:59
You need a slot connected to this signal:
void QProcess::readyReadStandardOutput ()** [signal]

croscato
17th January 2010, 23:00
I've changed the code to:



#include <QApplication>
#include <QProcess>
#include <QDebug>

int main(int argc, char **argv)
{
QApplication application(argc, argv);

QProcess proc;

proc.start("c:\\windows\\system32\\cmd.exe", QIODevice::ReadWrite);

if (proc.waitForStarted() == false) {
qDebug() << "Error starting cmd.exe process";
qDebug() << proc.errorString();

return (-1);
}

// Show process output
proc.waitForReadyRead();
qDebug() << proc.readAllStandardOutput();

proc.write("dir");

proc.waitForBytesWritten();
proc.waitForReadyRead();

qDebug() << proc.readAllStandardOutput();

proc.close();

return 0;
}


and was able to get the this output:



Starting C:\Documents and Settings\Usuario\Meus documentos\Teste\debug\Teste.exe...
"Microsoft Windows XP [versÆo 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Usuario\Meus documentos\Teste\debug>"
""


but the dir command output doesn't came out.

squidge
18th January 2010, 00:05
Depending on what you trying to do, you may wish to pass "/c dir" to cmd.exe as a parameter, which will give you the dir output.

croscato
18th January 2010, 16:27
fatjuicymole

Even with the "/c" parameter I can't get the dir output.

My intention is to send commands to the cmd.exe process and receive it's output.

There is a way of doing this?

Thanks....

mattc
18th January 2010, 16:45
It looks like you are getting an incomplete output from the process... maybe calling QProcess::waitForFinished() before QProcess::readAllStandardOutput can solve the problem.

SeN
18th November 2010, 16:56
Hi - I'm facing the a similar problem with Qt 4.7.1, running on Windows XP:

QString program = "cmd.exe";
QProcess *myProcess = new QProcess();
myProcess->start(program);
if (myProcess->waitForStarted(1000) == false)
qDebug() << "Error starting external program";
else
qDebug() << "external program running";

myProcess->waitForReadyRead(100);
myProcess->waitForFinished(100);
qDebug() << "read output" << myProcess->readAllStandardOutput();
/* At this point I get output:
read output "Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Users\myfolder\mysubfolder>"
*/
myProcess->write("dir");
myProcess->waitForBytesWritten(100);
myProcess->waitForFinished(100);
myProcess->waitForReadyRead(100);
qDebug() << "read output" << myProcess->readAllStandardOutput();

/*I get nothing here:
read output ""
*/

Is it actually possible to write to a QProcess on Windows? Is it mandatory to connect to the signals?

Thanks

Added after 8 minutes:

I found the solution, need to make use of myProcess->write("dir \n"); and myProcess->closeWriteChannel();

the following code works:
QString program = "cmd.exe";
QStringList arguments;
arguments << "-v";

QProcess *myProcess = new QProcess();
//myProcess->start(program, arguments);
myProcess->start(program);


if (myProcess->waitForStarted(1000) == false)
qDebug() << "Error starting external program";

else
qDebug() << "external program running";

myProcess->waitForReadyRead(100);
myProcess->waitForFinished(100);
qDebug() << "read output" << myProcess->readAllStandardOutput();

myProcess->write("dir \n");
myProcess->closeWriteChannel();
myProcess->waitForBytesWritten(100);
myProcess->waitForReadyRead(100);
myProcess->waitForFinished(100);
qDebug() << "read output" << myProcess->readAllStandardOutput();