asshands asshands - 9 months ago 56
Bash Question

Piping (or command chaining) with QProcess

I'm using Qt and bash over it, need to execute something like:

bash: cat file | grep string

in Qt:

QString cmd = "cat file | grep string";
QProcess *process = new QProcess;
qDebug() << process->readAll();

The problem is in pipe ("|"), and process returs nothing. If there is no ("|"), like

"cat file"

everything is ok.
I tried smth. like

"cat file \\| grep string",
"cat file \| grep string"

but result is the same. If I copy the command and run it in bash everything is ok.


and other transforms also have bad result.

Answer Source

The problem is you cannot run a system command with QProcess, but only a single process. So the workaround will be to pass your command as an argument to bash:

process.start("bash", QStringList() << "-c" << "cat file | grep string");