读取QProcess输出到字符串

读取QProcess输出到字符串

本文介绍了读取QProcess输出到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用QProcess这样的代码。

I have a code that uses QProcess like this.

 int main(int argc, char *argv[])
{
    int status=0;
    QProcess pingProcess;
    QString ba;
    QString exec = "snmpget";
    QStringList params;
     params << "-v" << "2c" << "-c" << "public" << "10.18.32.52" <<    ".1.3.6.1.4.1.30966.1.2.1.1.1.5.10";
    status=pingProcess.execute(exec, params);
    pingProcess.close();
}

这会输出以下结果。

  SNMPv2-SMI::enterprises.30966.1.2.1.1.1.5.10 = STRING: "0.1"

我想把这个输出作为字符串。我搜索这个,我找不到解决方案。提前感谢。

I want to take(read) this output as string. I searched for this and I cant find the solution. Thanks in advance.

推荐答案

您尝试过 QByteArray QProcess :: readAllStandardOutput() docs -

Did you try QByteArray QProcess::readAllStandardOutput() docs - here

QString可以从QByteArray实例化

QString can be instantiated from QByteArray

QString output(pingProcess.readAllStandardOutput());

正如别人所说,我加入他们,你不应该使用 execute 方法,并替换为

As others mentioned, and i join to them, you should not use execute method and replace it with

pingProcess.start(exec, params);
pingProcess.waitForFinished(); // sets current thread to sleep and waits for pingProcess end
QString output(pingProcess.readAllStandardOutput());

这篇关于读取QProcess输出到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 23:36