[SOLVED] result of QDir::entryList()

  • Hi,
    I'm trying to get a list of all files with the suffix ".cpp" and ".h" in a specific folder.
    That is the function I'm using:
    @QDir mydir(dir_path);
    QStringList filters;
    filters << ".cpp" << ".h";
    QStringList files = mydir.entryList(filters);@
    It works fine, but I'm asking myself, why documentation says:
    [quote]Returns a list of the names of all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting().[/quote]I mean, when I pass the nameFilter with a suffix, how can this function ever return directories??
    Also I'm wondering why I need the second argument when using the first one? When I pass a nameFilter (suffixes), then this function should only be able to return files, right? So why should I ever have to pass a Filter like QDir::Files or QDir::Dirs?
    @mydir.entryList(filters, QDir::Dirs);@
    This command does not make any sense, right? So does this function ever need a second argument when the first one (nameFilter) is given? If I only want to get the directories, then I would better use the overloaded function "QDir::entryList(Filters)"...

  • Lifetime Qt Champion


    Having an extension doesn't mean it's a file. Usually it would but take the example of OS X, applications on OS X are bundles which on disk are folders with the app extension e.g. finder.app The same goes for e.g. frameworks and other bundles.

  • Ah, good aspect. I'm not really familiar with OS other than MS Windows.. but that's good to know.

    Maybe one question:
    Assuming that I have subdirectories in my folder, and I'd like (like in the example above) to get all ".cpp" and ".h" files of the folder, including those who are in the subdirectories.
    Is there a simple way to solve this or do I have to get all subdirectories first (Filter --> QDir::Dirs) and then check every subdirectory in a loop with the filter above?

  • Lifetime Qt Champion

    QDirIterator comes to mind

  • Also don't use QDir to get all files from dir.
    I done some test, yesterday actually for my personal project, and results are:
    Test: 1 mil files, each file size 12 bytes, on SSD:

    QFileInfoList flst = QDir( testPath).entryInfoList();
    execution time around 2sec

    QDirIterator dit( testPath);
    while (dit.hasNext()) {
    finfo.append( dit.fileInfo());
    execution time around 1 sec.

    Above results are from Win OS, same test on Lin similar results. Diff is meaningless.

    Dunno why QDir perform so bad. Also QFileSystemModel performs bad on Win, investigating it atm.

  • Thank you both! That's an interesting point, I will try it.

    Btw: Could you maybe tell me how you measured the execution time of those two functions?
    I have already asked that in a different topic a few weeks ago, and there someone told me that measuring execution time is mostly used fo big code chunks, because for such small codes it would be necessary to repeat them a thousand times and divide the resulting time with factor thousand...

    So maybe you have an easier way to measure it, like you did it to compare QDir and QDirIterator... Please tell me! :-)

  • Hello,
    Basically "go big" :).
    Use large test data set for testing so that way You will find out if algorithm / module You wrote behave correctly much faster then doing "small tests" many times.

    Here is my project that I used for testing QDir vs QDirIterator: "link":https://www.dropbox.com/s/n76vuow84wb8mw1/fileTest.7z?dl=0

    NOTE: If you do create 1mil files, application will freeze and test directory "test" will be created in binary directory. Data will occupy around 3,5GB on HDD / SDD.

  • Hi,
    thank you very much for that example. It's a good idea to use a basic "big" code in both cases and only replace the algorithms.
    I'll try it with a few algorithms now, just to check performance of my code :-)

    Thank's again.

  • No problem glad to help.

    Also I need to correct my statement because I did tested entryInfoList() not entryList() and maybe in such case speed is same. But You can now test it out.

  • Oh yes, I didn't even realize that.. well, I'll test it by myself now, but for my purposes, also checking subdirectories, I'll take a look at QDirIterator anyway...

Log in to reply