Sunday, 11 October 2009

Power of proxy models

Our Usability Team created mockup of new "Add Buddy" window. It contains combobox for selecting group new contact will belong to. It looks like that:

  • - Select group -
  • Group 1
  • Group 2
  • Group 3
  • Create new group...
We have model for groups, but using it for that combobox would only show 3 groups, without additional items at top and at bottom. So I had to use proxy model.

Proxy models are models that you can stack (many of them) on another model (like our group model) to filter them, sort them or process data in any way. For "Add buddy" window there are 2 proxy models on top on group model:
  • QSortFilterProxyModel
  • ActionsProxyModel
First proxy model sorts groups by name, the second one adds items at top and at bottom of the list (it even make it italics). Code for stacking proxy models looks like this:

GroupsModel *groupComboModel =
new GroupsModel(GroupCombo);
  QSortFilterProxyModel *sortModel =
new QSortFilterProxyModel(GroupCombo);
ActionsProxyModel *groupsProxyModel =
new ActionsProxyModel(groupsModelBeforeActions,
  groupsModelAfterActions, GroupCombo);

The most important part is that you can reuse these proxy models everywhere you want and you don't have to manually update this combobox when new groups are created ;) Models do everything for you.

No comments:

Post a Comment