Get final QModelIndex in a chain of QAbstractProxyIndex
-
Hi,
[sorry for my bad english...]
I try to get the final QModelIndex in a chain of QAbstractProxyIndex (in fact the first in the chain which is a QAbstractItemModel but not a subclass of QAbstractProxyModel).
This code doesn't work:
QModelIndex BProxy::mapToSource(const QModelIndex &proxyIndex) const { QModelIndex index = proxyIndex; if (index.isValid()) { index = QIdentityProxyModel::mapToSource(index); QAbstractProxyModel *model = qobject_cast<QAbstractProxyModel *>(sourceModel()); while (model) { index = model->mapToSource(index); QAbstractItemModel *sModel = model->sourceModel(); model = qobject_cast<QAbstractProxyModel *>(sModel); } } return index; }
The error is
QSortFilterProxyModel: index from wrong model passed to mapToSource ASSERT: "!"QSortFilterProxyModel: index from wrong model passed to mapToSource"" in file
Do you have an idea where is the problem here?
-
@edlm said:
index = QIdentityProxyModel::mapToSource(index);
mapToSource()
is not a static method. You should call it on an instance of your identity proxy model. -
@Chris-Kawa said:
@edlm said:
index = QIdentityProxyModel::mapToSource(index);
mapToSource()
is not a static method. You should call it on an instance of your identity proxy model.This part doesn't call a static method but the superclass function member (BProxy is a subclass of QIdentityproxyModel).
Anyway I think I found the problem: in my case I should not override mapToSource because the need to get the QModelIndex of the "final" source model is not systematic. Uniquely when my subclass need it. Otherwise the mapToSource has to work as usual. It was a logical problem and the creation and use of another member function mapToDataSource (with the code above) resolved the bug.
Thank for your answer.
-
@edlm said:
his part doesn't call a static method but the superclass function member
Ah yes, sorry. Not my sharpest day I guess ;)
I'm glad you found a solution.