在QT开发中,总是绕不开需要获取各种路径,本文整理了常用的路径获取,代码如下:
#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();
}
输出结果如下:
applicationDirPath= "C:/Users/Administrator/Desktop/QtPathsTest/Debug"
applicationFilePath= "C:/Users/Administrator/Desktop/QtPathsTest/Debug/QtPathsTest.exe"
currentPath= "C:/Users/Administrator/Desktop/QtPathsTest/QtPathsTest"
HomeLocation= "C:/Users/Administrator"
HomeLocation2= "C:/Users/Administrator"
DocumentsLocation= "C:/Users/Administrator/Documents"
DocumentsLocation2= "C:/Users/Administrator/Documents"
DesktopLocation= "C:/Users/Administrator/Desktop"
DesktopLocation2= "C:/Users/Administrator/Desktop"
AppDataLocation= "C:/Users/Administrator/AppData/Roaming/QtPathsTest"
AppDataLocation2= "C:/Users/Administrator/AppData/Roaming/QtPathsTest"
AppConfigLocation= "C:/Users/Administrator/AppData/Local/QtPathsTest"
AppConfigLocation2= "C:/Users/Administrator/AppData/Local/QtPathsTest"
TempLocation= "C:/Users/Administrator/AppData/Local/Temp"
TempLocation2= "C:/Users/Administrator/AppData/Local/Temp"
tempPath= "C:/Users/Administrator/AppData/Local/Temp"
Qt上获取指定目录下或者及其子目录下各文件或者子目录的全路径
在做一个项目的时候,需要获取一个指定的文件夹下的所有子文件夹下的所有文件的全路径,这样将所有文件的绝对路径取到,然后对这些文件进行处理。
在发现没有相关的Qt自带的函数后,自己写了如下的函数。
头文件:
#ifndef ALLFILESORPATHINDIR_H
#define ALLFILESORPATHINDIR_H
#include <QString>
#include <QStringList>
class AllFilesOrPathInDir
{
public:
AllFilesOrPathInDir();
/**
* @brief AllFilesOrPathInDir 构造函数
* @param dirPath 目录的路径
*/
AllFilesOrPathInDir(QString dirPath);
/** 设置目录
* @brief setDirPath
* @param dirPath 目标文件夹的目录
*/
void setDirPath(QString dirPath);
/** 获取指定目录下的文件路径+名称
* @brief getFilePathNameOfSplDir
* @param dirPath 目标文件夹的目录
* @return
*/
QStringList getFilePathNameOfSplDir(QString dirPath);
/** 获取在指定目录下的目录的路径
* @brief getDirPathOfSplDir
* @param dirPath
* @return
*/
QStringList getDirPathOfSplDir(QString dirPath);
/** 获取指定目录下的文件和目录的全路径
* @brief getFileAndDirPathNameOfSplDir
* @param dirPath
* @return
*/
QStringList getFileAndDirPathNameOfSplDir(QString dirPath);
/** 在指定文件夹下的文件数
* @brief fileConutInSplDir
* @param dirPath
* @return
*/
int fileConutInSplDir(QString dirPath);
/** 在指定文件夹下的目录数
* @brief dirCountInSplDir
* @param dirPath
* @return
*/
int dirCountInSplDir(QString dirPath);
/** 获取指定目录及其子目录下文件的全路径
* @brief getFilePathNameOfSplAndChildDir
* @param dirPath
* @return
*/
QStringList getFilePathNameOfSplAndChildDir(QString dirPath);
/** 获取指定目录及其子目录下的文件夹的全路径
* @brief getDirPathOfSplAndChildDir
* @param dirPath
* @return
*/
QStringList getDirPathOfSplAndChildDir(QString dirPath);
/** 获取指定目录及其子目录下的文件与目录的全路径
* @brief getFileAndDirPathNameOfSplAndChildDir
* @param dirPath
* @return
*/
QStringList getFileAndDirPathNameOfSplAndChildDir(QString dirPath);
private:
QString dirPath;
};
#endif // ALLFILESORPATHINDIR_H
源文件:
#include "AllFilesOrPathInDir.h"
#include <QFileInfo>
#include <QFileInfoList>
#include <QDir>
#include <QDebug>
AllFilesOrPathInDir::AllFilesOrPathInDir()
{
}
AllFilesOrPathInDir::AllFilesOrPathInDir(QString dirPath)
{
this->dirPath = dirPath;
testCode();
}
void AllFilesOrPathInDir::setDirPath(QString dirPath)
{
this->dirPath = dirPath;
}
QStringList AllFilesOrPathInDir::getFilePathNameOfSplDir(QString dirPath)
{
QStringList filePathNames;
QDir splDir(dirPath);
QFileInfoList fileInfoListInSplDir = splDir.entryInfoList(QDir::Files);
QFileInfo tempFileInfo;
foreach (tempFileInfo, fileInfoListInSplDir) {
// absoluteFilePath() -- 截取到的是文件夹的路径
// absolutePath() -- 截取的是文件夹所在路径
filePathNames << tempFileInfo.absoluteFilePath();
}
return filePathNames;
}
QStringList AllFilesOrPathInDir::getDirPathOfSplDir(QString dirPath)
{
QStringList dirPaths;
QDir splDir(dirPath);
QFileInfoList fileInfoListInSplDir = splDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfo tempFileInfo;
foreach (tempFileInfo, fileInfoListInSplDir) {
dirPaths << tempFileInfo.absoluteFilePath();
}
return dirPaths;
}
QStringList AllFilesOrPathInDir::getFileAndDirPathNameOfSplDir(QString dirPath)
{
QStringList fileAndDirPathNames;
fileAndDirPathNames << getFilePathNameOfSplDir(dirPath);
fileAndDirPathNames << getDirPathOfSplDir(dirPath);
return fileAndDirPathNames;
}
int AllFilesOrPathInDir::fileConutInSplDir(QString dirPath)
{
return QDir(dirPath).entryList(QDir::Files).count();
}
int AllFilesOrPathInDir::dirCountInSplDir(QString dirPath)
{
return QDir(dirPath).entryList(QDir::Dirs | QDir::NoDotAndDotDot).count();
}
QStringList AllFilesOrPathInDir::getFilePathNameOfSplAndChildDir(QString dirPath)
{
QStringList filePathNames;
// 首先,得到这个目录下面的文件全部
filePathNames << getFilePathNameOfSplDir(dirPath);
QStringList childDirs;
childDirs << getDirPathOfSplDir(dirPath);
QString tempChildDir;
foreach (tempChildDir, childDirs) {
// 取其子文件夹内容
filePathNames << getFilePathNameOfSplAndChildDir(tempChildDir);
}
return filePathNames;
}
QStringList AllFilesOrPathInDir::getDirPathOfSplAndChildDir(QString dirPath)
{
QStringList dirPathNames;
dirPathNames << getDirPathOfSplDir(dirPath);
QString tempChildDir;
foreach (tempChildDir, dirPathNames) {
dirPathNames << getDirPathOfSplAndChildDir(tempChildDir);
}
return dirPathNames;
}
QStringList AllFilesOrPathInDir::getFileAndDirPathNameOfSplAndChildDir(QString dirPath)
{
QStringList fileAndDirPathNames;
fileAndDirPathNames << getFilePathNameOfSplDir(dirPath);
QStringList childDirs;
childDirs << getDirPathOfSplDir(dirPath);
QString tempChildDir;
foreach (tempChildDir, childDirs) {
fileAndDirPathNames << tempChildDir;
// 取其子文件夹内容
fileAndDirPathNames << getFileAndDirPathNameOfSplAndChildDir(tempChildDir);
}
return fileAndDirPathNames;
}
在上面的代码里面没有写对默认set进来的路径和构造函数进来的路径的代码,但是显而易见非常容易,写几个没有参数的同样的方法名,然后在其函数内部进行对上面函数的调用即可实现。