【问题标题】:Read stdoutput of continuous QProcess in Qt读取 Qt 中连续 QProcess 的标准输出
【发布时间】:2018-03-08 01:24:19
【问题描述】:

我在使用 Qt 的 QProcess 方面遇到了一些问题。我已将以下功能与按钮的 onClick 事件连接起来。基本上,我想在单击此按钮时执行另一个文件,并在我的 Qt 程序上获取它的输出。这个文件calculator 执行,显示一些输出,然后等待用户的输入。

void runPushButtonClicked() {
    QProcess myprocess;
    myprocess.start("./calculator")
    myprocess.waitForFinished();
    QString outputData= myprocess.readStandardOutput();
    qDebug() << outputData;
}

在一个场景中,当calculator 是这样一个只输出一些结果并最终终止的文件时,这很完美。但是,如果计算器在输出一些结果后等待用户进一步输入,我的outputData 将一无所获。事实上,waitForFinished() 会超时,但即使我删除了waitForFinished()outputData 仍然是空的。

我已经在 SO 上尝试了一些可用的解决方案,但无法处理这种情况。任何指导将不胜感激。

【问题讨论】:

  • 我已经尝试了一些解决方案显示您尝试过的内容。
  • 通过Signal和Slot的连接:connect(process,SIGNAL(readyRead()),this,SLOT(readStdOut()));

标签: c++ qt console output qprocess


【解决方案1】:

我建议您设置一个信号处理程序,当子进程产生输出时调用它。例如。你必须连接到readyReadStandardOutput

然后您可以识别子流程何时需要输入并发送您想要的输入。这将在readSubProcess() 中完成。

ma​​in.cpp

#include <QtCore>
#include "Foo.h"

int main(int argc, char **argv) {
   QCoreApplication app(argc, argv);

   Foo foo;

   qDebug() << "Starting main loop";
   app.exec();
}

接下来,启动子进程并检查输入。当calculator 程序结束时,main 也退出。

Foo.h

#include <QtCore>

class Foo : public QObject {
   Q_OBJECT
   QProcess myprocess;
   QString output;

public:
   Foo() : QObject() {
      myprocess.start("./calculator");

      // probably nothing here yet
      qDebug() << "Output right after start:"
               << myprocess.readAllStandardOutput();

      // get informed when data is ready
      connect(&myprocess, SIGNAL(readyReadStandardOutput()),
              this, SLOT(readSubProcess()));
   };

private slots:
   // here we check what was received (called everytime new data is readable)
   void readSubProcess(void) {
      output.append(myprocess.readAllStandardOutput());
      qDebug() << "complete output: " << output;

      // check if input is expected
      if (output.endsWith("type\n")) {
         qDebug() << "ready to receive input";

         // write something to subprocess, if the user has provided input,
         // you need to (read it and) forward it here.
         myprocess.write("hallo back!\n");
         // reset outputbuffer
         output = "";
      }

      // subprocess indicates it finished
      if (output.endsWith("Bye!\n")) {
         // wait for subprocess and exit
         myprocess.waitForFinished();
         QCoreApplication::exit();
      }
   };
};

对于子进程calculator,使用了一个简单的脚本。您可以看到生成输出的位置以及预期的输入位置。

#/bin/bash

echo "Sub: Im calculator!"

# some processing here with occasionally feedback
sleep 3
echo "Sub: hallo"

sleep 1

echo "Sub: type"
# here the script blocks until some input with '\n' at the end comes via stdin
read BAR

# just echo what we got from input
echo "Sub: you typed: ${BAR}"

sleep 1
echo "Sub: Bye!"

如果您不需要在主进程中执行任何其他操作(例如,显示 GUI、管理其他线程/进程......),最简单的方法是在子进程创建后循环中的 sleep然后是readSubprocess

【讨论】:

  • 感谢您的宝贵时间。答案对我不起作用,因为“ qDebug()
  • 当我使用执行而不是启动它工作但在终端而不是在控制台输出中运行