我想让应用程序在 MAC 的 finder 中打开一个目录,但是它不起作用。
它只适用于 Windows,不适用于 MAC。任何解决方案?
更新:
正如@Mohammad Kanan 建议的那样,使用这种语法:
QDesktopServices::openUrl(QUrl("file:///your_folder", QUrl::TolerantMode));
它正在工作,例如:
QString workingDir = qApp->applicationDirPath();
QString path = QString::fromLatin1("file://%1").arg(workingDir);
QDesktopServices::openUrl(QUrl(path, QUrl::TolerantMode));
附言。您还可以在其上添加相对路径,但要注意字符串中的 .. 和/:
QString workingDir = qApp->applicationDirPath();
workingDir += "/../../../";
QString path = QString::fromLatin1("file://%1").arg(workingDir);
QDesktopServices::openUrl(QUrl(path, QUrl::TolerantMode));
最佳答案
Windows 和 MAC 上的正确语法:
打开一个文件:
QDesktopServices::openUrl(QUrl("file:///Users/myname/myfile"));
使用相同的语法浏览文件夹:
QDesktopServices::openUrl(QUrl("file:///Users/myname/"));
注意,
如果文件系统驻留在包含空格的路径上,请使用TolerantMode
:
QDesktopServices::openUrl(QUrl("file:///C:/Documents and Settings/All Users/Desktop", QUrl::TolerantMode));
关于c++ - QDesktopServices::openUrl() 无法在 MAC Finder 中打开目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48243245/