16

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?

gollumullog
  • 1,249
  • 2
  • 14
  • 22

2 Answers2

23

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.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
4

Also, if you want to show files with more than one kind of extensions, you can do like the following.

export_folder.setNameFilters( QStringList() << "*.exe" << "*.pdf" << "*.docx" << "*.jpg" );
Casper
  • 4,435
  • 10
  • 41
  • 72