Question

What I'am trying to do: Overwrite QFileSystemModel's setData and data to implement Caching of pictures in the shown directory.

I use a QListView for testing purpose.

Here is the relevant code:

My Class with QFileSystemModel as parent:

.h-file:

#ifndef QPICSINFILESYSTEMMODEL_H
#define QPICSINFILESYSTEMMODEL_H

#include <QFileSystemModel>
#include <QCache>
#include <QDebug>

/* This Model holds all Picturefiles with a cached QPixmap of
 * them.
 */

class PicsInFileSystemModel : public QFileSystemModel
{
public:
    PicsInFileSystemModel();
    QVariant data (const QModelIndex & index, int role);
private:
    QCache<qint64,QPixmap> *cache; //Cache for the pictures

};

#endif // QPICSINFILESYSTEMMODEL_

.cpp-file:

#include "PicsInFileSystemModel.h"

PicsInFileSystemModel::PicsInFileSystemModel()
{
    QStringList myFilter;
    this->setFilter(QDir::Files | QDir::AllDirs);
    this->setRootPath(QDir::rootPath());//QDir::homePath());
    myFilter << "jpeg" << "jpg" << "png";
    //this->setNameFilters(myFilter);
}

/* Reimplement data to send the pictures to the cache */
QVariant PicsInFileSystemModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) {
    qDebug() << "index: " << index << "role: " << role;

    return ((QFileSystemModel)this).data(index,role);
}

How I call the object:

pics = new PicsInFileSystemModel;
form->listViewPictures->setModel(pics);
form->listViewPictures->setRootIndex(pics->index(
        "mypath"));

So here is the question: In my opinion I should see many debug outputs when the View access the Model. But there is nothing. Has anyone an idea what I'am doing wrong?

Thanks!

EDIT: The answers work. I also had to change this

return ((QFileSystemModel)this).data(index,role); 

into

QFileSystemModel::data(index,role))
Was it helpful?

Solution

Signature of the data method is:

QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const

Your method is non-const. Make your method const and mark the variables you need to modify as mutable.

OTHER TIPS

Your data function is never called because it doesn't match the original definition. You are not reimplementing data, you provided a non-const version.

Use QFileIconProvider for that purpose.

ThumbnailIconProvider.h

#ifndef THUMBNAILICONPROVIDER_H
#define THUMBNAILICONPROVIDER_H

#include <QFileIconProvider>

class ThumbnailIconProvider : public QFileIconProvider
{
public:
    ThumbnailIconProvider();

    QIcon icon(const QFileInfo & info) const;
};

#endif // THUMBNAILICONPROVIDER_H

ThumbnailIconProvider.cpp

#include "thumbnailiconprovider.h"

#include <QDebug>

ThumbnailIconProvider::ThumbnailIconProvider()
{
}

QIcon ThumbnailIconProvider::icon(const QFileInfo & info) const
{
    QIcon ico(info.absoluteFilePath());
    if (ico.isNull())
        return QFileIconProvider::icon(info);
    else {
        qDebug() << "Fetch icon for " << info.absoluteFilePath();
        return ico;
    }
}

To use this class call setIconProvider on your model.

QFileSystemModel * model = new QFileSystemModel(this);
model->setIconProvider(new ThumbnailIconProvider());
model->setRootPath(...);
...

Note that you can easily embed cacheing this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top