Qt獲取系統相關位置

在使用比如打開文件對話框(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::DesktopLocation 0 Returns the user's desktop directory.
QStandardPaths::DocumentsLocation 1 Returns the user's document.
QStandardPaths::FontsLocation 2 Returns the user's fonts.
QStandardPaths::ApplicationsLocation 3 Returns the user's applications.
QStandardPaths::MusicLocation 4 Returns the user's music.
QStandardPaths::MoviesLocation 5 Returns the user's movies.
QStandardPaths::PicturesLocation 6 Returns the user's pictures.
QStandardPaths::TempLocation 7 Returns the system's temporary directory.
QStandardPaths::HomeLocation 8 Returns the user's home directory.
QStandardPaths::DataLocation 9 Returns 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::CacheLocation 10 Returns a directory location where user-specific non-essential (cached) data should be written.
QStandardPaths::GenericCacheLocation 15 Returns a directory location where user-specific non-essential (cached) data, shared across applications, should be written.
QStandardPaths::GenericDataLocation 11 Returns a directory location where persistent data shared across applications can be stored.
QStandardPaths::RuntimeLocation 12 Returns a directory location where runtime communication files should be written. For instance unix local sockets.
QStandardPaths::ConfigLocation 13 Returns a directory location where user-specific configuration files should be written.
QStandardPaths::DownloadLocation 14 Returns a directory for user's downloaded files.










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章