您的位置:首页 > 编程语言 > Qt开发

Qt获取系统相关位置

2016-07-15 14:20 483 查看
在使用比如打开文件对话框(QFileDialog)时,想让其打开位置位于用户桌面等位置

Qt为我们提供了函数,使其实现起来十分方便

Qt4--Qt5用QDesktopServices;

首先要引入头文件
#include <QDesktopServices>
然后可以通过如下语句获取不同的路径
static QString QDesktopServices::storageLocation(StandardLocation type);
例如:获取桌面路径
//获取桌面路径
QString path = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);



=====================================================================================================================

Qt5以后用QStandardPaths;

首先要引用头文件
#include <QStandardPaths>

然后可以通过如下语句获取不同的路径

static QString writableLocation(StandardLocation type);
也可以通过一下语句获取路径下的文件和文件夹(注意:该函数返回值类型是list)
static QStringList standardLocations(StandardLocation type);


例如:获取桌面路径

QString path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);


参数为路径名字的指代值,列表如下

QStandardPaths::DesktopLocation0Returns the user's desktop directory.
QStandardPaths::DocumentsLocation1Returns the user's document.
QStandardPaths::FontsLocation2Returns the user's fonts.
QStandardPaths::ApplicationsLocation3Returns the user's applications.
QStandardPaths::MusicLocation4Returns the user's music.
QStandardPaths::MoviesLocation5Returns the user's movies.
QStandardPaths::PicturesLocation6Returns the user's pictures.
QStandardPaths::TempLocation7Returns the system's temporary directory.
QStandardPaths::HomeLocation8Returns the user's home directory.
QStandardPaths::DataLocation9Returns a directory location where persistent application data can be

stored. QCoreApplication::organizationName and

QCoreApplication::applicationName are appended to the directory location returned for GenericDataLocation.
QStandardPaths::CacheLocation10Returns a directory location where user-specific non-essential (cached) data should be written.
QStandardPaths::GenericCacheLocation15Returns a directory location where user-specific non-essential (cached) data, shared across applications, should be written.
QStandardPaths::GenericDataLocation11Returns a directory location where persistent data shared across applications can be stored.
QStandardPaths::RuntimeLocation12Returns a directory location where runtime communication files should be written. For instance unix local sockets.
QStandardPaths::ConfigLocation13Returns a directory location where user-specific configuration files should be written.
QStandardPaths::DownloadLocation14Returns a directory for user's downloaded files.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: