Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Filtering lists in Qt

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleCompatibility
Created: marcelobarrosalmeida (31 Jan 2010)
Last edited: hamishwillee (11 Oct 2012)

This article shows how to filter a list using QSortFilterProxyModel, part of the Qt model/view architecture.

Qt model/view architecture provides separated objects for presenting and handling data sources, offering great flexibility for programmers when dealing with data. In particular, the class QSortFilterProxyModel is useful for filtering/sorting data sources, mapping the source model indexes into new indexes in a transparent way.

Qt model/view [1]

In the next example is demonstrated how to use QSortFilterProxyModel. Two models are created. The first model, called model, is an instance of QStandardItemModel} and it is used as the primary data source. The second model, called proxy}, is an instance of QSortFilterProxyModel and it uses model as a data source. The list lsta will use model as model and the list lstb will use proxy as model.

model = new QStandardItemModel(0,3,this);
proxy = new QSortFilterProxyModel;
// ...

The user can filter lstb contents using regular expressions, wildcard or fixed string.

PC screeenshot Maemo screeenshot

You can download the entire source code from It was tested in GNU/Linux and Maemo 5 running Qt 4.6.1 but it can be compiled for other platforms as well.

This page was last modified on 11 October 2012, at 01:16.
65 page views in the last 30 days.