0

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.

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.

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)?

Thanks in advance!

BadRobot
  • 265
  • 1
  • 9
  • Does this answer your question? [read QProcess output to string](https://stackoverflow.com/questions/17344807/read-qprocess-output-to-string) – Andrei Vukolov Sep 13 '22 at 17:19
  • No, sorry, I need to suppress the warning message, I think I'm using the `start` method in the wrong way. – BadRobot Sep 13 '22 at 17:22
  • 1
    @BadRobot Try separating arguments from the command and passing them as an optional `QStringList` parameter to [QProcess::start](https://doc.qt.io/qt-6/qprocess.html#start) either manually like `executeCommand->start("tasklist", {"/FI", "IMAGENAME eq notepad.exe"});` or use [QProcess::splitCommand](https://doc.qt.io/qt-6/qprocess.html#splitCommand) to generate the argument list (excluding the command itself, of course). – absolute.madness Sep 13 '22 at 18:40
  • @absolute.madness. Hi, a few minutes ago before you answer me, I try with a line of code similar than yours and works perfect. Thanks a lot! – BadRobot Sep 13 '22 at 18:44
  • 1
    @BadRobot also you don't need to call `QProcess::terminate`. Especially after using `waitForFinished` but even without it the child process will be killed by [QProcess::~QProcess](https://doc.qt.io/qt-6/qprocess.html#dtor.QProcess) when needed. – absolute.madness Sep 13 '22 at 18:51
  • @absolute.madness, thanks I'll remove that code line. – BadRobot Sep 13 '22 at 18:56

1 Answers1

1

There is an answer read QProcess output to string recommending static method QProcess::readAllStandardOutput() returning QByteArray. Then just instantiate QString from QByteArray implicitly.

If you are working on Qt application, it is better to stay inside Qt API to keep the code more or less portable.

Andrei Vukolov
  • 118
  • 1
  • 10
  • Hi Andrei, yes, in fact with the code above I can obtain the output of the CMD command and works well. I'm using QProcess to execute the command, but the only issue is I get a warning message: `start` is deprecated – BadRobot Sep 13 '22 at 17:21
  • Hi! There is an answer pointing out this problem. https://stackoverflow.com/a/69165867/9560245 The only deprecation is declared for methods accepting full command strings without additional mode selectors. – Andrei Vukolov Sep 14 '22 at 08:51