Edward
Edward

Reputation: 304

QSortFilterProxyModel and columnCount

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)
}

Upvotes: 2

Views: 1247

Answers (1)

As Krzysztof Ciebiera had said, in not as many words: your data and columnCount methods are never called, because they are not declared correctly. The virtual methods you're supposed to implement have the signatures

int columnCount(const QModelIndex&) const;
QVariant data(const QModelIndex&, int) const;

while your methods have differing signatures

int columnCount(QModelIndex&) const;
QVariant data(QModelIndex&, int);

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.

Upvotes: 1

Related Questions