QProcess - 运行 路径包含空格的进程
QProcess - running process whose path contain spaces
在我的应用程序中,我 运行 从我的本地应用程序数据文件夹中分离了一个进程。以下代码适用于大多数情况。
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 上得到 "Unknown Error":
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());
}
加强,只有在用户名中有一个空格的用户时才会发生...
QString QDir::absolutePath() const
Returns the absolute path (a path that starts with "/" or with a drive
specification), which may contain symbolic links, but never contains
redundant ".", ".." or multiple separators.
将路径从根转换为绝对形式是有意义的:
QString dataPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
QString exePath = QDir(dataPath + "/../Programs").absolutePath();
qDebug() << "Executable path" << exepath;
qDebug() << "File exists" << QFile(exepath + "/MyApp.exe").exists();
至于另一个问题,由于路径中包含用户名中的空格,它无法 运行 执行。我们应该将整个路径用引号引起来,以便 Windows CreateProcess 满足:
process->startDetached(QStringLiteral("\"") + exepath + "/MyApp.exe" + QChar("\""), arguments);
请注意,Qt 通常能够接受路径参数的反斜杠“\”和斜杠“/”分隔符。
您可以尝试使用QDir解析路径:
QDir dataDir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
QString program = dataDir.absolutePath("../Programs/MyApp.exe");
在我的应用程序中,我 运行 从我的本地应用程序数据文件夹中分离了一个进程。以下代码适用于大多数情况。
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 上得到 "Unknown Error":
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());
}
加强,只有在用户名中有一个空格的用户时才会发生...
QString QDir::absolutePath() const
Returns the absolute path (a path that starts with "/" or with a drive specification), which may contain symbolic links, but never contains redundant ".", ".." or multiple separators.
将路径从根转换为绝对形式是有意义的:
QString dataPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
QString exePath = QDir(dataPath + "/../Programs").absolutePath();
qDebug() << "Executable path" << exepath;
qDebug() << "File exists" << QFile(exepath + "/MyApp.exe").exists();
至于另一个问题,由于路径中包含用户名中的空格,它无法 运行 执行。我们应该将整个路径用引号引起来,以便 Windows CreateProcess 满足:
process->startDetached(QStringLiteral("\"") + exepath + "/MyApp.exe" + QChar("\""), arguments);
请注意,Qt 通常能够接受路径参数的反斜杠“\”和斜杠“/”分隔符。
您可以尝试使用QDir解析路径:
QDir dataDir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
QString program = dataDir.absolutePath("../Programs/MyApp.exe");