Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QDirIterator Class Reference

The QDirIterator class provides an iterator for directory entrylists. More...

#include <qdiriterator.h>

+ Collaboration diagram for QDirIterator:

Public Types

enum  IteratorFlag { NoIteratorFlags = 0x0 , FollowSymlinks = 0x1 , Subdirectories = 0x2 }
 This enum describes flags that you can combine to configure the behavior of QDirIterator. More...
 

Public Member Functions

 QDirIterator (const QDir &dir, IteratorFlags flags=NoIteratorFlags)
 Constructs a QDirIterator that can iterate over dir's entrylist, using dir's name filters and regular filters.
 
 QDirIterator (const QString &path, IteratorFlags flags=NoIteratorFlags)
 Constructs a QDirIterator that can iterate over path.
 
 QDirIterator (const QString &path, QDir::Filters filter, IteratorFlags flags=NoIteratorFlags)
 Constructs a QDirIterator that can iterate over path, with no name filtering and filters for entry filtering.
 
 QDirIterator (const QString &path, const QStringList &nameFilters, QDir::Filters filters=QDir::NoFilter, IteratorFlags flags=NoIteratorFlags)
 Constructs a QDirIterator that can iterate over path, using nameFilters and filters.
 
 ~QDirIterator ()
 Destroys the QDirIterator.
 
QString next ()
 Advances the iterator to the next entry, and returns the file path of this new entry.
 
QFileInfo nextFileInfo ()
 
bool hasNext () const
 Returns true if there is at least one more entry in the directory; otherwise, false is returned.
 
QString fileName () const
 Returns the file name for the current directory entry, without the path prepended.
 
QString filePath () const
 Returns the full file path for the current directory entry.
 
QFileInfo fileInfo () const
 Returns a QFileInfo for the current directory entry.
 
QString path () const
 Returns the base directory of the iterator.
 

Friends

class QDir
 

Detailed Description

The QDirIterator class provides an iterator for directory entrylists.

Since
4.3

\inmodule QtCore

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 directories. It also supports listing directory contents recursively, and following symbolic links. Unlike QDir::entryList(), QDirIterator does not support sorting.

The QDirIterator constructor takes a QDir or a directory as argument. After construction, the iterator is located before the first directory entry. Here's how to iterate over all the entries sequentially:

while (it.hasNext()) {
qDebug() << dir;
// /etc/.
// /etc/..
// /etc/X11
// /etc/X11/fs
// ...
}

Here's how to find and read all files filtered by name, recursively:

while (it.hasNext()) {
QFile f(it.next());
qDebug() << f.fileName() << f.readAll().trimmed().toDouble() / 1000 << "MHz";
}

The next() and nextFileInfo() functions advance the iterator and return the path or the QFileInfo of the next directory entry. You can also call filePath() or fileInfo() to get the current file path or QFileInfo without first advancing the iterator. The fileName() function returns only the name of the file, similar to how QDir::entryList() works.

Unlike Qt's container iterators, QDirIterator is uni-directional (i.e., you cannot iterate directories in reverse order) and does not allow random access.

Note
This class is deprecated and may be removed in a Qt release. Use QDirListing instead.
See also
QDir, QDir::entryList()

Definition at line 12 of file qdiriterator.h.

Member Enumeration Documentation

◆ IteratorFlag

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

\value NoIteratorFlags The default value, representing no flags. The iterator will return entries for the assigned path.

\value Subdirectories List entries inside all subdirectories as well.

\value FollowSymlinks When 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.

Enumerator
NoIteratorFlags 
FollowSymlinks 
Subdirectories 

Definition at line 15 of file qdiriterator.h.

Constructor & Destructor Documentation

◆ QDirIterator() [1/4]

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

Constructs a QDirIterator that can iterate over dir's entrylist, using dir's name filters and regular filters.

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 in dir is ignored.

Note
To list symlinks that point to non existing files, QDir::System must be passed to the flags.
See also
hasNext(), next(), IteratorFlags

Definition at line 164 of file qdiriterator.cpp.

◆ QDirIterator() [2/4]

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

Constructs a QDirIterator that can iterate over path.

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().

Note
To list symlinks that point to non existing files, QDir::System must be passed to the flags.
See also
hasNext(), next(), IteratorFlags

Definition at line 199 of file qdiriterator.cpp.

◆ QDirIterator() [3/4]

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

Constructs a QDirIterator that can iterate over path, with no name filtering and filters for entry filtering.

You can pass options via flags to decide how the directory should be iterated.

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

Note
To list symlinks that point to non existing files, QDir::System must be passed to the flags.
See also
hasNext(), next(), IteratorFlags

Definition at line 182 of file qdiriterator.cpp.

◆ QDirIterator() [4/4]

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

Constructs a QDirIterator that can iterate over path, using nameFilters and filters.

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 QDir::entryList().

For example, the following iterator could be used to iterate over audio files:

Note
To list symlinks that point to non existing files, QDir::System must be passed to the flags.
See also
hasNext(), next(), IteratorFlags, QDir::setNameFilters()

Definition at line 222 of file qdiriterator.cpp.

◆ ~QDirIterator()

QDirIterator::~QDirIterator ( )

Destroys the QDirIterator.

Definition at line 231 of file qdiriterator.cpp.

Member Function Documentation

◆ fileInfo()

QFileInfo QDirIterator::fileInfo ( ) const

Returns a QFileInfo for the current directory entry.

See also
filePath(), fileName()

Definition at line 314 of file qdiriterator.cpp.

References QDirIteratorPrivate::currentFileInfo.

◆ fileName()

QString QDirIterator::fileName ( ) const

Returns the file name for the current directory entry, without the path prepended.

This function is convenient when iterating a single directory. When using the QDirIterator::Subdirectories flag, you can use filePath() to get the full path.

See also
filePath(), fileInfo()

Definition at line 294 of file qdiriterator.cpp.

References QDirIteratorPrivate::currentFileInfo, and QFileInfo::fileName().

+ Here is the call graph for this function:

◆ filePath()

QString QDirIterator::filePath ( ) const

Returns the full file path for the current directory entry.

See also
fileInfo(), fileName()

Definition at line 304 of file qdiriterator.cpp.

References QDirIteratorPrivate::currentFileInfo, and QFileInfo::filePath().

+ Here is the call graph for this function:

◆ hasNext()

bool QDirIterator::hasNext ( ) const

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

See also
next(), nextFileInfo(), fileName(), filePath(), fileInfo()

Definition at line 279 of file qdiriterator.cpp.

References QDirListing::end(), QDirIteratorPrivate::it, and QDirIteratorPrivate::lister.

+ Here is the call graph for this function:

◆ next()

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's file name or path, or fileInfo() to get a QFileInfo for the current entry.

Call nextFileInfo() instead of next() if you're interested in the QFileInfo.

See also
hasNext(), nextFileInfo(), fileName(), filePath(), fileInfo()

Definition at line 247 of file qdiriterator.cpp.

References QDirIteratorPrivate::advance(), QDirIteratorPrivate::currentFileInfo, and QFileInfo::filePath().

Referenced by while().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nextFileInfo()

QFileInfo QDirIterator::nextFileInfo ( )
Since
6.3

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.

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

Call next() instead of nextFileInfo() when all you need is the filePath().

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

Definition at line 267 of file qdiriterator.cpp.

References QDirIteratorPrivate::advance(), and QDirIteratorPrivate::currentFileInfo.

+ Here is the call graph for this function:

◆ path()

QString QDirIterator::path ( ) const

Returns the base directory of the iterator.

Definition at line 322 of file qdiriterator.cpp.

References QDirListing::iteratorPath(), and QDirIteratorPrivate::lister.

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ QDir

friend class QDir
friend

Definition at line 48 of file qdiriterator.h.


The documentation for this class was generated from the following files: