6.0.0-rc4Source

QDirIterator Class

The QDirIterator class provides an iterator for directory entries. More…

Header#include <QDirIterator>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QDirIterator(const QDir &dir, IteratorFlags flags = NoIteratorFlags)
QDirIterator(const QString &path, IteratorFlags flags = NoIteratorFlags)
QDirIterator(const QString &path, QDir::Filters filters, IteratorFlags flags = NoIteratorFlags)
QDirIterator(const QString &path, const QStringList &nameFilters, QDir::Filters filters = QDir::NoFilter, IteratorFlags flags = NoIteratorFlags)
~QDirIterator()
QStringnext()
QFileInfonextFileInfo()
boolhasNext() const
QStringfileName() const
QStringfilePath() const
QFileInfofileInfo() const
QStringpath() const

Detailed Description

You can use QDirIterator to navigate entries of a directory one at a time. It is similar to QDir::entryList() and 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 directory structures. It also supports listing directory contents recursively, and following symbolic links.

QDirIterator it("/etc", QStringList() << "*.conf", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
    qDebug() << it.next();

The path of an entry is the path of its directory joined with its name. Entries come in the order the file system returns them, which is not sorted. With Subdirectories, a directory is returned before the entries it contains, and a directory is entered even if its name does not match the name filters or QDir::Dirs is not set. Hidden directories are entered only with QDir::Hidden. The . and .. entries are returned unless QDir::NoDotAndDotDot is set, but are never entered.

See also QDir and QFileInfo.

Differences from Qt

The entries of one directory are read when the iterator enters it, so the directory is not read lazily entry by entry. Entries are not returned in exactly the order of Qt on every file system, because that depends on the file system. There is no QDirListing-based API.

Member Type Documentation

enum QDirIterator::IteratorFlag

This enum describes flags that you can combine to configure the behavior of QDirIterator.

ConstantDescription
QDirIterator::NoIteratorFlagsThe default value, representing no flags. The iterator will return entries for the assigned path.
QDirIterator::SubdirectoriesList entries inside all subdirectories as well.
QDirIterator::FollowSymlinksWhen combined with Subdirectories, this flag enables iterating through all subdirectories of the assigned path, following all symbolic links. Symbolic link loops (e.g., "link" => "." or "link" => "..") are automatically detected and ignored.

typedef QDirIterator::IteratorFlags

The IteratorFlags type is a typedef for QFlags<IteratorFlag>. It stores an OR combination of IteratorFlag values.

Member Function Documentation

[explicit] QDirIterator::QDirIterator(const QDir &dir, IteratorFlags flags = NoIteratorFlags)

Constructs a QDirIterator that can iterate over the entries of dir, using the name filters and the filters of dir. You can pass options via flags to decide how the directory should be iterated.

By default, flags is NoIteratorFlags, which provides the same behavior as in QDir::entryList().

The sorting of dir is ignored.

[explicit] QDirIterator::QDirIterator(const QString &path, IteratorFlags flags = NoIteratorFlags)

This is an overloaded function.

Constructs a QDirIterator that can iterate over path, with no name filter and QDir::NoFilter as filter. You can pass options via flags to decide how the directory should be iterated.

QDirIterator::QDirIterator(const QString &path, QDir::Filters filters, IteratorFlags flags = NoIteratorFlags)

This is an overloaded function.

Constructs a QDirIterator that can iterate over path. You can pass options via flags to decide how the directory should be iterated and filters to filter entries.

QDirIterator::QDirIterator(const QString &path, const QStringList &nameFilters, QDir::Filters filters = QDir::NoFilter, IteratorFlags flags = NoIteratorFlags)

This is an overloaded function.

Constructs a QDirIterator that can iterate over path, with the name filters nameFilters and the filters filters. You can pass options via flags to decide how the directory should be iterated.

QDirIterator::~QDirIterator()

Destroys the QDirIterator.

QFileInfo QDirIterator::fileInfo() const

Returns a QFileInfo for the current directory entry. If the current entry is invalid (i.e. no entry was returned by next() yet), a default constructed QFileInfo is returned.

See also filePath() and fileName().

QString QDirIterator::fileName() const

Returns the file name for the current directory entry, without the path prepended. If the current entry is invalid (i.e. no entry was returned by next() yet), an empty string is returned.

See also filePath() and fileInfo().

QString QDirIterator::filePath() const

Returns the full file path for the current directory entry. If the current entry is invalid (i.e. no entry was returned by next() yet), an empty string is returned.

See also fileInfo() and fileName().

bool QDirIterator::hasNext() const

Returns true if there is at least one more entry in the directory; otherwise, false is returned.

See also next() and nextFileInfo().

QString QDirIterator::next()

Advances the iterator to the next entry, and returns the file path of this new entry. If hasNext() returns false, this function does nothing, and returns an empty QString.

You can call fileName() or filePath() to get the current entry file name or path, or fileInfo() to get a QFileInfo for the current entry.

See also hasNext(), fileName(), filePath() and fileInfo().

QFileInfo QDirIterator::nextFileInfo()

Advances the iterator to the next entry, and returns the file info of this new entry. If hasNext() returns false, this function does nothing, and returns an empty QFileInfo.

This function was introduced in 6.3.

See also hasNext(), next() and fileInfo().

QString QDirIterator::path() const

Returns the base directory of the iterator.