C++ 读取 QProcess 输出到字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17344807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
read QProcess output to string
提问by sersem1
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.
我想将此输出作为字符串(读取)。我搜索了这个,但找不到解决方案。提前致谢。
回答by Shf
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());
回答by TheDarkKnight
@Shf is right in that you should be using readAllStandardOutput. However, you're using the function execute() which is a static method. You should be calling start( ) from an instance of a QProcess.
@Shf 是正确的,您应该使用 readAllStandardOutput。但是,您正在使用函数 execute(),它是一个静态方法。您应该从 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: -
此外,还有一个重载的 start 函数,它允许您传入整个命令,这可能会使您的代码更易于阅读:-
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 并连接到 finished() 信号,以便连接插槽读取数据。
回答by ariwez
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();
回答by Pavel Strakhov
You shouldn't use QProcess::execute
method, it's static and doesn't alter your pingProcess
variable. You have no access to a process started using this method. You need to use start()
method instead. Note that this method is asynchronous. You need to use waitForFinished
and then read the data.
你不应该使用QProcess::execute
方法,它是静态的,不会改变你的pingProcess
变量。您无权访问使用此方法启动的进程。您需要改用start()
方法。请注意,此方法是异步的。您需要使用waitForFinished
然后读取数据。
pingProcess.start(exec, params);
pingProcess.waitForFinished();
QByteArray output = pingProcess.readAllStandardOutput();