Guett_31
22nd March 2014, 07: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.
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.