如何启动文件/目录/ URL的关联应用程序? [英] How to launch the associated application for a file / directory / URL?

查看:117
本文介绍了如何启动文件/目录/ URL的关联应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux 似乎很简单: xdg-open< file / directory / URL>

显然, Mac 与之相似:应该使用 open 代替 xdg-open 。我无法使用Mac,因此无法对其进行测试。

Apparently, Mac is similar: open should be used instead of xdg-open. I don't have access to a Mac so I couldn't test it.

对于 Windows ,我发现了4条不同的建议,我尝试失败。

For Windows, I found 4 different suggestions and those that I have tried failed.

如何将重点放在打开外壳程序文件的默认程序上,从Java吗?建议

cmd / c start ...

如何打开给定文件的用户系统首选编辑器?

如何使用Java查找默认文件打开器?
建议
RUNDLL32.exe

使用ShellE的正确方法是什么C中的xecute()打开.txt

使用Windows的本机程序在C ++中打开文件代码

如何使用ShellExecute在Windows中使用C ++打开html文件?建议

ShellExecute

I已经使用 system() QProcess :: startDetached()尝试了前3个 http://www.stackoverflow.com 作为参数,但是它们都失败了;虽然 start 在命令行中也可以。我还没有尝试过 ShellExecute

I have tried the first 3 with system() and QProcess::startDetached() and "http://www.stackoverflow.com" as argument but they all failed; start works just fine from the command line though. I haven't tried ShellExecute yet.

什么是Windows下的 xdg-open ?在我看来,这是 start ,但是为什么我尝试使用 start 失败?

What is the Windows equivalent of xdg-open? It seem to me, it is start but why did my attempts with start fail?

ShellExecute 是我唯一的选择吗?

Is ShellExecute my only option?

编辑我以为 QDesktopServices :: openUrl()仅用于网页,因为它不适用于文件或目录。

EDIT I thought QDesktopServices::openUrl() was for web pages only because it did not work for files or directories.

经过调试后,我发现如果替换 \\ 在Windows上的路径中带有 / ,它适用于文件,但仍未打开目录。有什么想法我做错了吗?

After some debugging I figured out that if I replace \\ with / in the path on Windows, it works for files but the directories are still not opened. Any ideas what I am doing wrong?

QDir dir("C:/Documents and Settings/ali");

qDebug() << "Exists? " << dir.exists();

qDebug() << dir.absolutePath();

QDesktopServices::openUrl(QUrl(dir.absolutePath()));

qDebug() << "External app called";

应用程序输出:

Exists?  true 
"C:/Documents and Settings/ali" 
External app called 

但是没有任何反应,该目录未打开。在Linux上,按预期使用默认文件管理器打开目录。

But nothing happens, the directory is not opened. On Linux, directories are opened with the default file manager as expected.

解决方案:到Qt错误 Windows怪癖(格式错误的应用程序窗口),我最终使用了 ShellExecute 。这给了我足够的灵活性,可以花一些钱来实现我想要的东西...

SOLUTION: Due to the Qt bug and Windows quirks (malformed application window), I ended up using ShellExecute. That gives me enough flexibility to achieve exactly what I want at some expense...

推荐答案

为什么不只使用Qt支持这个吗?例如:

Why don't you just use Qt's support for this? For example:

QDesktopServices::openUrl(QUrl("/home/realnc/test.pdf"));

这将在Acrobat Reader中打开文档。通常,对于所有与一个或多个应用程序相关联的文件类型,它都遵循我操作系统中的首选应用程序设置。最好的是,它是独立于平台的。

This opens the document in Acrobat Reader. In general, it obeys the preferred application settings in my OS for all file types that have one or more applications associated with them. Best of all, it's platform-independent.

编辑:
它在Linux上但不在Windows上打开目录的事实闻起来像个bug。最好在 Qt的错误跟踪器中进行报告。同时,当文件为目录时,您可以使用Windows的解决方法:

The fact that it opens directories on Linux but not on Windows smells like a bug. It might be best to report this on Qt's bug tracker. In the meantime, you could have a workaround for Windows for when the file is a directory:

#ifdef Q_WS_WIN
    if (QFileInfo(path).isDir())
        QProcess::startDetached("explorer", QStringList(path));
    else
#endif
        QDesktopServices::openUrl(QUrl(path));

您也可以使用cmd.exe的start命令来执行此操作,但是会弹出一个丑陋的终端几秒钟的时间:

You can also do it with cmd.exe's start command, but you'll get an ugly terminal pop up for a few fractions of a second:

QProcess::startDetached("cmd", QStringList() << "/C" << "start"
                               << QDir::toNativeSeparators(path));

这篇关于如何启动文件/目录/ URL的关联应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆