I have 2 models:
MyModel (inherits QAbstractItemModel, its tree) and MyProxyModel (inherits QSortFilterProxyModel).
Column count of MyModel is 1, and items in MyModel contain information which should be showed in QTableView using MyProxyModel. I used MyProxyModel with MyProxyModel::columnCount() == 5.
I overloaded function MyProxyModel::data(). But table view shows only data from column 1(MyModel::columnCount).
After debugging I found that MyProxyModel::data() gets only indexes with column < MyModel::columnCount() (It seems that it uses MyModel::columnCount() and ignores MyProxyModel::columnCount()).
In the table view header sections count is equal to MyProxyModel::columnCount() (It’s OK ;)).
How can I show information in cells with 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)
}
As Krzysztof Ciebiera had said, in not as many words: your
dataandcolumnCountmethods are never called, because they are not declared correctly. The virtual methods you’re supposed to implement have the signatureswhile your methods have differing signatures
and thus they won’t get called. Notice that your methods incorrectly expect a non-const reference to a model index. Your
data()method also expects non-const object instance.