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

Jump to: navigation, search
Article Metadata

Code Example
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.

121 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved