简体   繁体   English

将QProcess输出读取为字符串

[英]read QProcess output to string

I have a code that uses QProcess like this. 我有一个使用QProcess这样的代码。

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();
}

This outputs the following. 这将输出以下内容。

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. 提前致谢。

Did you try QByteArray QProcess::readAllStandardOutput() docs - here 您是否尝试过QByteArray QProcess::readAllStandardOutput() 这里

QString can be instantiated from QByteArray : QString可以从QByteArray实例化:

QString output(pingProcess.readAllStandardOutput());

As others mentioned, and I join to them, you should not use execute method and replace it with: 正如其他人提到的,并且我加入了他们,您不应使用execute方法并将其替换为:

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

@Shf is right in that you should be using readAllStandardOutput. @Shf是正确的,因为您应该使用readAllStandardOutput。 However, you're using the function execute() which is a static method. 但是,您使用的是函数execute(),这是一个静态方法。 You should be calling start( ) from an instance of a QProcess. 您应该从QProcess实例调用start()。

It may also be a good idea to then either wait for the data with waitForReadyRead, or just wait for the process to finish with waitForFinished( ). 然后,通过waitForReadyRead等待数据,或者仅通过waitForFinished()等待过程完成,这也是一个好主意。

Also, there's an overloaded start function, which allows you to pass the whole command in, which may make your code easier to read: - 另外,还有一个重载的启动函数,它使您可以传递整个命令,这可能使您的代码更易于阅读:-

QProcess pingProcess;
QString exe = "snmpget -v 2c -c public 10.18.32.52 .1.3.6.1.4.1.30966.1.2.1.1.1.5.10";
pingProcess.start(exe);
pingProcess.waitForFinished();
QString output(pingProcess.readAllOutput());

Note that calling waitForFinished will hang the current process, so if you're going to do something that will take a while, you would then want to dynamically create the QProcess and connect to the finished() signal in order for a connected slot to then read the data. 请注意,调用waitForFinished将挂起当前进程,因此,如果您要花一些时间,则需要动态创建QProcess并连接到finish()信号,以便连接的插槽可以读取数据。

In a more Qt way you can try to use readyReadStandardOutput signal: 以更Qt的方式,您可以尝试使用readyReadStandardOutput信号:

connect(&pingProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readData()));

and in corresponding slot readData to the string 并在相应的插槽readData中到字符串

QString output = pingProcess.readAllStandardOutput(); QString输出= pingProcess.readAllStandardOutput();

You shouldn't use QProcess::execute method, it's static and doesn't alter your pingProcess variable. 您不应该使用QProcess::execute方法,它是静态的,并且不会更改pingProcess变量。 You have no access to a process started using this method. 您无权访问使用此方法启动的进程。 You need to use start() method instead. 您需要改用start()方法。 Note that this method is asynchronous. 请注意,此方法是异步的。 You need to use waitForFinished and then read the data. 您需要使用waitForFinished ,然后读取数据。

pingProcess.start(exec, params);
pingProcess.waitForFinished();
QByteArray output = pingProcess.readAllStandardOutput();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM