Qt 4.8
Public Types | Public Functions | Properties | Friends | List of all members
QDirIterator Class Reference

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

#include <qdiriterator.h>

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 Functions

QFileInfo fileInfo () const
 Returns a QFileInfo for the current directory entry. More...
 
QString fileName () const
 Returns the file name for the current directory entry, without the path prepended. More...
 
QString filePath () const
 Returns the full file path for the current directory entry. More...
 
bool hasNext () const
 Returns true if there is at least one more entry in the directory; otherwise, false is returned. More...
 
QString next ()
 Advances the iterator to the next entry, and returns the file path of this new entry. More...
 
QString path () const
 Returns the base directory of the iterator. More...
 
 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. More...
 
 QDirIterator (const QString &path, IteratorFlags flags=NoIteratorFlags)
 Constructs a QDirIterator that can iterate over path. More...
 
 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. More...
 
 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. More...
 
virtual ~QDirIterator ()
 Destroys the QDirIterator. More...
 

Properties

QScopedPointer< QDirIteratorPrivated
 

Friends

class QDir
 

Detailed Description

The QDirIterator class provides an iterator for directory entrylists.

Since
4.3

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() << it.next();
// /etc/.
// /etc/..
// /etc/X11
// /etc/X11/fs
// ...
}

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

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

QDirIterator works with all supported file engines, and is implemented using QAbstractFileEngineIterator.

See also
QDir, QDir::entryList(), QAbstractFileEngineIterator

Definition at line 54 of file qdiriterator.h.

Enumerations

◆ IteratorFlag

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

  • NoIteratorFlags The default value, representing no flags. The iterator will return entries for the assigned path.
  • Subdirectories List entries inside all subdirectories as well.
  • 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 56 of file qdiriterator.h.

Constructors and Destructors

◆ 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 424 of file qdiriterator.cpp.

425 {
426  // little trick to get hold of the QDirPrivate while there is no API on QDir to give it to us
427  class MyQDir : public QDir { public: const QDirPrivate *priv() const { return d_ptr.constData(); } };
428  const QDirPrivate *other = static_cast<const MyQDir*>(&dir)->priv();
429  d.reset(new QDirIteratorPrivate(other->dirEntry, other->nameFilters, other->filters, flags, !other->fileEngine.isNull()));
430 }
bool isNull() const
Returns true if this object is holding a pointer that is null.
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
QFileSystemEntry dirEntry
Definition: qdir_p.h:91
QScopedPointer< QDirIteratorPrivate > d
Definition: qdiriterator.h:87
QScopedPointer< QAbstractFileEngine > fileEngine
Definition: qdir_p.h:85
void reset(T *other=0)
Deletes the existing object it is pointing to if any, and sets its pointer to other.
QStringList nameFilters
Definition: qdir_p.h:76
static const QMetaObjectPrivate * priv(const uint *data)
QDir::Filters filters
Definition: qdir_p.h:78

◆ 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 462 of file qdiriterator.cpp.

464 {
465 }
QScopedPointer< QDirIteratorPrivate > d
Definition: qdiriterator.h:87
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ 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 445 of file qdiriterator.cpp.

446  : d(new QDirIteratorPrivate(QFileSystemEntry(path), QStringList(), filters, flags))
447 {
448 }
QScopedPointer< QDirIteratorPrivate > d
Definition: qdiriterator.h:87
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static const char *const filters[3]

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

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 480 of file qdiriterator.cpp.

482  : d(new QDirIteratorPrivate(QFileSystemEntry(path), nameFilters, filters, flags))
483 {
484 }
QScopedPointer< QDirIteratorPrivate > d
Definition: qdiriterator.h:87
static const char *const filters[3]

◆ ~QDirIterator()

QDirIterator::~QDirIterator ( )
virtual

Destroys the QDirIterator.

Definition at line 489 of file qdiriterator.cpp.

490 {
491 }

Functions

◆ fileInfo()

QFileInfo QDirIterator::fileInfo ( ) const

Returns a QFileInfo for the current directory entry.

See also
filePath(), fileName()

Definition at line 557 of file qdiriterator.cpp.

Referenced by QNetworkDiskCache::expire(), QDirPrivate::initFileLists(), and QSslSocketPrivate::systemCaCertificates().

558 {
559  return d->currentFileInfo;
560 }
QScopedPointer< QDirIteratorPrivate > d
Definition: qdiriterator.h:87

◆ 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 537 of file qdiriterator.cpp.

Referenced by QAbstractFileEngine::entryList(), and qmlFilesInDirectory().

538 {
539  return d->currentFileInfo.fileName();
540 }
QString fileName() const
Returns the name of the file, excluding the path.
Definition: qfileinfo.cpp:726
QScopedPointer< QDirIteratorPrivate > d
Definition: qdiriterator.h:87

◆ filePath()

QString QDirIterator::filePath ( ) const

Returns the full file path for the current directory entry.

See also
fileInfo(), fileName()

Definition at line 547 of file qdiriterator.cpp.

Referenced by next().

548 {
549  return d->currentFileInfo.filePath();
550 }
QScopedPointer< QDirIteratorPrivate > d
Definition: qdiriterator.h:87
QString filePath() const
Returns the file name, including the path (which may be absolute or relative).
Definition: qfileinfo.cpp:707

◆ 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(), fileName(), filePath(), fileInfo()

Definition at line 515 of file qdiriterator.cpp.

Referenced by QSslSocketPrivate::ensureCiphersAndCertsLoaded(), QAbstractFileEngine::entryList(), QNetworkDiskCache::expire(), QSslCertificate::fromPath(), QDirPrivate::initFileLists(), qmlFilesInDirectory(), QFileSystemModel::remove(), and QSslSocketPrivate::systemCaCertificates().

516 {
517  if (d->engine)
518  return !d->fileEngineIterators.isEmpty();
519  else
520 #ifndef QT_NO_FILESYSTEMITERATOR
521  return !d->nativeIterators.isEmpty();
522 #else
523  return false;
524 #endif
525 }
QScopedPointer< QDirIteratorPrivate > d
Definition: qdiriterator.h:87
QDirIteratorPrivateIteratorStack< QAbstractFileEngineIterator > fileEngineIterators
QScopedPointer< QAbstractFileEngine > engine
QDirIteratorPrivateIteratorStack< QFileSystemIterator > nativeIterators
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139

◆ 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 a null 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(), fileInfo()

Definition at line 503 of file qdiriterator.cpp.

Referenced by QAbstractFileEngine::entryList(), QNetworkDiskCache::expire(), QSslCertificate::fromPath(), QDirPrivate::initFileLists(), qmlFilesInDirectory(), QFileSystemModel::remove(), and QSslSocketPrivate::systemCaCertificates().

504 {
505  d->advance();
506  return filePath();
507 }
QScopedPointer< QDirIteratorPrivate > d
Definition: qdiriterator.h:87
QString filePath() const
Returns the full file path for the current directory entry.

◆ path()

QString QDirIterator::path ( ) const

Returns the base directory of the iterator.

Definition at line 565 of file qdiriterator.cpp.

566 {
567  return d->dirEntry.filePath();
568 }
QScopedPointer< QDirIteratorPrivate > d
Definition: qdiriterator.h:87
QString filePath() const
QFileSystemEntry dirEntry

Friends and Related Functions

◆ QDir

friend class QDir
friend

Definition at line 88 of file qdiriterator.h.

Properties

◆ d

QScopedPointer<QDirIteratorPrivate> QDirIterator::d
private

Definition at line 87 of file qdiriterator.h.

Referenced by fileInfo(), fileName(), filePath(), hasNext(), next(), and path().


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