
Originally Posted by
Seroti
According to your advices and due to the circumstances I have no choice but to touch the existing tree model at the end

.
I didn't say that. Maybe if we had more information about the model, we might suggest a better approach. However based on the vague ascii art you gave us it is not possible to suggest anything better.
While we are at it, can you explain why ? I'm just interested
To create an index you need three values -- the index row, the index column and the index internal value (id or pointer). The only thing that differs between an index with row 0 and column 0 from its parent (or any other index that has rows and columns set to 0) is the internal value. QSortFilterProxyModel keeps an internal mapping of all nodes (twice -- once in each direction) between models to be able to return that value which then allows to find the index in the source model. QIdentityProxyModel in turn returns the internal value of the original model's index. But for that it needs to call mapToSource() with the index in the proxy. Since it doesn't have any information that lets it find the proper index in the base model, it just creates an index (on behalf of the source model) with the same row and column and the internal value and calls parent() on that, maps it back to itself and calls createIndex() using that value. Something along the lines of:
QModelIndex srcParentIndex
= mapToSource
(proxyIndex
).
parent();
QModelIndex parentIndex
= mapFromSource
(srcParentIndex
);
return createIndex(parentIndex.row(), parentIndex.column(), parentIndex.internalId());
}
return sourceModel()->createIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalId()); // createIndex() is protected!
}
QModelIndex Proxy::parent(const QModelIndex &proxyIndex) const {
QModelIndex srcParentIndex = mapToSource(proxyIndex).parent();
QModelIndex parentIndex = mapFromSource(srcParentIndex);
return createIndex(parentIndex.row(), parentIndex.column(), parentIndex.internalId());
}
QModelIndex Proxy::mapToSource(const QModelIndex &proxyIndex) const {
return sourceModel()->createIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalId()); // createIndex() is protected!
}
To copy to clipboard, switch view to plain text mode
You can work around this with a really nasty hack:
public:
QModelIndex myHackedCreateIndex
(int row,
int column, qint64 internalId
) const { return createIndex
(row, column, internalId
);
} };
const MyHackedModel *hackedModel = static_cast<const MyHackedModel*>(sourceModel()); // whaam! h4x0r!
return hackedModel->myHackedCreateIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalId());
}
class MyHackedModel : public QAbstractItemModel {
public:
QModelIndex myHackedCreateIndex(int row, int column, qint64 internalId) const { return createIndex(row, column, internalId); }
};
QModelIndex Proxy::mapToSource(const QModelIndex &proxyIndex) const {
const MyHackedModel *hackedModel = static_cast<const MyHackedModel*>(sourceModel()); // whaam! h4x0r!
return hackedModel->myHackedCreateIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalId());
}
To copy to clipboard, switch view to plain text mode
... but you know, it's a nasty... hack
And if someone asks -- you don't know it from me.
Bookmarks