QProcess中執行一個單一的過程。 。你所要做的是執行shell命令,不是一個過程命令的管道是你的shell的一個特點
有三種可能的解決方案:
把comman d。您想作爲參數傳遞給sh
後-c
執行(「命令」):
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 | grep
ifconfig.start("ifconfig");
grep.start("grep", QStringList() << "inet"); // pass arguments using QStringList
grep.waitForFinished(); // grep finishes after ifconfig does
QByteArray output = grep.readAll(); // now the output is found in the 2nd process
ifconfig.close();
grep.close();
您需要指定ifconifg的完整路徑。你的應用程序有不同的PATH變量,那麼你的終端 –
@KamilKlimek由於這可能是單個命令的問題,在這種情況下,命令的管道(QProcess不支持)是真正的問題。 – leemes
對!總是忘了那一個 –