【发布时间】:2016-06-14 20:08:34
【问题描述】:
在我的应用程序中,我从本地应用程序数据文件夹中运行了一个分离的进程。下面的代码适用于大多数情况。
void executeApp(const QString &id)
{
QString program = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
program = program + "\\..\\Programs\\MyApp.exe";
QStringList arguments;
arguments << "--app_id="+id; //it is only one argument
QProcess* process = new QProcess(this);
bool success = process->startDetached(program, arguments);
if (!success) //TODO: Error handling
qDebug() << "Couldn't start process at " << program << process->errorString();
}
运行一些测试,我发现当 Windows 帐户用户名中包含空格时它不起作用(Windows 实际上允许这样做)。
如何解决?
--- 编辑:
根据发布的答案,我对代码进行了一些更改。但是,我仍然从下面的代码中收到 QMessageBox 上的“未知错误”:
void executeApp(const QString &id)
{
QString program = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
program = QDir(program + "/../Programs/MyApp.exe").absolutePath();
QStringList arguments;
arguments << "--app_id="+id; //it is only one argument
QProcess* process = new QProcess(this);
bool success = process->startDetached(program, arguments);
if (!success)
QMessageBox::critical(NULL, tr("Launching App"), process->errorString());
}
强调一下,只有在用户名中有一个空格的用户时才会发生这种情况......
【问题讨论】:
-
我尝试过使用引号 (""),但没有成功
标签: windows qt command-line whitespace qprocess