用QProcess执行CMD命令以获得QString输出的正确方法是什么



我刚开始使用QT,我需要在windows上执行一个CMD命令,并将该命令的输出作为字符串,以便稍后处理和获取特定数据。下面的代码运行良好(似乎运行良好(。唯一的问题是我得到了以下警告:"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;

如何删除此警告消息?

此外,我在web中发现,我可以使用system()执行CMD命令,但无法将输出捕获为字符串。

另一个问题是:上面的哪个选项更适合实现我想要做的事情,QProcess还是System(如果可以将输出作为QString(?

提前感谢!

有一个答案读取QProcess输出到字符串,推荐静态方法QProcess::readAllStandardOutput()返回QByteArray。然后从QByteArray隐式实例化QString

如果您正在处理Qt应用程序,最好留在Qt API内部,以使代码或多或少具有可移植性。

最新更新