0

如果我在 windows XP 和 windows 32 位系统中以编程方式传递用户主目录,则处理方法不起作用

下面的代码工作正常:

 QProcess process;
 process.execute("C:/DOCUME~1/pjo/myprok/tmp/APP.exe");

不工作代码:

在这里,我使用 QDir::homePath 获取 APP.exe 的路径

    process.execute("C:/Documents and Settings/pjo/myprok/tmp/APP.exe");

错误字符串返回“未知错误”

我也尝试了 start 方法,但它永远不会起作用:

B 不工作代码:

    process.start("C:/Documents and Settings/pjo/myprok/tmp/APP.exe");

错误:找不到路径

    process.start("C:/DOCUME~1/pjo/myprok/tmp/APP.exe");

错误:未知错误

4

2 回答 2

2

execute() 是一个静态函数,所以应该这样调用它:

QProcess::execute("C:/Documents and Settings/pjo/myprok/tmp/APP.exe");

您是说您以编程方式获取主目录,但您显示的代码并没有这样做。也许您正在创建这样的路径:

QProcess::execute(QDir::homePath() + "APP.exe");

然后路径将错过目录和文件名之间的 / ,如下所示:

"C:/Documents and Settings/pjo/myprok/tmpAPP.exe"
于 2012-05-02T05:16:02.557 回答
1

您的问题可能是由于路径中的空格引起的引用问题(C:\Documents and Settings...)。注意 start() 有两个重载:

void    start ( const QString & program, OpenMode mode = ReadWrite )
void    start ( const QString & program, const QStringList & arguments, OpenMode mode = ReadWrite )

您正在使用第一个,它将可执行路径和所有参数放在一个字符串中,并希望它被正确引用。不加引号,“c:\documents”被解释为可执行文件,“and”“Settings...”等被解释为参数。

第二个版本分别采用参数,并将正确解释可执行路径,无需任何引用。因此,最简单的方法是使用

process.start("C:/Documents and Settings/pjo/myprok/tmp/APP.exe", QStringList());

这样可以确保使用第二个版本,并且可以避免所有引用问题。我建议始终使用该重载。

这同样适用于execute(),如前所述,它是一个静态方法,因此不会设置QProcess 对象的错误代码。

于 2012-05-02T15:18:09.053 回答