calling Qprocess with arguments containing spaces - Windows

helplessKirk picture helplessKirk · Aug 27, 2015 · Viewed 8.2k times · Source

I am trying to call an executable with qprocess and pass some arguments which might (and most probably will) contain spaces (not all of them). The executable is a python script that has been packaged with Py2exe. The python script uses optparse to parse the arguments.

If I call the py2exe.exe in cmd.exe the call is like this:

pythonExecutable.exe -aarg_a -barg_b -c"path with spaces" -darg_d

A call like this will be successful.

I want to do this through a Qt application using Qprocess, but I can't find a way to do it because Qprocess will strip any quotes("") leaving the arguments broken wherever spaces appear.

I seem to be missing something, can anyone help with this issue?

Answer

Midhun picture Midhun · Aug 27, 2015

that won't be much of an issue if u use the QProcess in a more proper way

QString program = "pythonExecutable.exe";
QStringList arguments;
arguments <<"-aarg_a"<< "-barg_b"<< "-c\"path with spaces\""<< "-darg_d";

QProcess *myProcess = new QProcess(parent);
myProcess->start(program, arguments);

normaly when u have arguments with space and do't need a " symbol

you just have to pass the argument in a QStringList

QString program = "pythonExecutable.exe";
QStringList arguments;
arguments <<"a"<< "path with spaces";

QProcess *myProcess = new QProcess(parent);
myProcess->start(program, arguments);

this program is a modified version of example program listed in the Qt docs Here