PDA

View Full Version : QSortFilterProxyModel::filterAcceptsRow not being called



nama1510
12th April 2021, 10:19
Hello all!

I basically have a source model that organizes the entries in a tree fashion in order to display it in a TreeView. Now I want to display the leaves of that tree source model in a TableView so I made a custom QSortFilterProxyModel that also works with filtering in order to display these entries properly. As far as I understand when I call the InvalidateFilter method it should invoke filterAcceptsRow but in this case it never gets called. Looking for a solution in the internet I found that apparently Qt won't invoke filterAcceptsRow if the parent of the index is not in the model (which I don't want it to be).

Have you guys had a similar issue to this one and have a workaround that might work?

Thanks a lot!

ChristianEhrlicher
12th April 2021, 17:56
What does 'is not in the model' mean? I don't see why a model should do something when an index for anything which is not in it's source model changes...

nama1510
13th April 2021, 09:36
What does 'is not in the model' mean? I don't see why a model should do something when an index for anything which is not in it's source model changes...

Source Model
Folder_1
|->Item_1
|->Item_2

Folder_2
|->Item_3
|->Item_4


ProxyModel
Item_1
Item_2
Item_3
Item_4

So basically the parent indexes from the source model (Folder_1 and Folder_2) are not in the Proxy Model.
I want to apply a filter to the proxy model, in the filterAcceptsRow method I check if the entry should be accepted in the model by checking if the entry matches the filter. Every time the filter changes I emit a filterChanged signal that will trigger a InvalidateFilter call that (in theory) will invoke that filterAcceptsRow method. The problem I have is that in this case filterAcceptsRow will never be called for some reason.
Looking into other forums for people that had similar issues I came across this answer:
I just understood that filterAcceptsRow get invoked only if parent of index is displayed in proxy model. Oherwise proxy model thinks that it is not worth to insert row in proxy model.
So it is just how Qt handles that kind of situations.

Hope this clarifies the issue.

thanks!

ChristianEhrlicher
13th April 2021, 10:14
So the index is in the model and in the proxy model, it's just filtered out in the view.
If you call invalidateFilter() then a new filtering will be started. Also you don't pass any model index to filterChanged() at all so I don't understand what you're trying to say.
Please provide a *minimal* compilable example.