Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSortFilterProxyModel data() method and setFilterFixedString
I am using a QAbstractListModel as my base model and want to connect several QSortFilterProxyModel. Since all views need to display the same data differently, I have reimplemented the data method in the corresponding QSortFilterProxyModel to return the right value for the DisplayRole. But actually it is not only about the DisplayRole, but also about filtering. The problem, as soon as I set the setFilterFixedString (or setFilterRegExp ...) to any value, the data method of the proxy model is no longer used. Instead it always calls the data method of the source model.
I am using Qt for Python, but I thought I post this to the general thread, since I guess that this behavior is not python specific.
In case this behavior is intended. How can I use the data method of the proxy model and use the filtering without setting the setFilterFixedString. Or asked differently, what do I need to return for the filterRole. As I understood, the return value from the filterRole must match the setFilterFixedString (or setFilterRegExp) to be not filtered. How can I use the filterRole without using setFilterFixedString?
Thanks for your help,
If it is just a matter of displaying, it would be better to implement a custom QStyledItemDelegate. So you keep your data and its representation cleanly separated.
Thanks for the fast reply. I am using many different roles, like fontRole, decoratorRole etc. Up too now it worked fine without QStyledItemDelegate, but we always used new models, even when using the same data. Now we changed the general design and we run into this problems.
Could you maybe explain a bit more about the QStyledItemDelegate? Where do I have to implement it?
Could you maybe also answer my question before? Is the behavior normal, that the data method of the SortFilterProxy is ignored as soon as I use setFilterFixedString?
For your strange behavior you should add which version of Qt you are using. There's nothing in the code of data that shows any such alteration, it just returns the index matching within the source model.
As for QStyledItemDelegate, what exact question do you have ?
I am using PySide2 5.15.2, but I also saw this behavior on other versions, at least 5.15.1. An I think I had this problem already about 3 years ago, using PyQt 5.x.
I solved the problem now by putting a QIdentityProxyModel in between. That means, I have for one data set on QAbstractListModel and for each view connecting to this base model a QIdentityProxyModel (with the data method implemented) and a QSortAndFilterModel (for filtering and sorting, but the data for sort and filter role come from the data method of the identiyProxyModel).
Is this good practise or bullshit?
Nothing wrong with having several proxies.
And it's not a bad idea to split filtering and data transformation.