【发布时间】:2010-11-26 03:39:19
【问题描述】:
好的,我有一个非常基本的QStandardItemModel,里面有一些数字。我设法在QTableView 中显示它,没关系。我创建了一个新模型(QAbstractItemModel 或 QAbstractProxyModel 的子类),它是现有模型的某种层 - 需要设置源模型,并且这个新层应该对真实模型进行一些转换.
我的问题是,在顶层,说“层模型”,data( const QModelIndex & index, int role ) 成员函数从未调用过,但是我想通过角色参数更改显示方法。
这是一个示例代码,它演示了原始模型的data(index,role) 始终被调用,而层模型的data(index,role) 永远不会被调用。为什么? QTableView 对象如何“跳过”顶层的data(index,role)?
#include#include #include MyModel 类:公共 QStandardItemModel { 民众: MyModel(const int r, const int c, QObject* parent = 0) : QStandardItemModel(r,c,parent) {} QVariant 数据 ( const QModelIndex & index, int role = Qt::DisplayRole ) const { qDebug() itemFromIndex(index)->data(role); } }; MyProxyModel 类:公共 QAbstractProxyModel { 民众: MyProxyModel(QObject* parent = 0) : QAbstractProxyModel(parent) {} QModelIndex 索引( int 行,int 列,const QModelIndex & parent = QModelIndex() ) const { 返回 this->sourceModel()->index(row,column,parent); } QModelIndex 父级 ( const QModelIndex & index ) const { 返回 this->sourceModel()->parent(index); } QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const { 返回源索引; } QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const { 返回代理索引; } QVariant 数据 ( const QModelIndex & index, int role = Qt::DisplayRole ) const { qDebug() sourceModel()->data(index,role); } int rowCount ( const QModelIndex & parent = QModelIndex() ) const { 返回 this->sourceModel()->rowCount(parent); } int columnCount ( const QModelIndex & parent = QModelIndex() ) const { 返回 this->sourceModel()->columnCount(parent); } }; int main(int argc, char *argv[]) { QApplication 应用程序(argc,argv); MyModel 模型(8, 2); 我的代理模型我的模型; mymodel.setSourceModel(&model); QTableView 表视图; tableView.setModel(&mymodel); tableView.horizontalHeader()->setStretchLastSection(true); for (int row = 0; row
【问题讨论】:
标签: qt proxy model view chaining