读取 Qt 中连续 QProcess 的标准输出
Read stdoutput of continuous QProcess in Qt
我在使用 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上提供的一些解决方案,但无法处理这种情况。任何指导将不胜感激。
我建议你设置一个信号处理程序,当子进程产生输出时被调用。 例如,您必须连接到readyReadStandardOutput
。
然后,您可以识别子流程何时需要输入并发送所需的输入。这将在readSubProcess()
中完成。
主.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
程序完成时,主程序也会退出。
福.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("typen")) {
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();
}
};
};
对于子过程计算器,使用一个简单的脚本。您可以看到生成输出的位置和预期输入的位置。
#/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
这样。
相关文章:
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 使用提升过程获取 shell 命令的标准输出
- 将标准输出重定向到ostream
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- 在程序执行期间从标准输出重定向到自定义流
- 标准输出C++意外行为
- 防止 qDebug() 写入标准输出
- C++ - <<运算符重载,链表 - 地址而不是标准输出
- 有没有办法知道C++中将打印到标准输出的大小?
- 最后推迟标准输出
- 得到~对标准输出没有回应~在黑客排名问题上
- Win32 GUI C(++) 应用将标准输出和标准输出重定向到磁盘上的同一文件
- 在标准输出中执行打印会导致数组中随机分配值
- 捕获远程进程子进程的标准输出
- 使用 QProcess 读取标准输出
- 如何将值发送到标准输出流并在另一个程序中访问这些值
- 读取 Qt 中连续 QProcess 的标准输出
- QProcess -> 从 Linux 上的 sftp 读取标准输出
- 如何从调用QProcess的程序中获得返回的标准输出
- 如何从分离的QProcess中读取标准输出/标准错误