在 QProcess 中执行 shell 命令

Executing the shell command in QProcess.Piping the input

本文关键字:shell 命令 执行 QProcess      更新时间:2023-10-16

我正在尝试管道命令并执行它,但我无法弄清楚如何管道它。 我正在尝试使用 shell 命令一次复制多个文件

对于 %I(源(复制 %I(目标(

QString files = "for %I in (source) do copy %I (destination)"
QProcess copy ;
copy.start(files);

我必须实现管道才能做到这一点。 例如。

QProcess sh;
sh.start("sh", QStringList() << "-c" << "ifconfig | grep inet");
sh.waitForFinished();
QByteArray output = sh.readAll();
sh.close();

如何为复制过程实现管道?

试试这个例子:

QProcess sh;
sh.start( "sh", { "-c", "ifconfig | grep inet" } );
if ( !sh.waitForFinished( -1 ) )
{
qDebug() << "Error:" << sh.readAllStandardError();
return -1;
}
const auto output = sh.readAllStandardOutput();
// ...

waitForFinished()应该在阻止模式下调用,并且必须检查它是否成功。