【Qt之·路径获取】

系列文章目录



前言

  在进行Qt开发时,经常需要获取文件的路径,如图片、音频、配置文件等。路径的获取可以通过直接指定绝对路径或者使用相对路径的方式来实现。本篇博客将介绍如何在Qt中获取文件路径的方法。


在Qt中,常用的获取路径的方法有以下几种:

一、使用相对路径

1.1 相对路径

相对路径是相对于当前工作目录的路径,可以通过qApp->applicationDirPath()来获取当前应用程序的路径。

QString path = qApp->applicationDirPath() + "/image/logo.png";

在上面的例子中,我们将图片的路径设置为当前应用程序的路径下的image文件夹中的logo.png文件。

1.2 绝对路径

绝对路径是从文件系统的根目录开始的路径,可以直接指定文件的全路径来获取。

QString path = "/home/user/image/logo.png";

1.3 QDir类

QDir类提供了一系列方法来获取文件路径。例如,通过dir.exists()方法可以判断文件或目录是否存在,通过dir.absolutePath()方法可以获取文件或目录的绝对路径。

QDir dir("/home/user/image");
if (dir.exists()) {
    QString path = dir.absolutePath() + "/logo.png";
}

在上面的例子中,我们首先判断了image目录是否存在,如果存在则拼接出了图片的绝对路径。

1.4 QFileDialog对话框

QFileDialog对话框提供了一个文件选择的界面,可以让用户选择文件并返回文件路径。

QString path = QFileDialog::getOpenFileName(this, "选择图片", qApp->applicationDirPath(), "图片文件(*.jpg *.png)");

在上面的例子中,我们使用了QFileDialog::getOpenFileName()方法来打开一个文件选择对话框,用户选择完文件后返回文件的路径。

二、示例

2.1 示例一

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QStandardPaths>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

	//获取程序所在路径
	QString applicationDirPath = QCoreApplication::applicationDirPath();
	qDebug() << "applicationDirPath=" << applicationDirPath;

	//程序的完整路径
	QString applicationFilePath = qApp->applicationFilePath();
	qDebug() << "applicationFilePath=" << applicationFilePath;

	//当前工作目录
	QString currentPath = QDir::currentPath();
	qDebug() << "currentPath=" << currentPath;

	//用户目录路径
	QString HomeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
	qDebug() << "HomeLocation=" << HomeLocation;

	QStringList HomeLocation2 = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
	qDebug() << "HomeLocation2=" << HomeLocation2[0];

	//我的文档路径
	QString DocumentsLocation = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
	qDebug() << "DocumentsLocation=" << DocumentsLocation;

	QStringList DocumentsLocation2 = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
	qDebug() << "DocumentsLocation2=" << DocumentsLocation2[0];

	//桌面路径
	QString DesktopLocation = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
	qDebug() << "DesktopLocation=" << DesktopLocation;

	QStringList DesktopLocation2 = QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
	qDebug() << "DesktopLocation2=" << DesktopLocation2[0];

	//程序数据存放路径
	QString AppDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
	qDebug() << "AppDataLocation=" << AppDataLocation;

	QStringList AppDataLocation2 = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
	qDebug() << "AppDataLocation2=" << AppDataLocation2[0];

	/*Qt5.5 中引入了另一种方法*/
	QString AppConfigLocation = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
	qDebug() << "AppConfigLocation=" << AppConfigLocation;

	QStringList AppConfigLocation2 = QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation);
	qDebug() << "AppConfigLocation2=" << AppConfigLocation2[0];

	//临时文件路径
	QString TempLocation = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
	qDebug() << "TempLocation=" << TempLocation;

	QStringList TempLocation2 = QStandardPaths::standardLocations(QStandardPaths::TempLocation);
	qDebug() << "TempLocation2=" << TempLocation2[0];

	//更传统的方法是利用QDir的一个静态函数tempPath()
	QString tempPath  = QDir::tempPath();
	qDebug() << "tempPath=" << tempPath;

	system("pause");
    return a.exec();
}

总结

  总之,在进行Qt开发中,获取文件路径是一个常见的操作,根据具体需求选择合适的方法获取文件路径能够更加方便地进行文件的读取、写入、显示等操作。

相关推荐

  1. Qt·路径获取

    2024-04-30 13:38:02       8 阅读
  2. springboot 获取路径

    2024-04-30 13:38:02       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-30 13:38:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-30 13:38:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-30 13:38:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-30 13:38:02       18 阅读

热门阅读

  1. 动态规划专训5——子序列系列

    2024-04-30 13:38:02       10 阅读
  2. React面试题(一)

    2024-04-30 13:38:02       10 阅读
  3. LCD1602移动显示

    2024-04-30 13:38:02       11 阅读
  4. Android 版本号名称及SDK对应关系

    2024-04-30 13:38:02       10 阅读
  5. HTTP协议中的Keep-Alive是什么作用?

    2024-04-30 13:38:02       9 阅读
  6. 教程推荐:手机应用自动化

    2024-04-30 13:38:02       12 阅读
  7. 深入了解排序算法:数据结构中的排序技术

    2024-04-30 13:38:02       12 阅读
  8. C++基础语法:new定位符的一点思考

    2024-04-30 13:38:02       10 阅读
  9. mysql添加远程登录账户

    2024-04-30 13:38:02       12 阅读
  10. kubernetes 之 一键安装prometheus

    2024-04-30 13:38:02       13 阅读
  11. SAP接口实现QA11 BDC绕过决策数字签名

    2024-04-30 13:38:02       13 阅读