如何重定向流程标准输出

How to Redirct QProcess StandardOutput

我正在使用 QProcess 的两个实例。在我的代码的某些部分,我需要 process1 将其输出重定向到 process2.
通过遵循 Qt 文档,我可以使用 QProcess.setStandardOutput(),这很棒。但是,稍后我必须在代码中禁用它。 如何将其输出重定向到默认行为 (stdout)?我试过process1 = QProcess(),但没用。

process1 = QProcess()
process2 = QProcess()
process1.setStandardOutputProcess(process2)
process1.start("command1")
process2.start("command2")

process1.setStandardOutputProcess(QProcess())
可行,但这是重新划分的正确方法吗?或者我正在创建一个新的“uneccessry”对象?

Qt 错误跟踪器上将其报告为错误后。

我收到了以下回复:

That is a limitation of Qt, QProcess::setStandardOutputProcess(Qprocess *destination) expects a non-null pointer; it is not meant to be changed at runtime.

我解决这个问题的方法是使用 process1.setStandardOutputProcess(QProcess()).

编辑
我询问是否有解决方法并收到以下回复

Is there a recommended workaround?

他回复了

No. It is not designed to allow for a change of device (since there are most likely I/O buffers involved). Similarly, when doing something like

ls -l | fgrep "bla"

in the shell, there is no way the change the 2nd process in mid-flight, either.