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")
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With