我正在做的事: 覆蓋QFileSystemModel的setData和數據以實現在所示目錄中緩存圖片。在Qt的QFileSystemModel中覆蓋數據和setData
我使用QListView進行測試。
下面是相關代碼:
我的班級與QFileSystemModel父:
的.h文件:
#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文件:
#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);
}
如何我打電話給對象:
pics = new PicsInFileSystemModel;
form->listViewPictures->setModel(pics);
form->listViewPictures->setRootIndex(pics->index(
"mypath"));
所以這裏是一個問題:在我看來,當View訪問模型時我應該看到很多調試輸出。但沒有什麼。有沒有人知道我做錯了什麼?
謝謝!編輯: 答案的工作。我也不得不改變這種
return ((QFileSystemModel)this).data(index,role);
到
QFileSystemModel::data(index,role))
謝謝你是這個問題。我也不得不改變這個 return((QFileSystemModel)this).data(index,role); 分成 return((const const QFileSystemModel *)this) - > data(index,role); – Herrbert 2010-02-21 12:06:13
好的。關於多態的講座太早了,正確的語法是:QFileSystemModel :: data(index,role) – Herrbert 2010-02-21 13:29:44