【问题标题】:How to use QProcess::startDetached() combined with setStandardOutputFile()如何结合使用 QProcess::startDetached() 和 setStandardOutputFile()
【发布时间】:2019-02-17 07:42:53
【问题描述】:

我有一个进程应该启动另一个进程并且正在使用 QProcess::startDetached() 因为与分叉的进程没有关系。现在我试图找出一种将标准输出重定向到文件的方法。 当我将 setStandardOutputFile() 与 QProcess::startDetached() 一起使用时,重定向到文件不起作用。 虽然 setStandardOutputFile() 与 QProcess::start() 配合得很好。

我认为由于 QProcess::startDetached() 是静态方法,它可能不适用于 setStandardOutputFile() 但我在 QProcess 文档中看到了这个声明,

Only the following property setters are supported by startDetached():
setArguments()
setCreateProcessArgumentsModifier()
setStandardOutputFile()
etc.

试图理解这句话在文档中的含义。

这里是代码的要点,

void ForkProcess()
{
    QProcess processObj;
    processObj.setStandardOutputFile("/tmp/stdoutfile.txt");
    processObj.startDetached(processWithArguments);
}

这不会将标准输出重定向到文件,而如果我使用 processObj.start(processWithArguments),则重定向工作正常。

关于为什么 QProcess::startDetached() 不起作用有什么想法吗?

【问题讨论】:

  • 您在哪个平台上运行?你能提供一个MCVE吗?

标签: c++ qt


【解决方案1】:

尝试使用non-static `QProcess::startDetached member...

bool QProcess::startDetached(qint64 *pid = nullptr);

以下对我来说似乎工作正常(Suse Linux + Qt5.12.0)...

QProcess process;
process.setProgram("ls");
process.setStandardOutputFile("/tmp/stdoutfile.txt");
process.startDetached();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    相关资源
    最近更新 更多