QDesktopServices类提供了访问常见桌面服务的方法。
许多桌面环境提供的服务可被应用程序用于执行常见任务,例如打开web页面,其方式既一致又考虑到用户的应用程序首选项。
该类包含向这些服务提供简单接口的函数,这些接口可以返回该服务是否调取成功的结果。
- openUrl()函数
该函数用于打开外部应用程序中位于任意url的文件。对于与本地文件系统上的资源相对应的URL (URL模式为“file”),将使用合适的应用程序来打开该文件;否则,将使用web浏览器获取并显示该文件。
使用openUrl()函数,需要提前确认操作系统是否设置当前用户的防火墙(用户的桌面设置可以控制某些类型可执行文件是打开以供浏览,还是直接执行。有些桌面环境被配置为防止用户执行从非本地url获取的文件,或者在执行之前请求用户的许可)。 - 通过返回默认系统路径storageLocation ( StandardLocation type )
Qt给出了以下 StandardLocation type 用于创建不同类型的目录:
Constant Value Description
QDesktopServices::DesktopLocation 0 Returns the user's desktop directory.
QDesktopServices::DocumentsLocation 1 Returns the user's document.
QDesktopServices::FontsLocation 2 Returns the user's fonts.
QDesktopServices::ApplicationsLocation 3 Returns the user's applications.
QDesktopServices::MusicLocation 4 Returns the users music.
QDesktopServices::MoviesLocation 5 Returns the user's movies.
QDesktopServices::PicturesLocation 6 Returns the user's pictures.
QDesktopServices::TempLocation 7 Returns the system's temporary directory.
QDesktopServices::HomeLocation 8 Returns the user's home directory.
QDesktopServices::DataLocation 9 Returns a directory location where persistent application data can be stored. QCoreApplication::applicationName and QCoreApplication::organizationName should work on all platforms.
QDesktopServices::CacheLocation 10 Returns a directory location where user-specific non-essential (cached) data should be written.
以下以创建系统临时目录为例:
QString path = "";
QString strCount = "";
// 获取系统的临时目录/tempPath
path = QDesktopServices::storageLocation(QDesktopServices::TempLocation)
+ QDir::separator() + "tempPath";
// getTickCount() 创建下级目录并格式化输出
strCount.sprintf("%08x", getTickCount());
m_strTempPath =
QString("%1%2%3")
.arg(path)
.arg(QDir::separator())
.arg(strCount);
注意:
- storageLocation返回的存储位置可以是不存在的目录,也就是说,它可能需要由系统或用户创建。
- 在Symbian操作系统上,在同一驱动器上,应用程序与可执行文件的位置总是指向/sys/bin文件夹。FontsLocation总是指向ROM驱动器上的文件夹。Symbian OS没有桌面概念,desktolocation和DocumentsLocation返回相同的路径。其余的标准位置指向与executable在同一驱动器上的相应文件夹,如果executable在ROM中,则返回C驱动器上的文件夹。