系列文章目录
前言
在进行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开发中,获取文件路径是一个常见的操作,根据具体需求选择合适的方法获取文件路径能够更加方便地进行文件的读取、写入、显示等操作。