Skip to content

PythonQt.Qt.QDirIterator

QDirIterator

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

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 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()) {
    QString dir = it.next();
    qDebug() << dir;
    // /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.

Source: https://doc.qt.io/archives/qt-5.15/qdiriterator.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
FollowSymlinks attribute Any (shared captured text)
IteratorFlag attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
IteratorFlags attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
NoIteratorFlags attribute Any (shared captured text)
Subdirectories attribute Any (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
fileInfo function (self) -> Any X.fileInfo() -> PythonQt.QtCore.QFileInfo (shared captured text)
fileName function (self) -> str X.fileName() -> str (shared captured text)
filePath function (self) -> str X.filePath() -> str (shared captured text)
hasNext function (self) -> bool X.hasNext() -> bool (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
next function (self) -> str X.next() -> str (shared captured text)
path function (self) -> str X.path() -> str (shared captured text)