问题描述
我必须在运行时获取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完成,该过程启动一个应该在后台永久运行的自动化接口。因此,我使用readyReadStandardOutput并将其连接到插槽CheckOutput()。 CheckOutput()正在进程被调用,如果进程已经完成。否则我在等待无休止。
我已经搜索了很多关于这个问题,但没有什么工作。我非常确定输出是缓冲,只是返回如果进程已经完成。因此我已经启动了处于非缓冲模式的进程。我也试图转发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。方案
我通常以如下的定期检查输出:
bool returnBool = false;
while(returnBool = false)
{
/ *!如果进程完成,请等待一秒钟。然后读取所有输出到
* stdout和stderr和重做。 * /
returnBool = process.waitForFinished(1000);
QString outputStdOut = process.readAllStandardOutput();
QString outputStdErr = process.readAllStandardError();
}
I have to get the output of a QProcess while it is running. Therefore I have written the following Code:
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);
}
}
This works fine if QProcess gets finished but in my case the Process starts an automation interface which should run in background permanently. Therefore I have used "readyReadStandardOutput" and connect it to the slot CheckOutput(). CheckOutput() is getting called just if the process has been finished. Otherwise I am waiting endless.
I have googled a lot about the problem but nothing worked. I am very sure that the output is getting buffered and does just return if the Process has finished. Therefore I have started the Process in Unbuffered-Mode. I have also tried to forward the channels of mTdiProcess. Here the Code:
void CommandExecutor_C::ExecuteCommand(QString &aCommand)
{
mTdiProcess->setProcessChannelMode(QProcess::ForwardedChannels);
mTdiProcess->start(aCommand, QProcess::Unbuffered | QProcess::ReadWrite);
LOGINFO(FB_TDI,"Launch command: " + aCommand.toStdString());
}
But nothing worked. I hope you can help me.
I am using Qt 5.4.2 if that's important.
I usually check the output in regular intervals like this:
bool returnBool = false;
while (returnBool == false)
{
/*! Wait one second if the process finishes. Then read all output to
* stdout and stderr and redo. */
returnBool = process.waitForFinished(1000);
QString outputStdOut = process.readAllStandardOutput();
QString outputStdErr = process.readAllStandardError();
}
这篇关于Qt:如何获取正在运行的QProcess的实时输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!