首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QSortFilterProxyModel和columnCount

QSortFilterProxyModel和columnCount
EN

Stack Overflow用户
提问于 2012-06-26 12:06:44
回答 1查看 1.1K关注 0票数 2

我有两个模型:MyModel (inherits QAbstractItemModel,它的树)和MyProxyModel (inherits QSortFilterProxyModel)。

MyModel的列计数为1,MyModel中的项包含使用MyProxyModel在QTableView中显示的信息。我用MyProxyModel和MyProxyModel::columnCount() == 5连用。

我重载了函数MyProxyModel::data()。但是表视图只显示来自第1列(MyModel::columnCount)的数据。

调试后,我发现MyProxyModel::data()只使用column < MyModel::columnCount()获取索引(它似乎使用MyModel::columnCount()而忽略了MyProxyModel::columnCount())。

在表视图中,标题部分的计数等于MyProxyModel::columnCount() (可以;)。

如何使用column > MyModel::columnCount()在单元格中显示信息

MyModel.cpp:

代码语言:javascript
复制
int MyModel::columnCount(const QModelIndex& parent) const
{
    return 1;
}

MyProxyModel.cpp:

代码语言:javascript
复制
int MyProxyModel::columnCount(const QModelIndex& parent) const
{
    return 5;
}

QVariant MyProxyModel::data(const QModelIndex& index, int role) const
{
    const int  r = index.row(),
                  c = index.column();
    QModelIndex itemIndex = itemIndex = this->index(r, 0, index.parent());
    itemIndex = mapToSource(itemIndex);
    MyModel model = dynamic_cast<ItemsModel*>(sourceModel());
    Item* item = model->getItem(itemIndex);
    if(role == Qt::DisplayRole)
    {
          if(c == 0)
          {
                return model->data(itemIndex, role);
          }
          return item->infoForColumn(c);
    }
    return QSortFilterProxyModel::data(index, role)
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-26 23:39:00

正如Krzysztof Ciebiera所说,用不了那么多的话:您的datacolumnCount方法从未被调用过,因为它们没有被正确声明。您应该实现的虚拟方法具有以下签名

代码语言:javascript
复制
int columnCount(const QModelIndex&) const;
QVariant data(const QModelIndex&, int) const;

虽然您的方法有不同的签名

代码语言:javascript
复制
int columnCount(QModelIndex&) const;
QVariant data(QModelIndex&, int);

这样他们就不会被叫了。注意,您的方法不正确地期望对模型索引的非const引用。您的data()方法还需要非const对象实例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11206972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档