本文介绍了如何从 QProcess 获取 STDOUT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以为我将使用以下代码从 QProcess 获取输出:
I thought I was going to get the output from a QProcess using the following code:
// Start the process
process.start(tr("php-cgi www/test.php"),QIODevice::ReadWrite);
// Wait for it to start
if(!process.waitForStarted())
return 0;
// Continue reading the data until EOF reached
QByteArray data;
while(process.waitForReadyRead())
data.append(process.readAll());
// Output the data
qDebug(data.data());
qDebug("Done!");
我期望看到程序的输出打印到调试控制台,但我看到的是:
What I am expecting is to see the output from the program printed to the debug console, but all I see is:
完成!
我知道:
- 程序启动正常,因为打印了最后的消息.
- 程序确实会打印输出,因为在终端中运行完全相同的命令会按预期生成一长串文本.
- The program is started fine, because the message at the end is printed.
- The program does print output because running the exact same command in the terminal produces a long string of text as expected.
我在这里做错了什么?
推荐答案
在开始流程调用之前:
process.setProcessChannelMode(QProcess::MergedChannels);
它会导致将所有内容(甚至 STDERR 输出)打印到 STDOUT 输出.
It will cause printing everything (even STDERR output) to STDOUT output.
这篇关于如何从 QProcess 获取 STDOUT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!