qtfilesystemsposixqdir

What is the implementation of QDir::count()


All over the internet it seems the accepted way of getting a directory's child count is to loop through its entries manually and count them. This seems far too tedious and generally there are other frameworks and API's that offer a convenience function to get child counts, one of them being QDir::count().

Is there a POSIX API that returns the number of entries in a directory efficiently? If so, I would imagine that this is the implementation of QDir::count(). If not, then perhaps it's done the way so many people are doing it, looping through the entries and counting them if they're appropriate.


Solution

  • Internally, QDir iterates over the directory reading the entries into a list. QDir::count() returns the number of entries in that list (count() will populate the list if that hasn't happened yet).

    See qtbase/src/corelib/io/qdir.cpp for the implementation of