简体   繁体   English

使用 QProcess 执行 CMD 命令以将 output 作为 QString 的正确方法是什么?

[英]What is the correct way to execute a CMD command with QProcess to get the output as QString?

I'm new using QT, I need to execute a CMD command on windows and get the output of this command as a string to later process and get specific data. I'm new using QT, I need to execute a CMD command on windows and get the output of this command as a string to later process and get specific data. The following code works well (it seems to work well).以下代码运行良好(似乎运行良好)。 The only problem is that I get the following warning: "start is deprecated" , I think this warning message is because the start method needs an arguments list as parameter.唯一的问题是我收到以下警告: "start is deprecated" ,我认为这个警告消息是因为start方法需要一个arguments列表作为参数。

QString command = "tasklist /FI \"IMAGENAME eq notepad.exe\"";
QProcess *executeCommand = new QProcess();
executeCommand->start(command);
executeCommand->waitForFinished(-1);
QString output = executeCommand->readAllStandardOutput();
executeCommand->terminate();
qDebug() << commandOutput;

how can I remove this warning message?如何删除此警告消息?

Also I found in the web that I can use system() to execute a CMD command, but I'm not able to catch the output as string.我还在 web 中发现我可以使用system()执行 CMD 命令,但我无法将 output 作为字符串捕获。

Another question: which of the above options is better to achieve what I'm trying to do, the QProcess or System (if is possible to get the output as QString)?另一个问题:上述哪个选项更好地实现我正在尝试做的事情,QProcess 或系统(如果可以将 output 作为 QString)?

Thanks in advance!提前致谢!

There is an answer read QProcess output to string recommending static method QProcess::readAllStandardOutput() returning QByteArray .有一个答案读取 QProcess output 到字符串推荐 static 方法QProcess::readAllStandardOutput()返回QByteArray Then just instantiate QString from QByteArray implicitly.然后隐式地从QByteArray实例化QString

If you are working on Qt application, it is better to stay inside Qt API to keep the code more or less portable.如果您正在处理 Qt 应用程序,最好留在 Qt API 中以保持代码或多或少的可移植性。

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

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