网站首页 > 博客文章 正文
Qt 程序获取程序所在路径、用户目录路径、临时文件夹等特殊路径的方法
经常我们的程序中需要访问一些特殊的路径,比如程序所在的路径、用户目录路径、临时文件夹等。在 Qt 中实现这几个功能所用的方法虽然都不难,但是各不相同,每次用到时还要现去查,很不方便。因此就写了这篇博客,把这几种需求的实现方式总结了一下。算是个备忘录吧。
程序所在路径
获取程序所在路径,QCoreApplication 类里就实现了相关的功能:
QString QCoreApplication::applicationDirPath()
比如我们有一个程序在:
C:/Qt/examples/tools/regexp/regexp.exe
那么 qApp->applicationDirPath() 的结果是:
C:/Qt/examples/tools/regexp
如果除了程序所在路径,我们还想要程序的完整名称。那么可以这么写:
qApp->applicationFilePath()
还是上面的例子,结果是:
C:/Qt/examples/tools/regexp/regexp.exe
当前工作目录
QDir 提供了一个静态函数 currentPath() 可以获取当前工作目录,函数原型如下:
QString QDir::currentPath()
如果我们是双击一个程序运行的,那么程序的工作目录就是程序所在目录。
如果是在命令行下运行一个程序,那么运行程序时在命令行的哪个目录,那个目录就是当前目录。
【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】
点击→领取「链接」
用户目录路径
Qt 4 中的方法。下面的方法只对 Qt 4 有效,Qt 5 已经删除了 storageLocation() 方法。
QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
Qt 5 中引入的方法。
QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
或者
QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
这两个方法的区别是 standardLocations() 返回值是 QStringList。当然对于 HomeLocation 来说这个 QStringList 中只有一个 QString。
还有另外一种方法,利用 QDir 类的一个静态函数:
QDir::homePath();
我的文档路径
Qt 4 中的方法。下面的方法只对 Qt 4 有效,Qt 5 已经删除了 storageLocation() 方法。
QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
Qt 5 中引入的方法。
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
桌面路径
Qt 4 中的方法。下面的方法只对 Qt 4 有效,Qt 5 已经删除了 storageLocation() 方法。
QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
Qt 5 中引入的方法。
QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
程序数据存放路径
通常我们会将程序所需的一些数据存入注册表。但是有时需要存储的数据太多,放在注册表中就不适合了。这时我们就要找个专门的地方来放数据。以前我喜欢将数据直接放到程序所在目录,但是后来发现我的程序运行时经常没有权限对这个目录下的文件进行写操作。后来发现其实 Qt 早就替我们考虑过这些问题了。
Qt 4 中的方法。下面的方法只对 Qt 4 有效,Qt 5 已经删除了 storageLocation() 方法。
QDesktopServices::storageLocation(QDesktopServices::DataLocation);
Qt 5 中引入的方法。
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
Qt 5.5 中引入了另一种方法:
QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation);
这个方法一般来说和上面的方法得到的结果是相同的。按照 Qt 帮助文档的解释,这个方法可以确保返回的路径非空。所以我认为应该优先选用这个方法。
临时文件路径
Qt 4 中的方法。下面的方法只对 Qt 4 有效,Qt 5 已经删除了 storageLocation() 方法。
QDesktopServices::storageLocation(QDesktopServices::TempLocation);
Qt 5 中引入的方法。
QStandardPaths::writableLocation(QStandardPaths::TempLocation);
QStandardPaths::standardLocations(QStandardPaths::TempLocation);
更传统的方法是利用 QDir 的一个静态函数 tempPath()。
QDir::tempPath();
在这个目录下生成临时文件和临时目录需要用到另外两个类: QTemporaryFile 和 QTemporaryDir。就不展开介绍了,大家可以参考 qt 的帮助文档。
至此,常用的各种特殊路径就介绍的差不多了。剩下还有些不常用的,可以参考 QStandardPaths 类的介绍。
猜你喜欢
- 2024-09-08 Qt 贪吃蛇制作(含源码)(qt编写贪吃蛇)
- 2024-09-08 qt 提示“启动程序失败,路径或者权限错误?”解决方法
- 2024-09-08 电脑文件不小心删除了怎么办?两个方法教你秒恢复
- 2024-09-08 Qt——内存回收(qti内存)
- 2024-09-08 C++跨平台库QT学习 操作Excel(跨平台c++开发工具)
- 2024-09-08 Qt 工程 pro文件(qt工程文件.pro在哪儿找)
- 2024-09-08 Qt中的快捷键汇总(qtcreator快捷键)
- 2024-09-08 Qt中文乱码解决思路(vscode终端中文乱码怎么解决)
- 2024-09-08 Qt5+VS2015编程实例:下拉列表框QComboBox控件使用
- 2024-09-08 Qt编程进阶(87):基于HTTP协议的网络文件下载
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)