本文介绍了在QProcess中执行shell命令.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过管道执行命令,但无法确定如何通过管道执行.我正在尝试使用shell命令

I am trying to pipe the commands and execute it, but I am not able to figure how to pipe it.I am trying to copy multiple files at once using the shell command

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

我必须实施管道来做到这一点.例如

I have to implement the piping to do that.for Eg.

QProcess sh;
sh.start("sh", QStringList() << "-c" << "ifconfig | grep inet");

sh.waitForFinished();
QByteArray output = sh.readAll();
sh.close();

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

How can I implement piping for my copy process?

推荐答案

尝试以下示例:

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()应该在阻止模式下调用,并且必须检查它是否成功.

waitForFinished() should be called in blocking mode and it must be checked if it was successful or not.

这篇关于在QProcess中执行shell命令.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 23:33