我必须在QProcess运行时获得它的输出。因此我写了以下代码:
CommandExecutor_C::CommandExecutor_C():
mProcessStatus(AI_UNKNOWN),
mOnTdiActiveCallback(),
mTdiProcess(new QProcess)
{
connect(mTdiProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(CheckOutput()));
connect(mTdiProcess, SIGNAL(readyReadStandardError()), this, SLOT(CheckOutput()));
}
void CommandExecutor_C::ExecuteCommand(QString &aCommand)
{
mTdiProcess->start(aCommand, QProcess::Unbuffered | QProcess::ReadWrite);
LOGINFO(FB_TDI,"Launch command: " + aCommand.toStdString());
}
void CommandExecutor_C::CheckOutput()
{
QString StdOut = QString(mTdiProcess->readAllStandardOutput());
QString StdErr = QString(mTdiProcess->readAllStandardError());
mProcessStatus = CheckTdiAutomationInterface(StdOut.toStdString(), StdErr.toStdString());
if(mProcessStatus != AI_UNKNOWN)
{
OnTdiActive(mProcessStatus);
}
}
如果QProcess完成,这可以正常工作但在我的情况下,Process启动一个应该永久在后台运行的自动化接口。因此我使用了" readyReadStandardOutput"并将其连接到插槽CheckOutput()。只有在进程完成后才会调用CheckOutput()。否则我会无休止地等待。
我已经搜索了很多关于这个问题但没有任何效果。我非常确定输出是缓冲的,如果Process已经完成则返回。因此,我已经在无缓冲模式下启动了进程。我也尝试转发mTdiProcess的通道。代码:
void CommandExecutor_C::ExecuteCommand(QString &aCommand)
{
mTdiProcess->setProcessChannelMode(QProcess::ForwardedChannels);
mTdiProcess->start(aCommand, QProcess::Unbuffered | QProcess::ReadWrite);
LOGINFO(FB_TDI,"Launch command: " + aCommand.toStdString());
}
但没有任何效果。我希望你能帮助我。
我正在使用Qt 5.4.2,如果这很重要。