5

I have very strange problem with QProcess and it's strange behaviour.

What i wanna get at the end is something like this (this is cmd.exe in windows 7)

C:\path_to_somewhere>cmd /c "C:\Program Files\path_to_dir\executable"

(cmd is for compatibility with show of QProcess)

So to do something like that i create this:

QProcess proc;
QString command;
QStringList attributes;

command = "c:\\windows\\system32\\cmd.exe";
QStringList << QString("/c \"C:\\Program Files\\path_to-dir\\executable"");
proc.start(command, attributes);

What i get on error output is:

Name '\"c:\Program Files\Quantum GIS Wroclaw\bin\gdalwarp.exe\"' is not recognized as
internat or external command, executable or batch file.

(it's translated by me from polish so it may be a little diffrent in english).

Seems like the \ character is not escaped in the string, leaving the \" as to characters in command. What am I doing wrong?

I've tried the

proces.start(QString) 

function with triple \"\"\" and it doesnt work either. I suppose the solution of this problem has to be sooo easy that I dont event think about it.

Jakub Chromiak
  • 593
  • 1
  • 5
  • 15

2 Answers2

3

OK I don't know if it's Qt bug but in documentation about void QProcess::start(QString, QStringList, OpenMode) it is said something like that:

Windows: Arguments that contain spaces are wrapped in quotes.

Seems like it's not true as my program uses path with space and cmd shell breaks there.

But, I found out function that is designed for systems that accept one string only arguments (so like Windows does).

It's QProcess::setNativeArguments(QString)

that accepts one QString as argument, created especially for Windows and Symbian.

So after all, if one has a problem with passing arguments in Windows (or Symbian) to system, he should try setNativeArguments(QString).

Jakub Chromiak
  • 593
  • 1
  • 5
  • 15
2

As you already noted, Qt wraps arguments containing spaces with quotes, which means that the actual command issued by QProcess will look something like that (not sure about the inner quotes):

c:\windows\system32\cmd.exe "/c \"C:\Program Files\path_to_dir\executable\""

which is not what you want: the entire string is passed to cmd including /c. Since /c and the path are two arguments, you should pass them separately to QProcess, without worrying about spaces as they will be handled automatically:

QString command = "cmd.exe";
QStringList arguments = 
    QStringList() << "/c" << "C:\\Program Files\\path_to_dir\\executable";
// note there are two arguments now, and the path is not enclosed in quotes
proc.start(command, arguments);
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137