views:

202

answers:

2

Hi,

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))
+2  A: 

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.

Lukáš Lalinský
Thanks this was the problem. I also had to change this return ((QFileSystemModel)this).data(index,role);into return ((const QFileSystemModel *)this)->data(index,role);
Herrbert
ok. the lecture about polymorphism was too long ago the correct syntax is: QFileSystemModel::data(index,role)
Herrbert
+2  A: 

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.

Sofahamster