method one
Use the class QDirIterator to traverse. The introduction
probably means that it is suitable for large directory traversal, supports recursion but does not support sorting.
QDirIterator::NoIteratorFlags
By default, with no flags, the iterator will return path
matching QDir::Filters
entries. Also list the entries matching the rule
QDirIterator::Subdirectories
under all sub-filesQDir::Filters
QDirIterator it(dirPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::AllDirs);
Returns all files and subdirectories under a directory, excluding file links and. ..
QDirIterator it(dirPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);
Return all files in the directory (there is no sub-file file directory at this time), excluding file links and . ..
codes
void Widget::on_pushButton_clicked()
{
QStringList list;
QString dirPath = "F:\\dirTest";
QDirIterator it(dirPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::AllDirs, QDirIterator::Subdirectories);
while(it.hasNext())
{
it.next();
if(it.fileInfo().suffix() == "icd")
{
list.append(it.fileInfo().absoluteFilePath());
}
}
foreach(QString str, list)
{
qDebug() << str;
}
}
output
"F:/dirTest/nihao/qunimade/max.icd"
"F:/dirTest/nihao/明天.icd"
"F:/dirTest/tayehao/nihao.icd"
"F:/dirTest/wobuhao.icd"
Method Two
Using the entryInfoList of the QDir class
is more powerful, and can specify the file name to be traversed, as well as the standard and sorting functions of the traverse. As for the relationship entryInfoList
with them , we generally use the former, because there are many convenient interfaces. Things to pay attention to 1. How to return all entries (including entries under subfolders), at least need to specify . It's just that the docs don't say it straight (at least I think). 2. The default parameter does not mean that nothing is listed. Instead it returns readable or writable or executable. Or a file or file directory entry matching any combination of these three. the codeentryList
entryInfoList
QFileInfoList
QFileInfo
entryInfoList
QDir::AllDirs
Filters filters = NoFilter
A default constructed QDir will not filter out files based on their permissions, so entryList() and entryInfoList() will return all files that are readable, writable, executable, or any combination of the three. This makes the default easy to write, and at the same time useful.
void Widget::getAllFiles(const QString &dirPath)
{
QDir dir(dirPath);
QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::AllDirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
foreach(QFileInfo fileInfo, list)
{
if(fileInfo.isDir())
{
getAllFiles(fileInfo.absoluteFilePath());
}
else
{
fileList.append(fileInfo.absoluteFilePath());
}
}
}
output
"F:/dirTest/nihao/qunimade/max.icd"
"F:/dirTest/nihao/明天.icd"
"F:/dirTest/tayehao/nihao.icd"
"F:/dirTest/wobuhao.icd"
Tips Insert picture description constructor parameters
here can support some simple regular expressions. For the above example (looking for a file that ends with ), you can also write it like this. the code
QDirIterator
nameFilters
.icd
QString dirPath = "F:\\dirTest";
QStringList list;
QDirIterator it(dirPath, QStringList() << "*.icd", QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while(it.hasNext())
{
it.next();
list.append(it.fileInfo().absoluteFilePath());
}
foreach(QString str, list)
{
qDebug() << str;
}
output
"F:/dirTest/nihao/qunimade/max.icd"
"F:/dirTest/nihao/明天.icd"
"F:/dirTest/tayehao/nihao.icd"
"F:/dirTest/wobuhao.icd"
change into
QDirIterator it(dirPath, QStringList() << "*hao.icd", QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
output
"F:/dirTest/tayehao/nihao.icd"
"F:/dirTest/wobuhao.icd"