C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用C中的QProcess重定向gnome-terminal的输出大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为gui生成一个子进程,它将生成一个终端.我希望此终端(stdout和stderr)生成的数据显示在出现的窗口以及设置的日志文件中.当我直接在 shell中输入命令时,它按预期工作,但当作为启动命令提供给QProcess时,它实际上不会向文件写入任何@L_675_9@.

例如:如果用户没有安装二进制文件,则应该写入bin.log,找不到该命令.我已经为生成的QProcess设置了我的环境,使其与正在运行的父进程相同(以便它可以找到bash和其他任何东西),并在调用QProcess.start()方法之前设置了工作目录.我写了一个测试用例如下:

gnome-terminal --title 'Sub-Terminal' -e 'bash -c "foo [args to foo] |& tee foo.log"'

在Qt中,我执行以下操作:

QProcess *child = new QProcess(this);
QString   cmd   = "gnome-terminal --title 'Sub-Terminal' -e 'bash -c \"foo [args to foo] |& tee foo.log\"'";
child->setProcessEnvironment(<process environment I have created before>);
child->setWorkingDirectory(<current working dir>);
child->start(cmd);
...

生成终端但不向foo.log写入任何数据.我也尝试过以下方法

QProcess *child = new QProcess(this);
QString   prog  = "gnome-terminal"
QStringList args;
args << "-x" << "bash" << "foo" << "[foo's arguments]" << "|&" << "tee" << "foo.log";
// set the process env and working dir
child->start(prog,args);

有没有人对如何解决这个问题有任何建议?
我已经尝试使用QProcess.setStandardErrorFile(foo.log)和QProcess.setStandardOutputFile(foo.log)重定向stdout和stderr,但似乎这将从gnome-terminal本身重定向stdout(这不是任何东西).

解决方法

你必须在终端中提供要在终端中执行的整个命令作为“-e”之后QProcess的单个参数,以及将bash中执行的命令作为bash的单个参数:

args << "-e" << "bash -c 'foo [args to foo] |& tee foo.log'";

这基本上执行

bash -c 'foo [args to foo] |& tee foo.log'

在终端中,它本身执行

foo [args to foo] |& tee foo.log

在bash中.

您的猜测是正确的,读取gnome-terminal进程的输出不起作用.

大佬总结

以上是大佬教程为你收集整理的使用C中的QProcess重定向gnome-terminal的输出全部内容,希望文章能够帮你解决使用C中的QProcess重定向gnome-terminal的输出所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。