【发布时间】:2012-11-25 20:39:10
【问题描述】:
Linux 似乎很简单:xdg-open <file/directory/URL>。
显然,Mac 是类似的:应该使用open 而不是xdg-open。我无法使用 Mac,因此无法对其进行测试。
对于 Windows,我发现了 4 个不同的建议,但我尝试过的都失败了。
How to give focus to default program of shell-opened file, from Java? 建议
cmd /c start ...How to open user system preferred editor for given file?
How to Find Out Default File Opener with Java? 建议RUNDLL32.exeWhat is the correct way to use ShellExecute() in C to open a .txt
Open file with Windows' native program within C++ code
How to use ShellExecute to open html files in Windows using C++?建议ShellExecute
我已经尝试使用system() 和QProcess::startDetached() 和"http://www.stackoverflow.com" 作为参数的前3 个,但它们都失败了; start 在命令行中工作得很好。我还没试过ShellExecute。
xdg-open 的 Windows 等效项是什么?在我看来,它是 start,但为什么我尝试使用 start 失败了?
ShellExecute是我唯一的选择吗?
编辑我认为QDesktopServices::openUrl() 仅适用于网页,因为它不适用于文件或目录。
经过一些调试,我发现如果我在 Windows 的路径中将 \\ 替换为 /,它适用于文件,但目录仍然没有打开。任何想法我做错了什么?
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 上,目录按预期使用默认文件管理器打开。
解决方案:由于 Qt 错误和 Windows 怪癖(格式错误的应用程序窗口),我最终使用了 ShellExecute。这给了我足够的灵活性,可以以一定的代价实现我想要的……
【问题讨论】:
-
如果您想通过 system() 执行此操作,则必须调用
cmd /c start whatever并且可能需要指定 cmd.exe 的完整路径。不过,我认为 ShellExecute() 有更优雅的解决方案。
标签: c++ qt winapi cross-platform shellexecute