PDA

View Full Version : Reimpementing mapFromSource and mapToSource of QidentityProxyModel



Guett_31
22nd March 2014, 08:42
Hello,

I'm trying to alter a QFileSystemModel by means of a subclassed QidentityProyModel. The alteration consists in removing all rows but one at a specific location in the proxy model, and I don't want any modifications in the source model.
I tried to reimplement the mapToSource and mapFromSource methods that way:

QModelIndex RootItemProxyModel::mapFromSource(const QModelIndex & sourceIdx) const
{
// If the proxy is not activated, use the base mapFromSource function.
if (m_ActivatedProxy == false)
return IdentityFileSystemProxyModel::mapFromSource(source Idx);

// Alter the mapping so the proxy model sees only m_rootSrcIdx's row as m_rootSrcParentIdx child.
if (sourceIdx.parent().internalPointer() == m_rootSrcParentIdx.internalPointer())
{
if (sourceIdx.row() != 0)
return QModelIndex();
else
return createIndex(0,
sourceIdx.column(),
sourceModel()->sibling(m_rootSrcIdx.row(),sourceIdx.column(),m_ro otSrcIdx).internalPointer());
}
// In any other cases, use the base mapFromSource function.
return IdentityFileSystemProxyModel::mapFromSource(source Idx);
}


QModelIndex RootItemProxyModel::mapToSource(const QModelIndex & proxyIndex) const
{
// If the proxy is not activated, use the base mapToSource function.
if (m_ActivatedProxy == false)
return IdentityFileSystemProxyModel::mapToSource(proxyInd ex);

// Alter the mapping so the proxy model sees only m_rootSrcIdx's row as m_rootSrcParentIdx child.
QModelIndex sourceIdx = IdentityFileSystemProxyModel::mapToSource(proxyInd ex);
if (sourceIdx.parent().internalPointer() == m_rootSrcParentIdx.internalPointer())
{
if (proxyIndex.row() != 0)
return QModelIndex();
else
return sourceModel()->sibling(m_rootSrcIdx.row(),
proxyIndex.column(),
m_rootSrcIdx);
}
// In any other cases, use the base mapToSource function.
return sourceIdx;
}
The only examples I could find about mapToSource and mapFromSource reimplemented methods were for transpose proxy models in which each Modelindex in the Source model maps to a Modelindex in the Proxy model. In my case, the Proxy model has less Modelindexes than the SourceModel. I return QModelIndex() for any ModelIndex that maps to, or from, a ModelIndex that belong to the rows I want to remove.
I'm not sure that it is what I should return in that case. I'm not even sure that my overall strategy is correct.

If someone experienced can put me on the right track, that would be great.
Thanks.

anda_skoa
22nd March 2014, 11:50
Let me try to understand this: you want the proxy model to filter out rows, yet you are using the QIdentityProxyModel as a base class, which works with the base assumption that it does not change the data's structure.
Hence it being called QIdentityProxyModel.

Wouldn't it be much easier to base this on QSortFilterProxyModel and implement filterAcceptsRow()?

Cheers,
_

Guett_31
22nd March 2014, 16:26
Ideally I would use a QSortFilterProxyModel as you suggested. My problem is that other QIdentityProxyModels would be chained to that QSortFilterProxyModel for subsequent data processing in my project. According to my limited Qt experience, QSortFilterProxyModels and QIdentityProxyModels don't go well together. I reported a bug about that 9 month ago and it has not even been evaluated yet: QTBUG-32981
[URL="https://bugreports.qt-project.org/browse/QTBUG-32981"]
I tried to work around that by using a QIdentityProxyModel instead, so I have only QIdentityProxyModels in my proxy model chain.

anda_skoa
22nd March 2014, 16:57
Well, my guess would be that time would best be spent on fixing the bug, but if you really need your own filter model it is probably better to base it off QAbstractProxyModel instead of making QIdentityProxyModel do the opposite of that it is intended for.

Cheers,
_