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:
{
// If the proxy is not activated, use the base mapFromSource function.
if (m_ActivatedProxy == false)
return IdentityFileSystemProxyModel::mapFromSource(sourceIdx);
// 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)
else
return createIndex(0,
sourceIdx.column(),
sourceModel()->sibling(m_rootSrcIdx.row(),sourceIdx.column(),m_rootSrcIdx).internalPointer());
}
// In any other cases, use the base mapFromSource function.
return IdentityFileSystemProxyModel::mapFromSource(sourceIdx);
}
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(sourceIdx);
// 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_rootSrcIdx).internalPointer());
}
// In any other cases, use the base mapFromSource function.
return IdentityFileSystemProxyModel::mapFromSource(sourceIdx);
}
To copy to clipboard, switch view to plain text mode
{
// If the proxy is not activated, use the base mapToSource function.
if (m_ActivatedProxy == false)
return IdentityFileSystemProxyModel::mapToSource(proxyIndex);
// Alter the mapping so the proxy model sees only m_rootSrcIdx's row as m_rootSrcParentIdx child.
QModelIndex sourceIdx
= IdentityFileSystemProxyModel
::mapToSource(proxyIndex
);
if (sourceIdx.parent().internalPointer() == m_rootSrcParentIdx.internalPointer())
{
if (proxyIndex.row() != 0)
else
return sourceModel()->sibling(m_rootSrcIdx.row(),
proxyIndex.column(),
m_rootSrcIdx);
}
// In any other cases, use the base mapToSource function.
return sourceIdx;
}
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(proxyIndex);
// Alter the mapping so the proxy model sees only m_rootSrcIdx's row as m_rootSrcParentIdx child.
QModelIndex sourceIdx = IdentityFileSystemProxyModel::mapToSource(proxyIndex);
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;
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks