Alan Alan - 2 years ago 137
C++ Question

Monitor running qprocess and return value when qprocess is finished

I want to run a qprocess (the program adb) and when the process is finished return the results to the calling function. However, there's every possibility that adb could find itself in a loop, printing error messages such as "ADB server didn't ACK" to stdout, not ever finishing. I need to trap these errors.

QProcess run_command;
connect(&run_command,SIGNAL(readyReadStandardOutput()),this,SLOT( dolog() ));
QString result=RunProcess("adb connect");


QString MainWindow::RunProcess(QString cstring)


// keep gui active for lengthy processes.

while(run_command.state() != QProcess::NotRunning)

QString command=run_command.readAll();
return command; // returns nothing if slot is enabled.

void MainWindow::dolog()
QString logstring = run_command.readAllStandardOutput();
if (logstring.contains("error condition")


If I enable the signal/slot, dolog() prints stdout to a logfile, but RunProcess returns a blank string. If I disable the signal/slot, RunProcess() returns the qprocess output as expected.

Answer Source

First you need to identify which output stream the command in question is using for its errors. It is very like stderr so you would need to connect to the readyReadStandardError() signal instead.

For the command itself I would recommend to split it into command and arguments and use the QProcess::start() overload that takes the command and a list of arguments. Just more robust than to rely on a single string being correctly separated again.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download