preferred usage of QSortFilterProxyModel
-
Hi all -
I'm relatively new to using QSortFilterProxyModels. I've implemented one that sorts based on whether the item in the model exists in another model's list; works fine. Now I need to perform a different filtering operation, based on a property of the objects in the model.
The question is, is it considered preferable to create a separate QSortFilterProxyModel for each use case, or to only have one, and apply some processing intelligence to the filterAcceptsRow() function? The latter would be something like:
if (use case A) { perform filtering for A } else if (use case B) { perform filtering for B ...
and I'd have to set a member variable to represent the use case before using the proxy model.
Is one of these methods recommended over the other?
Thanks...
-
@mzimmers From what I have seen so far I would say it depends on your model size and the resulting performance. I have seen use cases where 5 or 6 proxies were used in a row with small data sizes while on larger model sizes only one was used which handled all possible filter actions. I would say use what suits you best. Also keep in mind that the more proxies you use the more function calls from Qt need to be processed so the less models should be the better.
-
@DerReisende thanks for the reply. In my application, the model is going to be relatively small (at most 100 elements in its list), and I don't anticipate the need for "chaining" the proxy models; I'll just use one at a time. So, based on what you've said, it sounds like creating multiple proxy models is the way to go.
It strikes me as a cleaner implementation anyway, as I won't have to concern myself with maintaining the variable that controls which route the filterAcceptsRow() function takes. My only concern was whether creating multiple proxy models for a given model was considered "wasteful," but from what you've said, I probably don't need to worry about that.
Thanks again...
-
M mzimmers has marked this topic as solved on
-
M mzimmers has marked this topic as unsolved on
-
I guess there's more to this than I realized. I've implemented 2 proxy models for my EquipmentModel. Each has its own filterAcceptsRow() function. In main.cpp:
EquipmentModel *equipmentModel = new EquipmentModel(&app, messageManager, systemModel); EquipmentProxyModel equipmentProxyModel(equipmentModel, spaceModel); EquipmentValveFilter equipmentValveFilter; // set the main models on the proxy models. equipmentProxyModel.setSourceModel(equipmentModel); equipmentValveFilter.setSourceModel(equipmentModel);
I had assumed the proxies would take effect only when used directly (as from QML), but it appears that both are in effect all the time. Do I need to selectively apply the proxies to the model as appropriate in my code? So, for one QML page, I'd activate one proxy, and for another, I'd activate the other?
EDIT:
From the docs:
Subclasses should call beginResetModel() at the beginning of the method, disconnect from the old model, call this method, connect to the new model, and call endResetModel().How do I disconnect?
Thanks...
-
After doing a little more reading (and experimentation), I realize that the behavior I described above is actually intended. I suppose if my model was large, this could present a concern, and I'd have to dynamically enable/disable the proxies depending on where I am in my application, but for this application, it seems to be OK.
Thanks to all who helped.
-
M mzimmers has marked this topic as solved on