关于Qt相关文件读写操作以及文件夹的一些知识,之前也写过一些博客:
Qt关于路径的处理(绝对路径、相对路径、路径拼接、工作目录、运行目录)_qt 相对路径-CSDN博客
C++/Qt 读写文件_qt c++ 读取文本文件-CSDN博客
C++/Qt读写ini文件_qt读取ini文件中对象-CSDN博客
C++/Qt读写xml文件_c++用qt库读取xml文件-CSDN博客
本文主要总结一下文件夹以及文件相关类的一些使用方法。
QDir
QDir
类提供了对目录和路径的操作,包括创建、删除、遍历目录,以及获取目录信息等功能。
创建文件夹:
QDir dir("mydir");
if (!dir.exists()) QDir().mkdir(dir.absolutePath());
判断工作目录下是否存在一个名为“mydir”的如果不存在则创建,这其中就涉及到了QDir的三个方法:exist()(判断文件夹是否存在)、mkdir()(用于创建单个目录)、absolutePath()(文件的绝对路径)。
关于exist方法这里不多讲,mkdir创建单个目录的方法,如果上级目录不存在,则会创建失败,如果想要创建多级目录则必须使用mkpath,QDir().mkpath("1/2/2/3/mydir")。对应的删除方法则是rmkdir和rmkpath。
QDir获取对应文件夹路径的有三个方法:
QString absolutePath() const
QString canonicalPath() const
QString path() const
试着都打印一下:
QDir dir("mydir");
if (!dir.exists()) QDir().mkdir(dir.absolutePath());
qDebug() << "path=" << dir.path();
qDebug() << "absolutePath=" << dir.absolutePath();
qDebug() << "canonicalPath=" << dir.canonicalPath();
可以看到path只是单纯的文件夹名(与dirName相同),而absolutePath和canonicalPath似乎都是绝对路径,可以看一下Qt的官方文档对canonicalPath的定义:
canonical一词为规范,canonicalPath()
方法返回目录的规范路径,会解析符号链接,返回一个规范化的路径。
可以使用cd进入对应目录以及cdUp退回上一级目录:
dir.mkdir("subdir");
dir.cd("subdir");
qDebug() << dir.dirName();
dir.cdUp();
qDebug() << dir.dirName();
获取目录下所有子目录以及文件:
QFileInfoList
entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const
QFileInfoList
entryInfoList(QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const
QStringList
entryList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const
QStringList entryList(QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const
前面两个方法和后面两个方法的区别是前者返回的类型是QFileInfo(具体在本文后面介绍QFileInfode的时候再说),后者则是直接返回对应文件路径。方法对应的三个参数分别是:nameFilters(文件名过滤),filters(文件类型过滤),sort(排序方式)。
QDir dir(QCoreApplication::applicationDirPath());
if (!dir.exists()) QDir().mkdir(dir.absolutePath());
dir.cdUp();
foreach (const QString &file,
dir.entryList({"*.cpp"}, QDir::Files, QDir::Size)) {
qDebug() << file;
}
获取对应目录下所有子目录以及文件,然后过滤后缀名为cpp以及只显示纯文件类型,并且按照文件大小排序,编译运行查看打印:
QFile
QFile文件类相对于QFileInfo文件信息类,则只关注文件的一些具体操作,比如打开进行读写操作、复制、重命名、删除:
注意moveToTrash就是将文件移入回收站,这个方法是Qt5.15之后才有的。
QFileInfo
QFileInfo作为文件信息类提供了对文件和目录信息的访问和查询功能。上文说到的QDir的两个方法就是将对应目录下的子目录和文件以QFileInfo的形式返回
QFileInfoList
entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const
QFileInfoList
entryInfoList(QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const
1. 构造:
QFileInfo(const QFileInfo &fileinfo)
QFileInfo(const QDir &dir, const QString &file)
QFileInfo(const QFile &file)
QFileInfo(const QString &file)
可以通过文件名、QFile类对象、QDir文件目录对象加目录下的对应路径构造。
也就是说QFile和QFileInfo是可以相互转换的。
2. 获取文件信息:
QDir dir(dirName);
foreach (const QFileInfo &info,
dir.entryInfoList({}, QDir::Files, QDir::Size)) {
//输出文件名以及路径相关
qDebug() << "--------------------fileName:" << info.fileName()
<< "----------------------";
qDebug() << "filePath:" << info.filePath();
qDebug() << "absolutePath:" << info.absolutePath();//文件所在目录的绝对路径
qDebug() << "absoluteFilePath:" << info.absoluteFilePath();//文件的绝对路径
qDebug() << "canonicalPath:" << info.canonicalPath();//文件规范路径
qDebug() << "baseName:" << info.baseName(); //前缀
qDebug() << "completeBaseName:"
<< info.completeBaseName(); //完全前缀,类似1.tar.gz这种
qDebug() << "suffix:" << info.suffix(); //后缀
qDebug() << "completeSuffix:"
<< info.completeSuffix(); //完全后缀,类似1.tar.gz这种
//输出文件时间相关
qDebug() << "birthTime:"
<< info.birthTime().toString(
"yyyy/MM/dd hh:mm:ss.zzz"); //创建时间
qDebug() << "lastRead:"
<< info.lastRead().toString(
"yyyy/MM/dd hh:mm:ss.zzz"); //最后一次访问时间
qDebug() << "lastModified:"
<< info.lastModified().toString(
"yyyy/MM/dd hh:mm:ss.zzz"); //最好一次修改时间
//输出文件大小
qDebug() << "size:" << info.size();//单位字节Byte
}
选择一个文件夹进行打印输出其中文件信息:
--------------------fileName: "dirtest.pdb" ----------------------
filePath: "G:/qtprojects/dirtest/debug/dirtest.pdb"
absolutePath: "G:/qtprojects/dirtest/debug"
absoluteFilePath: "G:/qtprojects/dirtest/debug/dirtest.pdb"
canonicalPath: "G:/qtprojects/dirtest/debug"
baseName: "dirtest"
completeBaseName: "dirtest"
suffix: "pdb"
completeSuffix: "pdb"
birthTime: "2024/03/17 11:24:11.882"
lastRead: "2024/03/17 14:46:48.111"
lastModified: "2024/03/17 14:46:48.111"
size: 2969600
--------------------fileName: "dirtest.vc.pdb" ----------------------
filePath: "G:/qtprojects/dirtest/debug/dirtest.vc.pdb"
absolutePath: "G:/qtprojects/dirtest/debug"
absoluteFilePath: "G:/qtprojects/dirtest/debug/dirtest.vc.pdb"
canonicalPath: "G:/qtprojects/dirtest/debug"
baseName: "dirtest"
completeBaseName: "dirtest.vc"
suffix: "pdb"
completeSuffix: "vc.pdb"
birthTime: "2024/03/17 11:24:09.965"
lastRead: "2024/03/17 14:46:47.589"
lastModified: "2024/03/17 14:46:47.589"
size: 1519616
--------------------fileName: "dirtest.ilk" ----------------------
filePath: "G:/qtprojects/dirtest/debug/dirtest.ilk"
absolutePath: "G:/qtprojects/dirtest/debug"
absoluteFilePath: "G:/qtprojects/dirtest/debug/dirtest.ilk"
canonicalPath: "G:/qtprojects/dirtest/debug"
baseName: "dirtest"
completeBaseName: "dirtest"
suffix: "ilk"
completeSuffix: "ilk"
birthTime: "2024/03/17 14:46:47.932"
lastRead: "2024/03/17 14:46:48.126"
lastModified: "2024/03/17 14:46:48.126"
size: 1334024
--------------------fileName: "mainwindow.obj" ----------------------
filePath: "G:/qtprojects/dirtest/debug/mainwindow.obj"
absolutePath: "G:/qtprojects/dirtest/debug"
absoluteFilePath: "G:/qtprojects/dirtest/debug/mainwindow.obj"
canonicalPath: "G:/qtprojects/dirtest/debug"
baseName: "mainwindow"
completeBaseName: "mainwindow"
suffix: "obj"
completeSuffix: "obj"
birthTime: "2024/03/17 11:24:11.595"
lastRead: "2024/03/17 14:46:47.834"
lastModified: "2024/03/17 14:46:47.834"
size: 230172
--------------------fileName: "dirtest.exe" ----------------------
filePath: "G:/qtprojects/dirtest/debug/dirtest.exe"
absolutePath: "G:/qtprojects/dirtest/debug"
absoluteFilePath: "G:/qtprojects/dirtest/debug/dirtest.exe"
canonicalPath: "G:/qtprojects/dirtest/debug"
baseName: "dirtest"
completeBaseName: "dirtest"
suffix: "exe"
completeSuffix: "exe"
birthTime: "2024/03/17 11:24:11.881"
lastRead: "2024/03/17 14:46:48.061"
lastModified: "2024/03/17 14:46:48.061"
size: 98304
--------------------fileName: "main.obj" ----------------------
filePath: "G:/qtprojects/dirtest/debug/main.obj"
absolutePath: "G:/qtprojects/dirtest/debug"
absoluteFilePath: "G:/qtprojects/dirtest/debug/main.obj"
canonicalPath: "G:/qtprojects/dirtest/debug"
baseName: "main"
completeBaseName: "main"
suffix: "obj"
completeSuffix: "obj"
birthTime: "2024/03/17 11:24:11.511"
lastRead: "2024/03/17 14:41:19.559"
lastModified: "2024/03/17 14:41:19.559"
size: 94348
--------------------fileName: "moc_mainwindow.obj" ----------------------
filePath: "G:/qtprojects/dirtest/debug/moc_mainwindow.obj"
absolutePath: "G:/qtprojects/dirtest/debug"
absoluteFilePath: "G:/qtprojects/dirtest/debug/moc_mainwindow.obj"
canonicalPath: "G:/qtprojects/dirtest/debug"
baseName: "moc_mainwindow"
completeBaseName: "moc_mainwindow"
suffix: "obj"
completeSuffix: "obj"
birthTime: "2024/03/17 11:24:11.507"
lastRead: "2024/03/17 14:41:19.549"
lastModified: "2024/03/17 14:41:19.549"
size: 89332
--------------------fileName: "moc_mainwindow.cpp" ----------------------
filePath: "G:/qtprojects/dirtest/debug/moc_mainwindow.cpp"
absolutePath: "G:/qtprojects/dirtest/debug"
absoluteFilePath: "G:/qtprojects/dirtest/debug/moc_mainwindow.cpp"
canonicalPath: "G:/qtprojects/dirtest/debug"
baseName: "moc_mainwindow"
completeBaseName: "moc_mainwindow"
suffix: "cpp"
completeSuffix: "cpp"
birthTime: "2024/03/17 11:24:09.846"
lastRead: "2024/03/17 14:41:17.072"
lastModified: "2024/03/17 14:41:17.072"
size: 3580
--------------------fileName: "moc_predefs.h" ----------------------
filePath: "G:/qtprojects/dirtest/debug/moc_predefs.h"
absolutePath: "G:/qtprojects/dirtest/debug"
absoluteFilePath: "G:/qtprojects/dirtest/debug/moc_predefs.h"
canonicalPath: "G:/qtprojects/dirtest/debug"
baseName: "moc_predefs"
completeBaseName: "moc_predefs"
suffix: "h"
completeSuffix: "h"
birthTime: "2024/03/17 11:24:09.378"
lastRead: "2024/03/17 14:41:16.826"
lastModified: "2024/03/17 11:24:09.445"
size: 245
注意birthTime这个方法是Qt5.10之后才有的,如果是老版本Qt则使用created方法获取文件创建时间。
QFileDialog
QFileDialog
类是Qt中用于打开和保存文件对话框的类。它提供了用户界面,允许用户选择文件或目录,并返回所选文件的路径。
打开文件对话窗口并选择一个文件:
static QString getOpenFileName(QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = nullptr,
Options options = Options());
第一个参数是窗体父指针,第二个参数是对话框标题,第三个参数是对话框打开的初始路径,第四个参数是文件过滤器,第五个参数是用于存储用户选择的过滤器的指针。当用户选择文件后,将存储所选文件类型的过滤器,第六个参数配置文件对话框的行为。可以使用QFileDialog::Option
枚举值来设置不同的选项,例如QFileDialog::DontUseNativeDialog
等。
例如从工作目录打开文件夹对话框选中一个后缀为cpp或h的文件
QString filePath = QFileDialog::getOpenFileName(nullptr, "Open File",QCoreApplication::applicationDirPath(), "Code Files (*.h *.cpp)");
如若要选择多个文件则方法为:
static QStringList getOpenFileNames(QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = nullptr,
Options options = Options());
打开文件夹对话框并选择一个文件夹:
static QString getExistingDirectory(QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
Options options = ShowDirsOnly);
对应参数就不过多介绍了。