PDA

View Full Version : Proxy for TreeView



patrikd
7th February 2008, 10:19
Hello,

I'm trying to create a proxy for the simpletreemodel from the examples. But I have a problem translating the parent relation. The first level beneath the root works fine but my proxy doesn't get the right parents for the subitems.


QModelIndex CUserProxyNoMeta::index(int r, int c, const QModelIndex& parent) const
{
if (!hasIndex(r, c))
return QModelIndex();
return createIndex(r, c);
}

QModelIndex CUserProxyNoMeta::mapFromSource(const QModelIndex& sourceIndex) const
{
if (!sourceIndex.isValid()){
return QModelIndex();
}

return index(sourceIndex.row(), sourceIndex.column());
}

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

QVariant CUserProxyNoMeta::data(const QModelIndex& ind, int role) const
{
return sourceModel()->data(mapToSource(ind), role);
}

QModelIndex CUserProxyNoMeta::parent(const QModelIndex& index) const
{
if (!index.isValid()){
return QModelIndex();
}
return mapFromSource(sourceModel()->parent(mapToSource(index)));
}

int CUserProxyNoMeta::rowCount(const QModelIndex& ind) const{
return sourceModel()->rowCount(mapToSource(ind));
}

int CUserProxyNoMeta::columnCount(const QModelIndex& ind) const
{
return sourceModel()->columnCount(mapToSource(ind));
}



How can I get the right parent relations for the proxy?

Thanks,
Patrik

wysota
7th February 2008, 10:44
You are not taking the "parent" index into consideration when mapping indexes. I don't know what your proxy is supposed to do, so I can't suggest a solution.

patrikd
7th February 2008, 13:50
Hi wysota,

in the first step I just wanna get my model into the proxy. Later I want to use the proxy to adapt the model. I tried to get the parent in the transformation but didn't succeeded. In the model the Item is used to get the parent, but as far as I understand the proxy concept it is not the right way to work over the items and I don't know how to get the parent over the index in this approach.

thanks,
Patrik

wysota
7th February 2008, 17:43
Maybe you should start with QSortFilterProxyModel instead of the abstract one?

patrikd
8th February 2008, 15:48
Hi wysota,

I made some tests with the QSortFilterProxy but that doesn't fit for the things I want to make :(

Cheers

wysota
8th February 2008, 20:12
And what do you want to make? Looking at your code I see you are trying to do 1-1 mapping and that's exactly what the sort filter proxy does out of the box.