diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2021-07-11 19:28:35 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2021-07-28 15:16:42 +0200 |
commit | 25fff849e8f34af6d41ff36f2891bb4099b89360 (patch) | |
tree | d34bb46cd97d25bd2abeee68c9060a1a9b40ff22 /src/corelib/io/qdiriterator.cpp | |
parent | 8ccd5d5af295ae36440157fe1d00a176fdf1c6bf (diff) |
QDirIterator: add nextFileInfo()
Before this change, next() was the only way to advance the iterator,
whether the caller was ultimately interested in just the filePath()
(good) or not (bad luck, had to call .fileInfo()).
Add a new function, nextFileInfo(), with returns fileInfo() instead.
Incidentally, the returned object has already been constructed as part
of advance()ing the iterator, so the new function is faster than
next() even if the result is ignored, because we're not calculating a
QString result the caller may not be interested in.
Use the new function around the code.
Fix a couple of cases of next(); fileInfo().filePath() (just use
next()'s return value) as a drive-by.
[ChangeLog][QtCore][QDirIterator] Added nextFileInfo(), which is like
next(), but returns fileInfo() instead of filePath().
Change-Id: I601220575961169b44139fc55b9eae6c3197afb4
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/io/qdiriterator.cpp')
-rw-r--r-- | src/corelib/io/qdiriterator.cpp | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/src/corelib/io/qdiriterator.cpp b/src/corelib/io/qdiriterator.cpp index 792f2a863d..7e86aacdb1 100644 --- a/src/corelib/io/qdiriterator.cpp +++ b/src/corelib/io/qdiriterator.cpp @@ -60,11 +60,11 @@ \snippet code/src_corelib_io_qdiriterator.cpp 1 - 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. + 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 @@ -490,10 +490,12 @@ QDirIterator::~QDirIterator() new entry. If hasNext() returns \c false, this function does nothing, and returns an empty QString. - You can call fileName() or filePath() to get the current entry file name + 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. - \sa hasNext(), fileName(), filePath(), fileInfo() + Call nextFileInfo() instead of next() if you're interested in the QFileInfo. + + \sa hasNext(), nextFileInfo(), fileName(), filePath(), fileInfo() */ QString QDirIterator::next() { @@ -502,10 +504,30 @@ QString QDirIterator::next() } /*! + \since 6.3 + + Advances the iterator to the next entry, and returns the file info of this + new entry. If hasNext() returns \c 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(). + + \sa hasNext(), fileName(), filePath(), fileInfo() +*/ +QFileInfo QDirIterator::nextFileInfo() +{ + d->advance(); + return fileInfo(); +} + +/*! Returns \c true if there is at least one more entry in the directory; otherwise, false is returned. - \sa next(), fileName(), filePath(), fileInfo() + \sa next(), nextFileInfo(), fileName(), filePath(), fileInfo() */ bool QDirIterator::hasNext() const { |