我正在使用Qt和bash,需要执行以下操作:
bash: cat file | grep string
Run Code Online (Sandbox Code Playgroud)
在Qt:
QString cmd = "cat file | grep string";
QProcess *process = new QProcess;
process->start(cmd);
process->waitForBytesWritten();
process->waitForFinished();
qDebug() << process->readAll();
Run Code Online (Sandbox Code Playgroud)
问题在于管道("|"),并且进程没有任何回复.如果没有("|"),就像
"cat file"
Run Code Online (Sandbox Code Playgroud)
一切都好.我试过smth.喜欢
"cat file \\| grep string",
"cat file \| grep string"
Run Code Online (Sandbox Code Playgroud)
但结果是一样的.如果我复制命令并在bash中运行它一切正常.
QString::toAscii().data()
Run Code Online (Sandbox Code Playgroud)
和其他变换也有不好的结果.
最简单的代码:
void test
{
QProcess p;
p.start("sleep 10");
p.waitForBytesWritten();
p.waitForFinished(1);
}
Run Code Online (Sandbox Code Playgroud)
当然,该过程无法在函数结束之前完成,因此它会显示一条警告消息:
QProcess: Destroyed while process ("sleep") is still running.
Run Code Online (Sandbox Code Playgroud)
我希望这条消息不被显示 - 我应该在结束函数之前自己销毁这个过程,但我找不到如何正确地做到这一点:p.~QProcess(),p.terminate(),p.kill( )无法帮助我.
注意:我不想等待进程执行,只是在运行时自己杀死它.