PDA

View Full Version : QSortFilterProxyModel problem



blukske
20th June 2006, 12:43
Hi,

I'm wondering how to use QSortFilterProxyModel in the following situation. I have a tree mode laid out like this with 4 columns:

A 1 2 3
|_B 1 2 3

where B is a child item of A with the same number of columns as A. I have two tree views on this model. In the first tree view, I only want to see

A
|_ B

and in the second one I only want to see

1 2 3
1 2 3

wysota
20th June 2006, 14:57
Subclass the model and reimplement filterAcceptsColumn to accept (1) only the first column, (2) all but the first column.

If you don't want a parent-child relation in the second case, reimplement mapFromSource to return indexes with an invalid parent.

blukske
20th June 2006, 15:29
Thanks for your answer, but that does not seem to work. My guess is that when filterAcceptsColumn() returns false for the first column in the second view, the child item B is not considered anymore, and thus its columns are not processed. I end up with:

first view:

A
|_B

second view

1 2 3

so the columns from B are lost. From the manual with regard to filterAcceptsColumn():

For hierarchical models, the filter is applied recursively to all children. If a parent item doesn't match the filter, none of its children will be shown.

Any ideas? Should I implement a complete QAbstractProxyModel for this to work? (Seems like overkill).

wysota
20th June 2006, 15:52
Reimplement mapFromSource and mapToSource.

blukske
21st June 2006, 08:16
I tried that, but I'm not sure how that would help. Where would I have to call these methods? QSortFilterProxyModel does not use these methods internally.

wysota
21st June 2006, 08:19
I tried that, but I'm not sure how that would help. Where would I have to call these methods?
You don't. The proxy model will call them to do a mapping from its indexes to source model indexes and the other way round.


QSortFilterProxyModel does not use these methods internally.
Have you checked in QAbstractProxyModel too?

blukske
21st June 2006, 08:35
You don't. The proxy model will call them to do a mapping from its indexes to source model indexes and the other way round.

I expected that as well. As it turns out, QSortFilterProxyModel maintains its own internal proxy_to_source & source_to_proxy mappings.


Have you checked in QAbstractProxyModel too?

I looked into it, but it requires a lot of additional signal/slot administration. Simply implementing the pure virtual functions is not enough, since changes to the source model are not propagated via the abstract proxy model to the view automatically. But I think I'll have to go that way now, as I'm ready to give up on QSortFilterProxyModel :)

My last try is to re-implement index() and data() using mapToSource & mapFromSource and see where that leaves me.

wysota
21st June 2006, 18:13
I looked into it, but it requires a lot of additional signal/slot administration.
I meant did you look into its source code while trying to find where mapFromSource and mapToSource are called.

Don't give up. All you need is to reimplement mapToSource and mapFromSource. And when I think of it, subclassing the abstract class directly could be the way to go, if you don't need the filtering and sorting functionality.

blukske
22nd June 2006, 08:26
I meant did you look into its source code while trying to find where mapFromSource and mapToSource are called.

Don't give up. All you need is to reimplement mapToSource and mapFromSource. And when I think of it, subclassing the abstract class directly could be the way to go, if you don't need the filtering and sorting functionality.

I gave up :p. I checked QAbstractProxyModel's source, but again, mapFromSource/mapToSource are only called by mapSelectionFromSource/ToSource. Implementing them for either QAbstractProxyModel/QSortFilterProxyModel is not enough, because they are not getting called at all. I've got it working now by simply hiding/unhiding the columns in the tree view. Not the solution I wanted, but it works for now.

I'll try to implement a proper QAbstractProxyModel later on.