Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QDirIterator (Windows) should be case insensitive but not

Tags:

c++

qt

I have a directory with 2 files: Test1.txt and test2.txt

This code should list me the two files:

QDirIterator *it;
QStringList nameFilters;
nameFilters << "t*.txt";
QString path = "C:/temp/test";
qDebug() << "nameFilters" << nameFilters;
it = new QDirIterator(path, nameFilters, QDir::NoFilter, QDirIterator::Subdirectories);
while (it->hasNext()) {
    QString filename = it->next();
    qDebug() << "filename" << filename;
}

but the output is :

nameFilters ("t*.txt") 
filename "C:/temp/test/test2.txt"

Note that this code works well:

QDir dir(path);
qDebug() << dir.entryList(nameFilters, QDir::NoFilter);

Outputs for QDir :

entryList ("Test1.txt", "test2.txt")
like image 645
Jean-Luc Biord Avatar asked Sep 02 '25 08:09

Jean-Luc Biord


1 Answers

The problem is not a bug in the core library but a subtle implementation quirk (?) in the Filter enum of Qt's QDir class.

Usually you will set the QDir::Filter parameter in your QDirIterator constructor to QDir::NoFilter. This enum member is defined to be negative one (all bits set):

NoFilter = -1

Among these bits, there's one called CaseSensitive:

CaseSensitive = 0x800,

This bit gets set when using QDir::NoFilter, but it should be cleared for Windows programs like yours.

I get the desired results by using this kind of code. omitting the QDir::NoFilter flags altogether:

#if _WIN32
    QDir::Filter filter = (QDir::Filter)((int)QDir::Filter::Files | (int)QDir::Filter::Readable);
#else
    QDir::Filter filter = QDir::Filter::NoFilter;
#endif
    QDirIterator it(path, nameFilters, filter, QDirIterator::Subdirectories);
like image 140
Manfred Müller Avatar answered Sep 04 '25 22:09

Manfred Müller