tags:

views:

720

answers:

3

Hello

I am trying to create a QTreeView which displays some sorted information. To do this I use a QSortFilterProxyModel between the view and my model.

The problem is that I want to limit the number of rows to the first n rows (after sorting). The filter function from the model receives the original sourceRow so I cannot use it.

I've tried chaining two QSortFilterProxyModel: the first for the sorting and the second for the filtering. But it seems that the second proxymodel(filtering) doesn't receive the rows sorted....

Is there another way to do it? Has anyone use this technique(chaining of 2 proxy models) and it works?

thank you

EDIT: I've tried with the rowCount and it doesn't work. I've also tried to chain 2 proxy models but the problem is that the view calls the sort function for the model it receives. So if the first proxy sorts and the second filters the sort will be called on the filter model and the data won't be sorted.

EDIT2: I've looked into the qt source code and the filtering is done before sorting, so in the filterAcceptsRow() I don't know any sorting order.

A: 

Just out of curiousity, have you tried overriding the rowCount method and just return 25 (or whatever n is in your case)? It might be as simple as that... well, if you'll always have at least n items.

Otherwise, you could try chaining the models. I don't know why it wouldn't work, but I've never tried something like it myself.

Caleb Huitt - cjhuitt
A: 

That's an interesting question, I've constructed a QTreeModel recently, and still haven't sorted it. Can you post some code to try it your way?

MadH
You can look at http://doc.trolltech.com/4.5/qsortfilterproxymodel.html and http://doc.trolltech.com/4.5/itemviews-basicsortfiltermodel.html to see some examples of using a proxy model.
A: 

After trying a number of overcomplicated ways to solve this I've done a small hack for my problem: after I insert/remove a row I call setRowHidden to hide the first n rows. This is not the most elegant solution and is particular for my needs, but I am unable to find a better alternative.

I like to mention that on gtk, because the filter and the sort proxy models are separated, this can be done fairly easy.

I'm still hoping someone can provide a better solution to this.