我有两个模型: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:
int MyModel::columnCount(const QModelIndex& parent) const
{
return 1;
}MyProxyModel.cpp:
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)
}发布于 2012-06-26 23:39:00
正如Krzysztof Ciebiera所说,用不了那么多的话:您的data和columnCount方法从未被调用过,因为它们没有被正确声明。您应该实现的虚拟方法具有以下签名
int columnCount(const QModelIndex&) const;
QVariant data(const QModelIndex&, int) const;虽然您的方法有不同的签名
int columnCount(QModelIndex&) const;
QVariant data(QModelIndex&, int);这样他们就不会被叫了。注意,您的方法不正确地期望对模型索引的非const引用。您的data()方法还需要非const对象实例。
https://stackoverflow.com/questions/11206972
复制相似问题