У меня недавно была такая же проблема, и у меня много проблем с родителями и с сопоставлением с исходной моделью.
Моя версия должна обрабатывать виртуальные столбцы слева, некоторые из них связаны с действиями, ивозможно тот, который является флажком.
Надеюсь, это кому-то тоже поможет :)
Тем не менее, примечание для мудрых, я делю QSortFilterProxyModel на подклассы, и, таким образом, я теряю возможность использовать sortЯ думаю, это потому, что я переопределяю методы индекса / данных.Если вместо этого я получу подкласс QIdentityProxyModel, а затем добавлю QSortFilterProxyModel, я вместо этого потеряю возможность отмечать / снимать флажок моего флажка ... даже если для флагов установлено значение Qt :: ItemIsEnabled |Qt :: ItemIsUserCheckable |Qt :: ItemIsEditable ... хитрый кадр:)
QModelIndex GenericProxy::mapToSource(const QModelIndex & proxy) const {
if(not proxy.isValid())
return QModelIndex();
if((action || checkbox)) {
int column = proxy.column() - addedCount();
if(column < 0) // this index is local.
return QModelIndex();
QModelIndex idx = sourceModel()->index(proxy.row(), column, mapToSource(proxy.parent()));
return idx ;
}
QModelIndex idx = sourceModel()->index(proxy.row(), proxy.column(), mapToSource(proxy.parent()));
return idx;
}
QModelIndex GenericProxy::mapFromSource(const QModelIndex & source) const {
if(not source.isValid())
return QModelIndex();
if((action || checkbox)) {
// simply add appropriate informations ..
int column = source.column() + addedCount();
QModelIndex idx = index(source.row(), column, mapFromSource(source.parent()));
return idx;
}
QModelIndex idx = index(source.row(), source.column(), mapFromSource(source.parent()));
return idx;
}
GenericItem * GenericProxy::convert(const QModelIndex & idx) const {
if(idx.isValid())
return _convert(index(idx.row(), firstRealColumn(), idx.parent()));
else
return _convert(idx);
}
// _convert doesn't take care of index not really at the rightplace_ness :)
GenericItem * GenericProxy::_convert(const QModelIndex & index) const {
if(not index.isValid())
return dynamic_cast<GenericModel *>(sourceModel())->convert(QModelIndex());
return static_cast<GenericItem*>(index.internalPointer());
}
QModelIndex GenericProxy::parent(const QModelIndex & item) const {
if(not item.isValid())
return QModelIndex();
GenericItem * child = _convert(item);
if(!child)
return QModelIndex();
GenericItem * parent = child->parentItem();
if(parent == _convert(QModelIndex()))
return QModelIndex();
int column = addedCount();
return sourceModel()->parent(mapToSource(createIndex(item.row(), column, parent )));
}
QModelIndex GenericProxy::index(int row, int column, const QModelIndex & parent) const {
if( not hasIndex(row,column,parent))
return QModelIndex();
GenericItem * pitem = convert(parent);
GenericItem * pchild = pitem->child(row);
if(pchild)
return createIndex(row, column, pchild);
else
return QModelIndex();
}