CopperSpice API  1.9.1
Proxy Model

In the model/view architecture data from a single model can be shared by any number of views and each view can represent the same information in a completely different way. It may seem reasonable for the view to be responsible for sorting and filtering operations, however this approach is not efficient. One alternative would be for the model to be responsible for things like sorting. This leads to similar problems where each view has to display items of data that are organized according to the most recent processing operation.

To solve this issue the model/view approach uses proxy models to organize the information supplied by the model and displayed using a view. Proxy models are classes which appears to be a model from the perspective of a view and look like a view from the perspective of a model.

Signals and slots are used to ensure each view is updated appropriately no matter how many proxy models are placed between the view and the source model.

Using Proxy Models

Proxy models can be inserted between an existing model and any number of views. CopperSpice has a standard proxy model called, QSortFilterProxyModel. The class is usually instantiated and used directly, however you can inherit from QSortFilterProxyModel to add custom sorting and filtering behavior.

QSortFilterProxyModel *filterModel = new QSortFilterProxyModel(parent);
filterModel->setSourceModel(stringListModel);
QListView *filteredView = new QListView;
filteredView->setModel(filterModel);

Since proxy models inherit from QAbstractItemModel they can be connected to any kind of view and can be shared between multiple views. They can also be used to process the information obtained from other proxy models in a pipeline arrangement.

Customizing Proxy Models

The proxy model maps each item of data from its original location in the source model, to either a different location in the proxy model or it hides the item. When an item is hidden the proxy model is called a filter.