Filter QFileInfoList files with Qt

I have a QFileInfoList (list) that contains information about the directory and its file

QFileInfoList list = directory.entryInfoList(); 

How can I apply filters to delete everything except the image file (jpg, gif, png, etc.)?

Here is a simple foreach loop that removes everything that is not a file

 foreach (QFileInfo f, list){ if (!f.isFile()){ list.removeOne(f); } 

How can I apply filters to delete everything except the image file (jpg, gif, png, etc.)?

+7
c ++ qt qt4
source share
2 answers

QDir :: entryInfoList () accepts name filters if you are more comfortable defining images by extension:

 QStringList nameFilter; nameFilter << "*.png" << "*.jpg" << "*.gif"; QFileInfoList list = directory.entryInfoList( nameFilter, QDir::Files ); 
+15
source share

First of all, why don't you use the correct flags for QDir :: entryInfoList and filter out everything that is not a directory and the desired file extensions.

Secondly, you can use `bool QImageReader :: canRead (in each file), but it will access the files to check if they are images, so it will be rather slow.

You can also try to create supported extensions using QImageReader::supportedImageFormats () and add them as a filter in entryInfoList

+2
source share

All Articles