这个问题困扰着我,因为它应该可以工作,但是可悲的是它没有。我尝试实现的目标是读取特定进程的标准输出,并使另一个进程处理它,即将其打印出来。
产生输出的进程如下所示:
该过程可以在另一个应用程序中以如下方式启动:
产生输出的进程如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int main() {
for (int i = 0; i < 100; i++) {
printf("yes %d\n",i);
fflush(stdout);
sleep(1);
}
return 0;
}
该过程可以在另一个应用程序中以如下方式启动:
#include <QProcess>
...
QProcess * process = new QProcess;
SomeClass * someClass = new SomeClass(process);
connect(process,SIGNAL(readyRead()),someClass,SLOT(onReadyRead()));
process->start("../Test/Test",QStringList());
if (!process->waitForStarted(4000)) {
qDebug() << "Process did not start.";
}
...
void SomeClass::onReadyRead() {
qDebug() << "Reading:" << process->readAllStdOutput();
}
我期望的输出结果是:
Reading: yes 0
Reading: yes 1
...
Reading: yes 99
然而,我根本没有任何输出。当我使用QCoreApplication时,我可以获得所有输出,但不是通过信号/槽,而是直接在控制台中。
我不明白,因为它在另一个使用Qt 4.8的应用程序中运行。
我的问题是,是否有人遇到了同样的问题,或者是否有人知道如何获得期望的行为?