I have had the same issue recently, and I have many problems with my parents and using the original model.
My version should handle virtual columns on the left, a few related to the actions, and possibly one that is a checkbox.
Hope this helps someone too :)
However, a note to the wise, I will subclass QSortFilterProxyModel, and by doing this I seem to lose the ability to use sorting. I suppose because I override the index / data methods. If earlier I came to a subclass of QIdentityProxyModel and then added QSortFilterProxyModel, I will instead lose the ability to check / uncheck my checkbox column ... although the flags are set to Qt :: ItemIsEnabled | Qt :: ItemIsUserCheckable | Qt :: ItemIsEditable ... more complicated :)
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(); }
source share