QT5:如何获取过程输出
#include <iostream>
#include <QProcess>
int main(int argc, char *argv[])
{
QProcess proc;
proc.setProgram("/home/weber/exp/qt5/build_bsptrans/bsptrans");
proc.setCurrentReadChannel(QProcess::StandardOutput);
proc.start();
if(!proc.waitForStarted())
{
std::clog << "Error start:" << proc.errorString().toStdString() << std::endl;
return -1;
}
char buffer[128];
qint64 length=proc.read(buffer,128);
int i=0;
while(length >=0 && i<10)
{
std::cout << length << std::endl;
i++;
length=proc.read(buffer,128);
}
}
BSPTRANS是一个简单的程序,它在Stdout上写数字。上面的代码只需启动该过程,然后尝试读取Stdout。
proc.Read()
长度的结果总是0。为什么?
该行为是相同的(Linux QT5.9,Windows QT5.12。)
如何用QT5编写程序M,该程序可以解释Stdout的输出? 目标是一个长期运行的程序将返回Stdout的百分比进度,而调用QT-Programm在Qprogressbar上显示了进度。 上面的代码仅行使处理Qprocess
非常感谢 鲁道夫
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
非常感谢您的有趣答案。看来,Readline正在投票。正如Guitarpicva所写的那样,使用信号和插槽是很好的:
sourcefile:
问候
鲁道夫
Thank you very much for the interesting answers. It seems, that readline is polling. As guitarpicva writes, it is good to use the signal and slots:
The sourcefile:
Greetings
Rudolf
首先使用QTCreator使其更容易,尽管还有更多文件。从QT Creator的向导启动“控制台项目”,使创建所有适当的文件的简短工作。这样,在添加主类时(在此处展示)时,重要的是要亚班级qobject,因此您可以使用信号和插槽!
首先,showit.pro文件,该文件有助于使用qmake构建makefile。该文件是由QT Creator创建的100%样板。
然后是标题:
利用QT对象模型以及信号和插槽的CPP类:
然后main.cpp将其全部踢开!
Starting by using QtCreator makes this a bit easier, though a few more files. Starting a "Console project" from Qt Creator's wizard makes short work of creating all of the appropriate files. In doing so, it's important to subclass QObject when adding the main class (showit here), so you may use signals and slots!
First the showit.pro file which helps build the Makefile using qmake. This file is 100% boilerplate created by Qt Creator.
Then the header:
The the CPP class which leverages the Qt Object model and signals and slots:
And then main.cpp to kick it all off!
我认为您的问题是,当您尝试从Stdout阅读时,该过程尚未写入任何数据。
关键是
Qiodevice :: Read
如果没有可用数据,则立即返回。我建议您使用 在
上
循环,然后qiodevice :: read
再次在周期中,而数据可用于读取或qiodevice: :readall
。但是您应该注意,Qiodevice :: waitforreadyread(-1)
因此,如果您想读取数据,请在GUI中显示它们,并且该应用程序应负责,则应在单独的线程中实现Process的输出的读数。
另一个解决方案是创建一个插槽来处理 您可以在其中实现相同的数据读取算法的信号。
现在,您可以选择最合格的方式。
I think that your problem is that the process have not written any data at that moment, when you try to read it from stdout.
The point is that
QIODevice::read
method returns immediately if no data available.I suggest you to use
QIODevice::waitForReadyRead(-1)
on awhile
cycle and thenQIODevice::read
in a cycle again, while data is available for reading, orQIODevice::readAll
. But you should note thatQIODevice::waitForReadyRead(-1)
So if you would like to read data, show it in GUI and the app should be responsible, you should implement the reading of process's output in a separate thread.
Another solution is to create a slot to handle
QIODevice::readyRead
signal where you can implement the same data reading algorithm.Now it's up to you to choose the most eligible way.