How to filter QListView

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData Steps:

1. Create a model that will hold all data and fill it

   QStandardItemModel model;
   for (char i = 'A'; i <= 'Z'; ++i) {
       model.appendRow(new QStandardItem(QString("Item %1").arg(i)));
   }

2. Create a proxy model which will be used to filter list.

   QSortFilterProxyModel proxyModel;

3. Link model and proxy model

   proxyModel.setSourceModel(&model);

4. Create a QListView

   QListView list;

5. Link proxy model and list view and show UI

   list.setModel(&proxyModel);
   list.show();

6. Apply filter

   QRegExp regExp("*R*", Qt::CaseInsensitive, QRegExp::Wildcard);
   proxyModel.setFilterRegExp(regExp);

This will automatically update QListView. See the example for more details and SIGNAL handling. File:QListViewExample.zip

Special thanks to divanov and mkfnx for their contribution in thread http://www.developer.nokia.com/Community/Discussion/showthread.php?210848-How-to-fiter-a-list