QDir entryList function strange behavior

610 views Asked by At

I'm using VS2019, Qt 5.15.2

I'm using entryList method of the class QDir on the following directory:

enter image description here

The following behavior of the entryList method with the QDir::NoDotAndDotDot parameter is strange:

tTe following example is self explanatory: My code :


    LOG( INFO ) << "entryList with no parameter";
    foreach( auto elementName, p_dir.entryList() )
    {
        LOG( INFO ) << "elementName : " << elementName.toStdString();
    }

    LOG( INFO ) << "entryList with QDir::NoDotAndDotDot";
    foreach( auto elementName, p_dir.entryList( QDir::NoDotAndDotDot ) )
    {
        LOG( INFO ) << "elementName : " << elementName.toStdString();
    }

The output is as follows:

2021-04-01-10:29:33,482724 | INFO            | entryList with no parameter | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:58
2021-04-01-10:29:33,482773 | INFO            | elementName : . | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482805 | INFO            | elementName : .. | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482844 | INFO            | elementName : ABDOMEN | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482876 | INFO            | elementName : CHEST | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482913 | INFO            | elementName : CSPINE | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482944 | INFO            | elementName : ELBOW | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,482985 | INFO            | elementName : FOOT | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483019 | INFO            | elementName : HAND | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483053 | INFO            | elementName : KNEE | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483090 | INFO            | elementName : LSPINE | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483123 | INFO            | elementName : PELVIS | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483155 | INFO            | elementName : RIB | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483186 | INFO            | elementName : SHOULDER | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483221 | INFO            | elementName : SKULL | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483252 | INFO            | elementName : TSPINE | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:61
2021-04-01-10:29:33,483284 | INFO            | entryList with QDir::NoDotAndDotDot | xxxxxxxxx.cpp->RecursiveProcessingFilesFinder:64

I don't catch why, if I use the filter QDir::NoDotAndDotDot, entryList output is now empty ???

1

There are 1 answers

2
Pat. ANDRIA On

Your question is :

I don't catch why, if I use the filter QDir::NoDotAndDotDot

The official documentation of QDir::entryList states that

it returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.

When you are doing p_dir.entryList(), the defaulft filter is NoFilter so it is normal to display all things. The normal behavior is NoFilter which means displaying all things including . (Dot) and .. (DotDot)

When you are doing p_dir.entryList( QDir::NoDotAndDotDot ), the filter is set to QDir::NoDotAndDotDot which means "Do not list the special entries "." and ".."." (see description of QDir::Filters and then nothing more matches the specification of the filter thus, it outputs an empty list.

To display all things without the line entryList with QDir::NoDotAndDotDot..., you should do:

foreach( auto elementName, p_dir.entryList( QDir::AllDirs | QDir::Files |QDir::NoDotAndDotDot )
{
    LOG( INFO ) << "elementName : " << elementName.toStdString();
}

This allows for displaying:

  • QDir::AllDirs - List all directories; i.e. don't apply the filters to directory names.
  • QDir::Files - List files.
  • QDir::NoDotAndDotDot- Do not list the special entries "." and "..".