Sharethefun
Sharethefun

Reputation: 824

Filter QFileInfoList files with Qt

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

QFileInfoList list = directory.entryInfoList();

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

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

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

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

Upvotes: 8

Views: 18722

Answers (2)

Scott
Scott

Reputation: 1189

QDir::entryInfoList() takes name filters, if you're comfortable determining images by extension:

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

Upvotes: 19

Kamil Klimek
Kamil Klimek

Reputation: 13130

First of all, why won't you use proper flags to QDir::entryInfoList and filter out everything that is not a directory, and desired file extensions.

Secondly, you may use on each file `bool QImageReader::canRead(, but it will access files to check if they're images, so it will be quite slow.

Also you may try to build up supported extensions with QImageReader::supportedImageFormats (), and add them as filter to entryInfoList

Upvotes: 2

Related Questions