[英]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. 以下代碼運行良好(似乎運行良好)。 唯一的問題是我收到以下警告: "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 命令,但我無法將 output 作為字符串捕獲。
另一個問題:上述哪個選項更好地實現我正在嘗試做的事情,QProcess 或系統(如果可以將 output 作為 QString)?
提前致謝!
有一個答案讀取 QProcess output 到字符串推薦 static 方法QProcess::readAllStandardOutput()
返回QByteArray
。 然后隱式地從QByteArray
實例化QString
。
如果您正在處理 Qt 應用程序,最好留在 Qt API 中以保持代碼或多或少的可移植性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.