QDir.setNameFilter how to show only files with specific extensions?

gollumullog picture gollumullog · Mar 21, 2013 · Viewed 20.6k times · Source

setNameFilters isn't working as I would expect, so if anyone can explain if I'm using it incorrectly, or if maybe this is a bug in Qt:

Here is my code sample:

QDir export_folder("C:\path");
QStringList fileList = export_folder.setNameFilters(QStringList()<<"*.exe");

after processing fileList contains the String "test.exe1"

I would expect fileList to only include files with extension .exe NOT .exe*.

If I wanted file extensions longer than .exe I would expect to put in "*.exe*" as my filter.

Can someone help clarify, or do I manually have to process my fileList after the fact?

Answer

user362638 picture user362638 · Mar 21, 2013

For starters, setNameFilters does not return QStringList, it returns nothing. Your code should look like this:

QDir export_folder("C:\\path");
export_folder.setNameFilters(QStringList()<<"*.exe");
QStringList fileList = export_folder.entryList();

Filtering works as expected (not returning files ending with "exe2") in Linux with Qt 5.0.1.