Qt5 QProcess处理带有“特殊”字符的参数

时间:2018-03-07 23:50:26

标签: c++ linux qt ftp

我正在维护Qt5计划,该计划使用QProcessncftpget上执行Linux

我正在尝试执行的命令是:

ncftpget -E -Z -u username -p 'a#$b1379' 1.1.1.1 /x/y/temp/ update/file

只需将文件从服务器传输到本地目录即可。如果我在bash命令行上执行上面的命令,它可以正常工作。

但是,当我的Qt5应用程序使用QProcess执行相同的命令时,ftp服务器会回复说用户名/密码不正确。当密码不包含任何“特殊”字符时,它可以正常工作。

正如S.M.所提到的QProcess没有在shell中执行命令。所以我假设fork()并且进行了exec()调用的某个版本。 如何让QProcess在这里做正确的事?

我假设问题与密码中的特殊字符有关。当我构建我的QProcess参数列表时,我特别确保密码被'包围,以便转义特殊字符。

代码段:

processFTP.setParent(this->parent());
processFTP.setProcessChannelMode(QProcess::MergedChannels);

// ncftpArgs is a QStringList
// snippet doesn't show complete argument list

ncftpArgs->push_back("-Z");
ncftpArgs->push_back("-u");//username
ncftpArgs->push_back(QString((*phashSettings)[FTPUSERNAME]));
ncftpArgs->push_back("-p");//pw
// password can have special characters when shell executes command.
QString password((*phashSettings)[FTPPASSWORD]);
password.prepend("'");
password.append("'");
ncftpArgs->push_back(password);

ncftpArgs->push_back(QString((*phashSettings)[FTPSERVERIP]));
ncftpArgs->push_back(QString((*phashSettings)[FTPTEMPDIR]));

//beging the ncftpget session
QString sExe = "ncftpget";  //process name

// processFTP is a QProcess object
processFTP.start(sExe, (*ncftpArgs));
bSuccess = processFTP.waitForStarted();

// .... more code to monitor process etc...

代码记录将传递给processFTP的命令和参数,一切看起来都正确。

如何正确设置参数并启动QProcess对象,以便将包含特殊字符的密码参数正确传递给可执行文件ncftpget

和/或如何调试问题?

1 个答案:

答案 0 :(得分:1)

  

当我构建我的QProcess参数列表时,我特别确保密码被'包围。以便转义特殊字符。

请勿使用'附加或添加密码,请删除这些行

password.prepend("'");
password.append("'");

QProcess::start负责正确的程序参数传递方式。除了bash解释器不会在你的代码中通过这个调用运行。

相关问题