Because QTableView uses the model index to retrieve the data, probably something like this.
QModelIndex index = model->index(row, column, parentIndex);
index.data(Qt::DisplayRole);
And you are returning the model index of the source model instead of an index to your proxy model:
QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
return this->sourceModel()->index(row,column,parent);
}
Try to convert the model index into an index to your proxy model
QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
return this->createIndex(row,column,row);
}
Don't forget to rewrite the map to source and map from source functions.
Solution
class MyTableProxyModel : public QAbstractProxyModel
{
Q_OBJECT
public:
MyTableProxyModel (QObject* parent = 0)
: QAbstractProxyModel(parent) {
}
QModelIndex index(int row, int column, const QModelIndex& parent=QModelIndex()) const {
return createIndex(row,column,row);
}
QModelIndex parent(const QModelIndex &index) const {
//Works only for non-tree models
return QModelIndex();
}
QModelIndex mapFromSource(const QModelIndex &source) const {
return index(source.row(), source.column(), source.parent());
}
QModelIndex mapToSource(const QModelIndex &proxy) const {
return (sourceModel()&&proxy.isValid())
? sourceModel()->index(proxy.row(), proxy.column(), proxy.parent())
: QModelIndex();
}
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const {
qDebug() << "myproxymodel data";
return mapToSource(index).data(role);
}
int rowCount ( const QModelIndex & parent = QModelIndex() ) const {
return sourceModel() ? sourceModel()->rowCount(parent) : 0;
}
int columnCount ( const QModelIndex & parent = QModelIndex() ) const {
return sourceModel() ? sourceModel()->columnCount(parent) : 0;
}
};
TimW
2009-09-07 07:21:43