我的 QProcess 遇到了非常奇怪的问题,而且它的行为很奇怪。
我最终想要得到的是这样的东西(这是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)
具有三重“\”\”的函数,它也不起作用。我想这个问题的解决方案必须非常简单,我什至都没有想到它。
最佳答案
好吧,我不知道这是否是 Qt 错误,但在关于 void QProcess::start(QString, QStringList, OpenMode)
的文档中据说是这样的:
Windows: Arguments that contain spaces are wrapped in quotes.
似乎这不是真的,因为我的程序使用带有空格的路径,并且 cmd shell 在那里中断。
但是,我发现该函数是为仅接受一个字符串参数的系统设计的(就像 Windows 那样)。
这是 QProcess::setNativeArguments(QString)
接受一个 QString 作为参数,专为 Windows 和 Symbian 创建。
毕竟,如果有人在将 Windows(或 Symbian)中的参数传递到系统时遇到问题,他应该尝试 setNativeArguments(QString)
。
关于escaping - QProcess 和命令行 "/c"参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12769321/