Filtering lists in Qt

From Nokia Developer 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 File:MBA-demolist.zip. 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.
81 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.