2012-10-07 108 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)