2012-10-07 62 views
3

我有一个非常奇怪的QProcess问题,这是奇怪的行为。QProcess和命令行“/ c”参数

我想获得在最后是这样的(这是在Windows 7中的cmd.exe)

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

(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); 

什么,我会在错误的输出是:

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

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

好像\字符没有在字符串中逃脱,留下了\”为命令字符。我在做什么错?

我已经试过与

proces.start(QString) 

功能三倍“\”\“,它也不工作。我想这个问题的解决方案必须非常容易,以至于我不会去想它。

回答

1

正如你已经指出的那样,Qt的包包含引号空格的参数,这意味着在QProcess发出的实际命令看起来类似的东西(关于内部引号不知道):

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

这是不你想要什么:整个字符串被传递给cmd,包括/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); 
2

OK,我不知道这是否是Qt的错误,但在文档中关于void QProcess::start(QString, QStringList, OpenMode)据说这样的事情:

的Windows:包含空格的参数被包装在引号。

似乎它不是真的,因为我的程序在空间和cmd shell中使用路径休息。

但是,我发现这个函数是为只接受一个字符串参数的系统设计的(和Windows一样)。

这是QProcess::setNativeArguments(QString)

接受一个QString的作为参数,尤其是针对Windows和Symbian创建的。

因此,毕竟,如果在将Windows(或Symbian)中的参数传递给系统时遇到问题,他应该尝试setNativeArguments(QString)