![93b4e07a9c9fb661eb10887efea81ee7.png](https://img-blog.csdnimg.cn/img_convert/93b4e07a9c9fb661eb10887efea81ee7.png)
大多数情况下我们使用QProcess都是直接等待命令执行完成返回结果,但是有些情况下需要在获取命令运行中的输出。那该怎么做呢?先上代码。
class Process : public QObject
{
Q_OBJECT
public:
Process()
{
connect(&m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(onReadData()));
m_process.setReadChannel(QProcess::StandardOutput);
m_process.start("cmd /c ping /t www.qt.io");
}
private slots:
void onReadData()
{
qDebug() << m_process.readAllStandardOutput();
}
private:
QProcess m_process;
};
例子中关键的操作是设置setReadChannel
与绑定readyReadStandardOutput
信号(用于读数据通知)。
![473dfc04555c5643f68f446a9602c035.png](https://img-blog.csdnimg.cn/img_convert/473dfc04555c5643f68f446a9602c035.png)
![6901108d630c16ce1a05898d6dc14545.png](https://img-blog.csdnimg.cn/img_convert/6901108d630c16ce1a05898d6dc14545.png)
由于QProcess继承于QIODevice类,可以使用read
,readAll
,readLine
等接口。