所谓系统标准路径指的是本地文件系统中,用户的特定目录或系统的配置目录。比如在Windows系统中的“我的文档”,“视频”,“图片”等目录位置。
对于一个大型项目,系统的标准目录是保存数据,配置信息的一个非常有用的地方。例如,一个应用程序需要将下载好的文档保存在本地文件系统的某个地方,而它不能假设某个定好的路径是存在于磁盘上的。有的人可能会将这个文档保存在应用程序所在的工作目录中,当然这是个好方法,但有时应用程序并不希望将数据放在工作目录中,这是因为:
- 这会使得程序工作目录变得复杂,让用户来干预工作目录,这无疑是一件危险的事情,很可能会破坏程序。
- 有的程序会希望工作目录是只读的,所以禁止其它用户来对其进行修改。
- 如果某些配置文件是和其它程序是共享的,如若放置这些文件在某个程序的工作目录中,显然是不明智的做法,而最好的做法就是放在这些标准目录中啦。
对于系统标准目录,我们可以认定它是必然存在的(即使不存在,也可自动创建),但是不同的操作系统,可能有不一样的系统标准目录。例如“我的文档”目录位置:
- Windows: C:/Users//Documents
- MacOs : ~/Documents
- Linux : ~/Documents
- Android : /Documents,//Documents
- IOS : /Documents
如果想要做跨平台的系统,像这些路径,你都得想办法获取,这只是一个我的文档,如果再加上“下载”,“图片”等标准路径,想想是不是都很麻烦。
然而,Qt却给我们提供了非常方便的类来获取这些标准目录路径,它就是今天要介绍的QStandardPaths类。
常用接口介绍
这些接口均为静态函数
1. QStandardPaths::displayName
static QString displayName(StandardLocation type);
根据标准目录类型化,返回对应的本地名称,如果找不到,返回空QString。 例如指定type为DesktopLocation,则返回桌面名称;DocumentsLocation,则返回文档目录名称。
#include <QDebug>#include <QStandardPaths>int main(){qDebug() << QStandardPaths::displayName(QStandardPaths::DesktopLocation); // "Desktop"qDebug() << QStandardPaths::displayName(QStandardPaths::DocumentsLocation); // "Documents"qDebug() << QStandardPaths::displayName(QStandardPaths::MusicLocation); // "Music"qDebug() << QStandardPaths::displayName(QStandardPaths::HomeLocation); // "Home"}
2.QStandardPaths::findExecutable
QString QStandardPaths::findExecutable(const QString &executableName,const QStringList &paths = QStringList());
在指定的路径中查找名为executableName的可执行文件,如果paths为空,则在系统路径中查找。 系统路径指PATH环境变量的值。 如果存在,返回可执行文件的绝对文件路径,如果没有找到,则返回空字符串。
#include <QDebug>#include <QStandardPaths>int main(){QStringList paths;paths << "D:/Program Files/Notepad++";// "D:/Program Files/Notepad++/notepad++.exe"qDebug() << QStandardPaths::findExecutable("notepad++", paths);}
3. QStandardPaths::locate
根据标准目录类型,在该目录中查找名为fileName的文件或目录。 返回找到的第一个文件或目录的完整路径(取决于options)。 如果找不到这样的文件或目录,则返回一个空字符串。
#include <QDebug>#include <QStandardPaths>int main(){// fr.txt文件存在, 返回: "C:/Users/Admin/Documents/fr.txt"qDebug() << QStandardPaths::locate(QStandardPaths::DocumentsLocation, "fr.txt");// frr.txt文件不存在, 返回: ""qDebug() << QStandardPaths::locate(QStandardPaths::DocumentsLocation, "frr.txt");}
这里大家可能有疑问,这个接口并不会递归查询子目录中的文件,而在同一目录下文件名是不可以重名的,所以也就最多有一个匹配的文件,但它的接口描述是返回第一个。其实,某一种类型的系统标准目录可能会有多个路径,因此,系统会按照顺序查找这些路径中的文件或目录。 例如QStandardPaths::DataLocation,数据文件类型目录就有多个路径与之对应:
#include <QApplication>#include <QDebug>#include <QStandardPaths>int main(int argc, char* argv[]){QApplication a(argc, argv);qDebug() << QStandardPaths::standardLocations(QStandardPaths::DataLocation);}/* 会输出:("C:/Users/Admin/AppData/Local/untitled6","C:/ProgramData/untitled6","F:/projectCode/build-untitled6-Desktop_x86_windows_msvc2015_pe_64bit-Debug/debug","F:/projectCode/build-untitled6-Desktop_x86_windows_msvc2015_pe_64bit-Debug/debug/data")*/
所以,该函数会依次查询standardLocations接口返回的路径列表中所匹配的文件。
4.QStandardPaths::locateAll
static QStringList locateAll(StandardLocation type,const QString &fileName,LocateOptions options = LocateFile);
同locate接口,但这里返回的是所有符合条件的文件或目录。
5.QStandardPaths::standardLocations
static QStringList standardLocations(StandardLocation type);
根据标准目录类型,返回该类型的所属的所有目录路径。 如果可以确定的话,目录列表从高优先级到低优先级排序。 如果type没有定义,则此列表为空。
#include <QDebug>#include <QStandardPaths>int main(){// ("C:/Users/Admin/Desktop")qDebug() << QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);// ("C:/Users/Admin/Documents")qDebug() << QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);// ("C:/Users/Admin/Music")qDebug() << QStandardPaths::standardLocations(QStandardPaths::MusicLocation);// ("C:/Users/Admin")qDebug() << QStandardPaths::standardLocations(QStandardPaths::HomeLocation);}
