QProcess:startDetached 打开应用程序但未找到参数

QProcess:startDetached open application but does not find argument

我正在尝试 运行 Evince 使用 QProcess::startDetached 方法从我的 Qt 程序中读取 pdf 文件:

QProcess myProcess = QProcess();
myProcess.startDetached("evince", "~/mypath/doc.pdf");

Evince 启动良好,但在其 HMI 中我收到消息 "Cannot open the file, No such file or directory"

但是路径是可以的,因为当我使用"acroread"读取文件时,它找到了文件并可以打开它。

感谢您的帮助:)

您是否尝试发送完整路径 /home/user/mypath/doc.pdf

也尝试用一个参数调用它:

myProcess.startDetached("evince ~/mypath/doc.pdf");

我记得我遇到过同样的问题,无法解决。然而这对我有用:

QString commandLine = command + " " + parameter;
int result = QProcess::execute(commandLine.toLatin1());

波浪字符是一个 shell 快捷方式,它对任何其他程序没有任何意义。

对于 shell,它表示等价于 $HOMEacrocread 可能是一个 shell 脚本并在启动实际应用程序之前隐式扩展参数,evince 可能是程序本身,因此您必须自己扩展它。

例如

QDir homeDir = QDir::home();
QFileInfo fileInfo(homeDir, "mypath/doc.pdf");

QProcess::startDetached("evince", QStringList() << fileInfo.absoluteFilePath());

如果您想在用户的 reader 选择中打开 PDF,请参阅 QDesktopServices::openUrl()