Qt中获取各种目录用法

一、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()QStringList类型的数据,所以格式和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.csdn.net/weber_chen/article/details/79804074

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值