QDirIterator Class

The QDirIterator class provides an iterator for directory entrylists. 更多...

頭: #include <QDirIterator>
qmake: QT += core
Since: Qt 4.3

公共類型

enum IteratorFlag { NoIteratorFlags, Subdirectories, FollowSymlinks }
flags IteratorFlags

公共函數

QDirIterator (const QDir & dir , QDirIterator::IteratorFlags flags = NoIteratorFlags)
QDirIterator (const QString & path , QDirIterator::IteratorFlags flags = NoIteratorFlags)
QDirIterator (const QString & path , QDir::Filters filters , QDirIterator::IteratorFlags flags = NoIteratorFlags)
QDirIterator (const QString & path , const QStringList & nameFilters , QDir::Filters filters = QDir::NoFilter, QDirIterator::IteratorFlags flags = NoIteratorFlags)
~QDirIterator ()
QFileInfo fileInfo () const
QString fileName () const
QString filePath () const
bool hasNext () const
QString next ()
QString path () const

詳細描述

The QDirIterator class provides an iterator for directory entrylists.

可以使用 QDirIterator 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 (), 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:

QDirIterator it("/etc", QDirIterator::Subdirectories);
while (it.hasNext()) {
    qDebug() << it.next();
    // /etc/.
    // /etc/..
    // /etc/X11
    // /etc/X11/fs
    // ...
}
					

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

QDirIterator it("/sys", QStringList() << "scaling_cur_freq", QDir::NoFilter, QDirIterator::Subdirectories);
while (it.hasNext()) {
    QFile f(it.next());
    f.open(QIODevice::ReadOnly);
    qDebug() << f.fileName() << f.readAll().trimmed().toDouble() / 1000 << "MHz";
}
					

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.

另請參閱 QDir and QDir::entryList ().

成員類型文檔編製

enum QDirIterator:: IteratorFlag
flags QDirIterator:: IteratorFlags

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

常量 描述
QDirIterator::NoIteratorFlags 0x0 The default value, representing no flags. The iterator will return entries for the assigned path.
QDirIterator::Subdirectories 0x2 List entries inside all subdirectories as well.
QDirIterator::FollowSymlinks 0x1 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.

IteratorFlags 類型是 typedef 對於 QFlags <IteratorFlag>。它存儲 IteratorFlag 值的 OR 組閤。

成員函數文檔編製

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

構造 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.

默認情況下, flags is NoIteratorFlags , which provides the same behavior as in QDir::entryList ().

The sorting in dir 被忽略。

注意: To list symlinks that point to non existing files, QDir::System must be passed to the flags.

另請參閱 hasNext (), next (),和 IteratorFlags .

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

構造 QDirIterator that can iterate over path . You can pass options via flags to decide how the directory should be iterated.

默認情況下, flags is NoIteratorFlags , which provides the same behavior as in QDir::entryList ().

注意: To list symlinks that point to non existing files, QDir::System must be passed to the flags.

另請參閱 hasNext (), next (),和 IteratorFlags .

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

構造 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.

默認情況下, filters is QDir::NoFilter ,和 flags is NoIteratorFlags , which provides the same behavior as in QDir::entryList ().

注意: To list symlinks that point to non existing files, QDir::System must be passed to the flags.

另請參閱 hasNext (), next (),和 IteratorFlags .

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

構造 QDirIterator that can iterate over path ,使用 nameFilters and filters . You can pass options via flags to decide how the directory should be iterated.

默認情況下, flags is NoIteratorFlags , which provides the same behavior as QDir::entryList ().

注意: To list symlinks that point to non existing files, QDir::System must be passed to the flags.

另請參閱 hasNext (), next (),和 IteratorFlags .

QDirIterator:: ~QDirIterator ()

銷毀 QDirIterator .

QFileInfo QDirIterator:: fileInfo () const

返迴 QFileInfo for the current directory entry.

另請參閱 filePath () 和 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.

另請參閱 filePath () 和 fileInfo ().

QString QDirIterator:: filePath () const

Returns the full file path for the current directory entry.

另請參閱 fileInfo () 和 fileName ().

bool QDirIterator:: hasNext () const

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

另請參閱 next (), fileName (), filePath (),和 fileInfo ().

QString QDirIterator:: next ()

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

可以調用 fileName () 或 filePath () to get the current entry file name or path, or fileInfo () to get a QFileInfo for the current entry.

另請參閱 hasNext (), fileName (), filePath (),和 fileInfo ().

QString QDirIterator:: path () const

Returns the base directory of the iterator.