Qt – how to get output system() command in Qt

qprocessqtsystem

I use system() command in Qt. and I want to get output and show it to users.
my command is:

system("echo '" + rootPass.toAscii() + "' | su - root -c 'yum -y install " + packageName.toAscii() + "'");

this command can't run when I use it in QProcess (start or execute function)
but if i can run this command in QProcess i can get output with QProcess::readAllStandardOutput() function.

also when i used ">" in system command to save output in a file, I receive output when the package completely installed. like bellow:

system("echo '" + rootPass.toAscii() + "' | su - root -c 'yum -y install " + packageName.toAscii() + "' > result.out");

is there any idea about running this command with QProcess, or get output from system() command as soon as write each line.

Best Answer

You can also obtain the output directly from QProcess

QProcess process;
process.start(/* command line stuff */);
process.waitForFinished(-1); // will wait forever until finished

QString stdout = process.readAllStandardOutput();
QString stderr = process.readAllStandardError();

If you don't want to block your event loop, you can always use the signals:

readyReadStandardOutput();
readyReadStandardError();

And then call a function to readAllStandard[Output/Error]

Related Topic