The QDirListing class provides an STL-style iterator for directory entries. 更多...
| 头: |
#include <QDirListing>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
| Since: | Qt 6.8 |
| class | DirEntry |
(从 6.8 起)
class
|
const_iterator |
(从 6.8 起)
class
|
sentinel |
| 枚举类 | IteratorFlag { Default, ExcludeFiles, ExcludeDirs, ExcludeSpecial, ResolveSymlinks, …, FollowDirSymlinks } |
| flags | IteratorFlags |
| QDirListing (const QString & path , QDirListing::IteratorFlags flags = IteratorFlag::Default) | |
| QDirListing (const QString & path , const QStringList & nameFilters , QDirListing::IteratorFlags flags = IteratorFlag::Default) | |
| QDirListing (QDirListing && other ) | |
| ~QDirListing () | |
| QDirListing::const_iterator | begin () const |
| QDirListing::const_iterator | cbegin () const |
| QDirListing::sentinel | cend () const |
| QDirListing::sentinel | end () const |
| QDirListing::IteratorFlags | iteratorFlags () const |
| QString | iteratorPath () const |
| QStringList | nameFilters () const |
| QDirListing & | operator= (QDirListing && other ) |
You can use QDirListing to navigate entries of a directory one at a time. It is similar to QDir::entryList () 和 QDir::entryInfoList (), but because it lists entries one at a time instead of all at once, it scales better and is more suitable for large directories. It also supports listing directory contents recursively, and following symbolic links. Unlike QDir::entryList (), QDirListing does not support sorting.
The QDirListing constructor takes a directory path string as argument. Here's how to iterate over all entries recursively:
using ItFlag = QDirListing::IteratorFlag; for (const auto &dirEntry : QDirListing(u"/etc"_s, ItFlag::Recursive)) { qDebug() << dirEntry.filePath(); // /etc/. // /etc/.. // /etc/X11 // /etc/X11/fs // ... }
Here's how to find and read all regular files filtered by name, recursively:
using F = QDirListing::IteratorFlag; QDirListing dirList(u"/sys"_s, QStringList{u"scaling_cur_freq"_s}, F::FilesOnly | F::Recursive); for (const auto &dirEntry : dirList) { QFile f(dirEntry.filePath()); if (f.open(QIODevice::ReadOnly)) qDebug() << f.fileName() << f.readAll().trimmed().toDouble() / 1000 << "MHz"; }
Here's how to list only regular files, recursively:
using F = QDirListing::IteratorFlag; const auto flags = F::FilesOnly | F::Recursive; for (const auto &dirEntry : QDirListing(u"/etc"_s, flags)) { // ... }
Here's how to list only regular files and symbolic links to regular files, recursively:
using F = QDirListing::IteratorFlag; const auto flags = F::FilesOnly | F::Recursive | F::ResolveSymlinks; for (const auto &dirEntry : QDirListing(u"/etc"_s, flags)) { // ... } }
QDirListing::const_iterator models C++20 std::input_iterator , that is, it is a move-only, forward-only, single-pass iterator, that doesn't allow random access. It can be used in ranged-for loops (or with C++20 range algorithms that don't require random access iterators). Dereferencing a valid iterator returns a QDirListing::DirEntry object. The (c) end () sentinel marks the end of the iteration. Dereferencing an iterator that is equal to sentinel is undefined behavior.
QDirListing::DirEntry offers a subset of QFileInfo 's API (for example, fileName(), filePath(), exists()). Internally, DirEntry only constructs a QFileInfo object if needed, that is, if the info hasn't been already fetched by other system functions. You can use DirEntry::fileInfo () to get a QFileInfo 。例如:
using ItFlag = QDirListing::IteratorFlag; for (const auto &dirEntry : QDirListing(u"/etc"_s, ItFlag::Recursive)) { // Faster if (dirEntry.fileName().endsWith(u".conf")) { /* ... */ } // This works, but might be potentially slower, since it has to construct a // QFileInfo, whereas (depending on the implementation) the fileName could // be known already if (dirEntry.fileInfo().fileName().endsWith(u".conf")) { /* ... */ } } using ItFlag = QDirListing::IteratorFlag; for (const auto &dirEntry : QDirListing(u"/etc"_s, ItFlag::Recursive)) { // Both approaches are the same, because DirEntry will have to construct // a QFileInfo to get this info (for example, by calling system stat()) if (dirEntry.size() >= 4'000 /* 4KB */) { /* ...*/ } if (dirEntry.fileInfo().size() >= 4'000 /* 4KB */) { /* ... */ } }
另请参阅 QDir and QDir::entryList ().
This enum class describes flags that can be used to configure the behavior of QDirListing . Values from this enumerator can be bitwise OR'ed together.
| 常量 | 值 | 描述 |
|---|---|---|
QDirListing::IteratorFlag::Default
|
0x000000
|
List all entries, that is, files, directories, symbolic links including broken symbolic links (where the target doesn't exist) and special (
other
) system files, see ExcludeOther for details. Hidden files and directories and the special entries
.
and
..
aren't listed by default.
|
QDirListing::IteratorFlag::ExcludeFiles
|
0x000004
|
Don't list regular files. When combined with ResolveSymlinks, symbolic links to regular files will be excluded too. |
QDirListing::IteratorFlag::ExcludeDirs
|
0x000008
|
Don't list directories. When combined with ResolveSymlinks, symbolic links to directories will be excluded too. |
QDirListing::IteratorFlag::ExcludeSpecial
|
0x000010
|
Don't list file system entries that are
not
directories, regular files, nor symbolic links.
|
QDirListing::IteratorFlag::ResolveSymlinks
|
0x000020
|
Filter symbolic links based on the type of the target of the link, rather than the symbolic link itself. With this flag, broken symbolic links (where the target doesn't exist) are excluded. This flag is ignored on operating systems that don't support symbolic links. |
QDirListing::IteratorFlag::FilesOnly
|
ExcludeDirs | ExcludeSpecial
|
Only regular files will be listed. When combined with ResolveSymlinks, symbolic links to files will also be listed. |
QDirListing::IteratorFlag::DirsOnly
|
ExcludeFiles | ExcludeSpecial
|
Only directories will be listed. When combined with ResolveSymlinks, symbolic links to directories will also be listed. |
QDirListing::IteratorFlag::IncludeHidden
|
0x000040
|
List hidden entries. When combined with Recursive, the iteration will recurse into hidden sub-directories as well. |
QDirListing::IteratorFlag::IncludeDotAndDotDot
|
0x000080
|
List the
.
and
..
special entries.
|
QDirListing::IteratorFlag::CaseSensitive
|
0x000100
|
The file glob patterns in the name filters passed to the QDirListing constructor, will be matched case sensitively (for details, see QDir::setNameFilters ()). |
QDirListing::IteratorFlag::Recursive
|
0x000400
|
List entries inside all sub-directories as well. When combined with FollowDirSymlinks, symbolic links to directories will be iterated too. |
QDirListing::IteratorFlag::FollowDirSymlinks
|
0x000800
|
When combined with Recursive, symbolic links to directories will be iterated too. Symbolic link loops (e.g., link => . or link => ..) are automatically detected and ignored. |
IteratorFlags 类型是 typedef 对于 QFlags <IteratorFlag>。它存储 IteratorFlag 值的 OR 组合。
(c) begin () 返回 QDirListing::const_iterator that can be used to iterate over directory entries.
std::move()
d.
std::input_iterator
is partially-formed (a copy of an iterator that has since been advanced), the only valid operations on such an object are destruction and assignment of a new iterator. Therefore the post-increment operator advances the iterator and returns
void
.
const DirEntry &
注意: Each time (c) begin () is called on the same QDirListing object, the internal state is reset and the iteration starts anew.
(Some of the above restrictions are dictated by the underlying system library functions' implementation).
例如:
using ItFlag = QDirListing::IteratorFlag; for (const auto &dirEntry : QDirListing(u"/etc"_s, ItFlag::Recursive)) { qDebug() << dirEntry.filePath(); // /etc/. // /etc/.. // /etc/X11 // /etc/X11/fs // ... }
Here's how to find and read all files filtered by name, recursively:
using F = QDirListing::IteratorFlag; QDirListing dirList(u"/sys"_s, QStringList{u"scaling_cur_freq"_s}, F::FilesOnly | F::Recursive); for (const auto &dirEntry : dirList) { QFile f(dirEntry.filePath()); if (f.open(QIODevice::ReadOnly)) qDebug() << f.fileName() << f.readAll().trimmed().toDouble() / 1000 << "MHz"; }
注意: The "classical" STL algorithms don't support iterator/sentinel, so you need to use C++20 std::ranges algorithms for QDirListing , or else a 3rd-party library that provides range-based algorithms in C++17.
另请参阅 QDirListing::DirEntry .
[explicit]
QDirListing::
QDirListing
(const
QString
&
path
,
QDirListing::IteratorFlags
flags
= IteratorFlag::Default)
Constructs a QDirListing that can iterate over path .
You can pass options via flags to control how the directory should be iterated.
默认情况下, flags is IteratorFlag::Default .
另请参阅 IteratorFlags .
[explicit]
QDirListing::
QDirListing
(const
QString
&
path
, const
QStringList
&
nameFilters
,
QDirListing::IteratorFlags
flags
= IteratorFlag::Default)
Constructs a QDirListing that can iterate over path .
You can pass options via flags to control how the directory should be iterated. By default, flags is IteratorFlag::Default .
The listed entries will be filtered according to the file glob patterns in nameFilters , which are converted to a regular expression using QRegularExpression::fromWildcard (见 QDir::setNameFilters () for more details).
For example, the following iterator could be used to iterate over audio files:
QDirListing audioFileIt(u"/home/johndoe/"_s, QStringList{u"*.mp3"_s, u"*.wav"_s}, QDirListing::IteratorFlag::FilesOnly);
Sometimes you can filter by name more efficiently by iterating over the entries with a range-for loop, using string comparison. For example:
using F = QDirListing::IteratorFlag;
const auto flags = F::FilesOnly | F::Recursive | F::ResolveSymlinks;
for (const auto &dirEntry : QDirListing(u"/usr"_s, flags)) {
// Faster than using name filters, filter ".txt" and ".html" files
// using QString API
const QString fileName = dirEntry.fileName();
if (fileName.endsWith(".txt"_L1) || fileName.endsWith(".html"_L1)) {
// ...
}
}
}
另请参阅 IteratorFlags and QDir::setNameFilters ().
[noexcept]
QDirListing::
QDirListing
(
QDirListing
&&
other
)
Move constructor. Moves other into this QDirListing.
注意: The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.
[noexcept]
QDirListing::
~QDirListing
()
销毁 QDirListing .
Returns the set of IteratorFlags used to construct this QDirListing .
Returns the directory path used to construct this QDirListing .
Returns the list of file name glob filters used to construct this QDirListing .
[noexcept]
QDirListing
&QDirListing::
operator=
(
QDirListing
&&
other
)
移动赋值 other 到此 QDirListing .
注意: The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.