
Originally Posted by
mentat
I actually meant that the proxy is considered a wrapper..at least that's how it is documented, but I'll give your suggestion a try.
It is a wrapper in that way that it transforms the already existing model to provide a new hierarchy.
In general, I've had a lot of trouble with proxymodels, and understanding how they are related with the underlying model. For instance, for the life of me, I can not get a tablular view of the data by re-implementing mapFromSource and mapToSource to translate or compress the underlying hierarchical model into a tabular model. I guess my question becomes: when are these map*Source functions called when the view is being built since obviously the QTableView itself doesn't call them? Is there any better documentation or real examples of M/V then those on the Qt site? In general, when it comes from starting from square one as I am, it is extremely difficult to figure out which end is up based on a few sentences here or there.
QTableView doesn't have to call them. It calls data() to read the data from the model. And then the proxy should map the request (for example the index) using methods you mentioned to fetch the data from the underlying model and give that data back to the object which requested it (like the view in this case).
I belive that if you wanted to make a simple transformation of the model where you'd like to transpose the model (exchange columns with rows), you'd have to implement such a proxy:
public:
return index(sourceIndex.column(), sourceIndex.row());
}
return sourceModel()->index(proxyIndex.column(), proxyIndex.row());
}
return createIndex(r,c);
}
}
return sourceModel()->columnCount();
}
return sourceModel()->rowCount();
}
return sourceModel()->data(mapToSource(ind), role);
}
};
class TransposeProxyModel : public QAbstractProxyModel{
public:
TransposeProxyModel(QObject *p = 0) : QAbstractProxyModel(p){}
QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const{
return index(sourceIndex.column(), sourceIndex.row());
}
QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const{
return sourceModel()->index(proxyIndex.column(), proxyIndex.row());
}
QModelIndex index(int r, int c, const QModelIndex &ind) const{
return createIndex(r,c);
}
QModelIndex parent(const QModelIndex&) const {
return QModelIndex();
}
int rowCount(const QModelIndex &) const{
return sourceModel()->columnCount();
}
int columnCount(const QModelIndex &) const{
return sourceModel()->rowCount();
}
QVariant data(const QModelIndex &ind, int role) const {
return sourceModel()->data(mapToSource(ind), role);
}
};
To copy to clipboard, switch view to plain text mode
Edit: I forgot to implement the rest of pure virtual methods
Bookmarks