PDA

View Full Version : FilterProxy model



gyre
3rd December 2007, 21:22
Hi guys,
I have a question about filtering the TableView.
I need to make something like this:
I have a TableView widget that has a model set. model has 4 columns and views selection behaviour is set to rows...
I need to implement a filtering of a view according to some value specified in lets say line edits that will be placed at the top(or better above) of that view...And filter should be applied when a checkbox( that is near those line edits) is checked...When it is unchecked it should not be applied and view should return to its previous state -> to see all of model's items.
Ive read about QSortFilterProxyModel...but found a sentence in doc saying:
"The QSortFilterProxyModel class provides support for sorting and filtering data passed between another model and a view"
Do I understand it correctly that there Is no way of filtering the view of ONE model using this FilterProxyModel class ?
How would you advise me to implement my widget ?
Thanks a Lot!

wysota
3rd December 2007, 21:32
Of course there is. You pass the original model to the proxy and then pass the proxy model instead of the original model to the view. If you want complex filtering you'll have to reimplement filterAcceptRow().

gyre
4th December 2007, 10:00
another question...
is it possible to filter to filter the view (using the SortFilterProxyModel) using 2 line edits ?...custom filter proxy example shows only using one line edit for sorting so Im just asking if it is possible to filter view using two ciretrias given by two line edits..

Khal Drogo
4th December 2007, 10:20
Yes, its possible.
In fact, when you look at the code of the Custom Sort/Filter Model example, you see that filterAcceptsRow actually implements 3 different criterias.

So yes, you can do so, that the row is shown only if the data in column1 fits the criterion from lineEdit1 and/or the data in column2 fits the criterion in lineEdit2.

wysota
4th December 2007, 10:33
If you reimplement filterAcceptRows you can do any filtering you want. You get an index of the model, you do whatever you want with it and then return true or false to make the index visible or not. For example once I made a filter that filtered rows based on results of some heavy calculations performed on the whole model. The threshold value was set by a slider that was connected to the proxy model so whenever the slider value changed, the filter was recalculated.

gyre
4th December 2007, 12:37
thanks guys...Ill try to implement it as you advised me...
just one last question...
Is there such thing(method) that would allow me to disable/enable filtering upon some event ? like when the state of checkbox changes ...

I thought I understood invalidateFilter() method but it seems I dont...what does it do ?
Thanks

Khal Drogo
4th December 2007, 12:56
Since you are using a custom proxy model, you probably dont use the setFilterRegExp, setFilterFixedString, etc. methods, to pass a single string as filtering criterion.
The above functions (if i understood correctly) do call invalidateFilter themselves, when they are called.
So you have to use invalidateFilter yourself after setting the new filtering criterias; you are basically forcing the model to consider your updated criterias instead the old ones.

You can enable/disable using the filter, if thats what you meant by that, by for instance setting a flag in your model, when the checkbox is checked, then in filterAcceptsRow if this flag is checked, you use the filter, if not, you just return true, thus every row is accepted.
Don't forget calling invalidateFilter after you set the flag.

Hope it helps.

gyre
4th December 2007, 14:00
I dont know...I tried to implement the model as show in example but it somehow doesnt work...Can anyone tell me why not ? :)...
I attached the header and source files Im using...
Thanks

gyre
4th December 2007, 15:40
I changed the files to ones that are in attachements...
Filtering now works OK...but when I remove all characters from lineedit from which I control filtering the model does not get to the original state and ALL items are filtered...dont know why :(

gyre
4th December 2007, 15:46
I have found a problem...it has to be checked whether the regexps are empty...
see the attached code if youre interested...