×
Namespaces

Variants
Actions
Revision as of 04:16, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Filtering lists in Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleCompatibility
Platform(s):
Symbian
Article
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;
// ...
proxy->setSourceModel(model);
lsta->setModel(model);
lstb->setModel(proxy);

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 04:16.
67 page views in the last 30 days.