PDA

View Full Version : QAbstractProxyModel MapToSource Problem



tuedl
10th February 2010, 10:19
Hi,

I'm trying to develope a complex Model with different Proxymodels. The Main-Model is a Tree with different Items based on a ItemBaseClass.
The first Proxymodel is a QSortFilterProxyModel for the tree-Navigation-Widget. This works.



Tree:
-Item1
---sub1
---subsub1
---subsub2
---sub2
-Item2
---sub3

Table of sub1:
subsub1-name | subsub1-property
subsub2-name | subsub2-property



The second Proxymodel is to show and edit a TreeItem with subElements in a table.
This works a bit. The Elements are shown in the table, but I cannot insert or delete rows.
My problem is that I do not know how to implement the MapToSource/MapFromSource methods..
So here are my simple implementations:


QModelIndex TFSEditorItemModel::mapFromSource(const QModelIndex &source) const{
return index(source.row(), source.column(), source.parent());
}

QModelIndex TFSEditorItemModel::mapToSource(const QModelIndex &proxy) const {
if(!proxy.isValid())
{
return QModelIndex();
}
return sourceModel()->index(proxy.row(), proxy.column(), proxy.parent() ) ;
}


If I try to use mapToSource e.g. to insert a row through the sourceModel, I never get a correct sourceIndex. Especally the proxy.parent-Index is not valid, so the sourceModel cannot create a valid Index in the sourcetree.
How can I get an index of the table-root-item from the sourceModel? I have a Pointer to the Item (the tables root-item) but no Index. I think I cannot remember this Index because it can change..?

I hiope you can help me or give a reference to a good example. I tried to find something, but the results did not help me.

greets
david

mont_blanc
9th January 2012, 15:34
return sourceModel()->index(proxy.row(), proxy.column(), proxy.parent() ) ;

proxy.parent is an index of proxy model, you must pass an index of source model in this case