如何在 Qt 中获取输出 system() 命令? [英] how to get output system() command in Qt?

查看:63
本文介绍了如何在 Qt 中获取输出 system() 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Qt 中使用 system() 命令.我想获得输出并将其显示给用户.我的命令是:

I use system() command in Qt. and I want to get output and show it to users. my command is:

system("echo '" + rootPass.toAscii() + "' | su - root -c 'yum -y install " + packageName.toAscii() + "'");

这个命令在 QProcess 中使用时无法运行(启动或执行函数)但是如果我可以在 QProcess 中运行这个命令,我可以使用 QProcess::readAllStandardOutput() 函数获得输出.

this command can't run when I use it in QProcess (start or execute function) but if i can run this command in QProcess i can get output with QProcess::readAllStandardOutput() function.

此外,当我在系统命令中使用>"将输出保存在文件中时,我会在软件包完全安装后收到输出.如下图:

also when i used ">" in system command to save output in a file, I receive output when the package completely installed. like bellow:

system("echo '" + rootPass.toAscii() + "' | su - root -c 'yum -y install " + packageName.toAscii() + "' > result.out");

是否有关于使用 QProcess 运行此命令的想法,或者在写入每一行后立即从 system() 命令获取输出.

is there any idea about running this command with QProcess, or get output from system() command as soon as write each line.

推荐答案

您也可以直接从 QProcess

You can also obtain the output directly from QProcess

QProcess process;
process.start(/* command line stuff */);
process.waitForFinished(-1); // will wait forever until finished

QString stdout = process.readAllStandardOutput();
QString stderr = process.readAllStandardError();

如果你不想阻塞你的事件循环,你可以随时使用信号:

If you don't want to block your event loop, you can always use the signals:

readyReadStandardOutput();
readyReadStandardError();

然后调用一个函数来readAllStandard[Output/Error]

这篇关于如何在 Qt 中获取输出 system() 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆