Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to parse an command line argument string into a QStringList for QProcess ?
I have a command line string that I want to run with
QProcess, it was used to be like this:
QString executable = "x264.exe"; QString arguments= " -i xxxxx -o test.mp4"; QString commandline = executable + arguments; process->start(commandline);
But now qt warns about this syntax being deprecated, so how do update this and turn the arguments string into
JonB last edited by JonB
In this case you would be far better generating the list of separate arguments which
QProcess::start()wants, rather than making a string as you do now.
QString executable = "x264.exe"; QStringList arguments = (QStringList() << "-i" << "xxxx" << "-o" << "test.mp4"); process->start(executable, arguments);
The command line will be entered by the users.
And how does the user enter them ?
I mean via some GUI or via actual command line parameters to your app`?
I have a gui with a textarea for the entry
ok so user type one parameter pr line or does it come
like par1=1 par2=2 on same line ?
So we need to find way to split it into elements and
often we can split on space or or new line.
The commandline will be entered by the user in one line, without the executable at the start of the line.
-i xxxxx -o "D:\my test.mp4"
I tried splitting it by space but it failed when I included a file path.
Why not just use multiple LineEdits?
Input: [ ] output:[ ]
and then add -i and -o yourself when you build the QStringList
lansing last edited by lansing
I think you're right, separating input/output file path from the argument string will resolve headache like this. Then the argument string can be easily splitted by space and put into a
There's also QCommandLineParser class that can take care of all the spacing, quoting and parsing problems for you.
JonB last edited by JonB
The commandline will be entered by the user in one line
Then instead of using the deprecated
QProcess::start()which accepted a string you will want to use the new [static]QStringList QProcess::splitCommand(QStringView command), which effectively exposes the function to do the splitting into arguments that the old
start()used to offer.
Thanks it worked like magic.
QString executable = "x264.exe"; QString arguments = " -i xxxxx -o test.mp4"; QStringList argumentsList = QProcess::splitCommand(arguments); process->start(executable, argumentsList );