此类提供了访问标准路径的方法。
1、enum QStandardPaths::LocateOption:此枚举描述了可用于控制 locate() 和 locateAll() 行为的标志。
2、enum QStandardPaths::StandardLocation:此枚举描述了可以使用诸如 writableLocation()、standardLocations() 和 displayName() 等方法查询的不同位置。
1、【static】QString displayName(QStandardPaths::StandardLocation type)
返回给定位置类型的本地化显示名称或空 QString。
- const QMetaObject &mo = QStandardPaths::staticMetaObject;
- QMetaEnum metaEnum = mo.enumerator(mo.indexOfEnumerator("StandardLocation"));
- for (int i = 0; i < metaEnum.keyCount(); ++i)
- {
- qDebug()<
key(i)<<" "<displayName(static_cast(metaEnum.value(i))); - }

2、【static】QString findExecutable(const QString &executableName, const QStringList &paths = QStringList())
在指定路径中查找名为 executableName 的可执行文件,如果 paths 为空,则查找系统路径。
在大多数操作系统上,系统路径由 PATH 环境变量确定。
注意:在 Windows 上,会自动附加通常的可执行扩展(来自 PATHEXT 环境变量)。
例如,findExecutable("foo") 调用会查找 foo.exe 或 foo.bat(如果存在)。
返回可执行文件的绝对文件路径,如果未找到则返回空字符串。
3、【static】QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = LocateFile)
在 type 的标准位置查找名为 fileName 的文件或目录。
options 标志指定查找文件或目录。
返回找到的第一个文件或目录的绝对路径或者返回空字符串。
4、【static】QStringList locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = LocateFile)
在 type 的标准位置中按名称 fileName 查找所有文件或目录。
options 标志指定查找文件或目录。
返回找到的所有文件的列表。
5、【static】void setTestModeEnabled(bool testMode)
是否在 QStandardPaths 中启用特殊的“测试模式”,它将可写位置更改为指向测试目录。这可以防止自动测试读取或写入当前用户的配置。
它会影响测试程序可能写入文件的位置:
- GenericDataLocation
- AppDataLocation
- ConfigLocation
- GenericConfigLocation
- AppConfigLocation
- GenericCacheLocation
- CacheLocation
其他位置不受影响。
- 在 Unix 上,XDG_DATA_HOME 设置为 ~/.qttest/share,XDG_CONFIG_HOME 设置为 ~/.qttest/config,XDG_CACHE_HOME 设置为 ~/.qttest/cache。
- 在 macOS 上,数据转到 ~/.qttest/Application Support,缓存转到 ~/.qttest/Cache,配置转到 ~/.qttest/Preferences。
- 在 Windows 上,所有内容都转到 %APPDATA% 下的“qttest”目录。
6、【static】QStringList standardLocations(QStandardPaths::StandardLocation type)
返回类型文件所属的所有目录。
- #include
- #include
-
- int main(int argc, char *argv[])
- {
- const QMetaObject &mo = QStandardPaths::staticMetaObject;
- QMetaEnum metaEnum = mo.enumerator(mo.indexOfEnumerator("StandardLocation"));
- for (int i = 0; i < metaEnum.keyCount(); ++i)
- {
- qDebug()<
key(i)<<" -- "<standardLocations(static_cast(metaEnum.value(i))); - }
- }

7、【static】QString writableLocation(QStandardPaths::StandardLocation type)
返回类型文件应写入的目录。
返回的存储位置可能不存在,即它可能需要由系统或用户创建。
- const QMetaObject &mo = QStandardPaths::staticMetaObject;
- QMetaEnum metaEnum = mo.enumerator(mo.indexOfEnumerator("StandardLocation"));
- for (int i = 0; i < metaEnum.keyCount(); ++i)
- {
- qDebug()<
key(i)<<" -- "<writableLocation(static_cast(metaEnum.value(i))); - }
