QProcess执行一个进程。您要执行的操作是执行 Shell命令 ,而不是进程。命令管道是Shell的功能。
有三种可能的解决方案:
将要执行的命令作为参数放入
shafter
-c(“ command”):
QProcess sh;sh.start("sh", QStringList() << "-c" << "ifconfig | grep inet");sh.waitForFinished();QByteArray output = sh.readAll();sh.close();或者,您可以将命令作为标准输入编写到
sh:
QProcess sh;sh.start("sh");sh.write("ifconfig | grep inet");sh.closeWriteChannel();sh.waitForFinished();QByteArray output = sh.readAll();sh.close();避免的另一种方法
sh是启动两个QProcesses并在代码中进行管道处理:
QProcess ifconfig;QProcess grep;ifconfig.setStandardOutputProcess(&grep); // "simulates" ifconfig | grepifconfig.start("ifconfig");grep.start("grep", QStringList() << "inet"); // pass arguments using QStringListgrep.waitForFinished(); // grep finishes after ifconfig doesQByteArray output = grep.readAll(); // now the output is found in the 2nd processifconfig.close();grep.close();


