I am trying to populate a QListWidget (in icon view) with a QFileSystemModel. I want to list folders and only files with specific extension. I want to show the preview of my files as their thumbnail, so I am subclassing QIconProvider class and I am setting this to my model. Before setting my QIconProvider to the model I have already filtered the files that I want on my model, icon(const QFileInfo & info) is called for every file that exists in the listed directory. I have found a work around checking the extension of the file before returning my custom icon, but I am wondering if thre is a way to avoid this.
m_itemsModel = new QFileSystemModel(this);
m_itemsModel->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
m_itemsModel->setRootPath(QDir::homePath());
QStringList filters = formatsList();
m_itemsModel->setNameFilters(filters);
m_itemsModel->setNameFilterDisables(false);
ui.listView->setModel(m_itemsModel);
m_itemsModel->setIconProvider(new ThumbnailIconProvider(QRect(0, 0, 50, 50)));
my QIconProvider:
ThumbnailIconProvider::ThumbnailIconProvider(const QRect &rect)
: QFileIconProvider() {
m_rect = rect;
}
QIcon ThumbnailIconProvider::icon(const QFileInfo & info) const {
static QStringList filters = formatsList();
QString fileName = info.fileName();
QString extension = "*" + fileName.right(fileName.length() - fileName.lastIndexOf("."));
if (info.isFile() && filters.contains(extension)) {
QString path = info.absoluteFilePath();
FileDetails details = fileDetailsFromPathForRect(path, m_rect);
QPixmap pixmap = QPixmap::fromImage(details.image);
QIcon icon(pixmap);
return icon;
}
else
return QFileIconProvider::icon(info);
}
Any ideas please?