QT 获取各种路径

本文详细介绍了在QT开发中如何获取程序目录、完整路径、当前工作目录、用户目录、文档、桌面、程序数据、配置文件以及临时文件路径。同时提供了一个自定义类来遍历指定目录及其子目录的文件和目录全路径。
摘要由CSDN通过智能技术生成

在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进来的路径和构造函数进来的路径的代码,但是显而易见非常容易,写几个没有参数的同样的方法名,然后在其函数内部进行对上面函数的调用即可实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值