获取目录etc
一、QCoreApplication
1、常用路径
qDebug() << QCoreApplication::applicationName();
qDebug() << QCoreApplication::applicationDirPath();
qDebug() << QCoreApplication::applicationFilePath();
分别对应的是
1、工程名称
2、工程路径
3、工程文件路径全称
输出
"leaner_path_test"
"/Users/yucheng/Applications/Learn_Qt/build-leaner_path_test-Desktop_Qt_5_14_2_clang_64bit-Debug/leaner_path_test.app/Contents/MacOS"
"/Users/yucheng/Applications/Learn_Qt/build-leaner_path_test-Desktop_Qt_5_14_2_clang_64bit-Debug/leaner_path_test.app/Contents/MacOS/leaner_path_test"
2、库路径
qDebug() << QCoreApplication::libraryPaths();
输出的是连接的库路径
("/Users/yucheng/Qt5.14.2/5.14.2/clang_64/plugins", "/Users/yucheng/Applications/Learn_Qt/build-leaner_path_test-Desktop_Qt_5_14_2_clang_64bit-Debug/leaner_path_test.app/Contents/MacOS")
由于.libraryPaths()
是QStringLis
t类型的数据,所以格式和QString
不同。
3、题外话
qDebug() << QCoreApplication::applicationVersion();
qDebug() << QCoreApplication::organizationName();
qDebug() << QCoreApplication::organizationDomain();
在执行上述三句话时,出现的都是空字符串。
那是因为这些是开发商自行添加的版本、组织机构名称、组织机构领域。
使用以下语句即可

二、QApplication
QApplication是QCoreApplication的子类
具体的不用多说
和QCoreApplication用法一样

三、QDir
1、获取路径
qDebug() << QDir::currentPath();
qDebug() << QDir::homePath();
qDebug() << QDir::rootPath();
qDebug() << QDir::tempPath();
输出,具体看代码解释
"/Users/yucheng/Applications/Learn_Qt/build-leaner_path_test-Desktop_Qt_5_14_2_clang_64bit-Debug/leaner_path_test.app/Contents/MacOS"
"/Users/yucheng"
"/"
"/private/var/folders/sn/4s6c8fds6knc95wwt42nlqpw0000gp/T"
2、文件操作
QDir dir;
qDebug() << dir.exists("/Users/yucheng/try1");
dir.mkdir("/Users/yucheng/try1");
qDebug() << dir.exists("/Users/yucheng/try1");
dir.rmdir("/Users/yucheng/try1");
dir.cd("/Users/yucheng/try");
qDebug() << dir.entryList();
dir.setSorting(QDir::Size);
qDebug() << dir.entryList();
输出
false
true
(".", "..", "data", "f2.dat", "file.txt", "filea.txt", "fileb.txt", "filec.txt", "json", "svn软件", "test", "xia")
("file.txt", "..", ".", "svn软件", "data", "test", "xia", "json", "f2.dat", "filec.txt", "fileb.txt", "filea.txt")
.setSorting()
排序方式
方法 | 实现 |
---|---|
名称排序 | QDir::Name |
时间 | QDir::Time |
大小 | QDir::Size |
默认 | QDir::Unsorted |
目录优先 | QDir::DirsFirst |
相反顺序 | QDir::Reversed |
忽略大小写 | QDir::IgnoreCase |
四、QDesktopServices for Qt4
QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
五、QStandardPaths for Qt5
一共两种方式获得各种路径
static QString writableLocation(StandardLocation type);
static QStringList standardLocations(StandardLocation type);
1、.writableLocation()
函数使用
qDebug() << QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::DataLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::TempLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::FontsLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
qDebug() << QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
对应的输出路径
1、主路径
2、数据路径
3、临时路径
4、储藏路径
5、音乐路径
6、字体路径
7、桌面路径
8、配置路径
9、影片路径
10、应用路径
11、运行期间路径
12、下载路径
13、照片路径
14、应用配置路径
15、文档路径
16、普通数据路径
17、本地应用数据路径
18、普通储藏路径
19、普通配置路径
输出
"/Users/yucheng"
"/Users/yucheng/Library/Application Support/leaner_path_test"
"/private/var/folders/sn/4s6c8fds6knc95wwt42nlqpw0000gp/T"
"/Users/yucheng/Library/Caches/leaner_path_test"
"/Users/yucheng/Music"
"/Users/yucheng/Library/Fonts"
"/Users/yucheng/Desktop"
"/Users/yucheng/Library/Preferences"
"/Users/yucheng/Movies"
"/Users/yucheng/Library/Application Support/leaner_path_test"
"/Users/yucheng/Library/Application Support"
"/Users/yucheng/Downloads"
"/Users/yucheng/Pictures"
"/Users/yucheng/Library/Preferences/leaner_path_test"
"/Users/yucheng/Documents"
"/Users/yucheng/Library/Application Support"
"/Users/yucheng/Library/Application Support/leaner_path_test"
"/Users/yucheng/Library/Caches"
"/Users/yucheng/Library/Preferences"
2、.standardLocations()
函数使用
用法和.writableLocation()
一样。具体不阐述。
其生成QStringList路径
输出:
("/Users/yucheng")
("/Users/yucheng/Library/Application Support/leaner_path_test", "/Library/ApplicationSupport/leaner_path_test", "/Users/yucheng/Applications/Learn_Qt/build-leaner_path_test-Desktop_Qt_5_14_2_clang_64bit-Debug/leaner_path_test.app/Contents/Resources")
("/private/var/folders/sn/4s6c8fds6knc95wwt42nlqpw0000gp/T")
("/Users/yucheng/Library/Caches/leaner_path_test", "/Library/Caches/leaner_path_test")
("/Users/yucheng/Music")
("/Users/yucheng/Library/Fonts", "/Library/Fonts", "/System/Library/Fonts")
("/Users/yucheng/Desktop")
("/Users/yucheng/Library/Preferences")
("/Users/yucheng/Movies")
("/Users/yucheng/Library/Application Support/leaner_path_test", "/Library/Application Support/leaner_path_test", "/Users/yucheng/Applications/Learn_Qt/build-leaner_path_test-Desktop_Qt_5_14_2_clang_64bit-Debug/leaner_path_test.app/Contents/Resources")
("/Users/yucheng/Library/Application Support")
("/Users/yucheng/Downloads")
("/Users/yucheng/Pictures")
("/Users/yucheng/Library/Preferences/leaner_path_test")
("/Users/yucheng/Documents")
("/Users/yucheng/Library/Application Support", "/Library/Application Support")
("/Users/yucheng/Library/Application Support/leaner_path_test", "/Library/Application Support/leaner_path_test", "/Users/yucheng/Applications/Learn_Qt/build-leaner_path_test-Desktop_Qt_5_14_2_clang_64bit-Debug/leaner_path_test.app/Contents/Resources")
("/Users/yucheng/Library/Caches", "/Library/Caches", "/System/Library/Caches")
("/Users/yucheng/Library/Preferences")
文章部分参考来源:https://blog.youkuaiyun.com/weber_chen/article/details/79804074