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

3

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?

Bardwell answered 21/3, 2013 at 16:42 Comment(0)
K
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.

Kramlich answered 21/3, 2013 at 16:55 Comment(0)
S
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" );
Styx answered 1/8, 2014 at 5:23 Comment(0)
S
0

Try this

export_folder.setNameFilters(QStringList {"*.exe1 *.exe2 *.exe3", "EXE1 (*.exe1)", "EXE23 (*.exe2 *.exe3)"});
Strobilaceous answered 19/2 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.