QProcess 和命令行“/c”参数

Jak*_*iak 5 qstring escaping qprocess

我对 QProcess 有一个非常奇怪的问题,它的行为很奇怪。

我最后想要得到的是这样的东西(这是Windows 7中的cmd.exe)

C:\path_to_somewhere>cmd /c "C:\Program Files\path_to_dir\executable"
Run Code Online (Sandbox Code Playgroud)

(cmd是为了兼容QProcess的显示)

所以为了做类似的事情,我创建了这个:

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);
Run Code Online (Sandbox Code Playgroud)

我得到的错误输出是:

Name '\"c:\Program Files\Quantum GIS Wroclaw\bin\gdalwarp.exe\"' is not recognized as
internat or external command, executable or batch file.
Run Code Online (Sandbox Code Playgroud)

(它是我从波兰语翻译过来的,所以英语可能有点不同)。

似乎 \ 字符在字符串中没有转义,而将 \" 保留为命令中的字符。我做错了什么?

我已经尝试过

proces.start(QString) 
Run Code Online (Sandbox Code Playgroud)

函数与三重“\”\”,它也不起作用。我想这个问题的解决方案必须非常简单,我什至都没有想到它。

Luc*_*lle 2

正如您已经指出的,Qt 将包含空格的参数用引号括起来,这意味着发出的实际命令QProcess将类似于以下内容(不确定内部引号):

c:\windows\system32\cmd.exe "/c \"C:\Program Files\path_to_dir\executable\""
Run Code Online (Sandbox Code Playgroud)

这不是你想要的:整个字符串被传递给cmdinclude /c。由于/c和 路径是两个参数,因此您应该将它们单独传递给QProcess,而不必担心空格,因为它们将被自动处理:

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);
Run Code Online (Sandbox Code Playgroud)