Qt 4: The book

Page 234

8 Displaying Data Using “Interview”

During filtering, the model returns the model indices for those rows in which the text in a column matches the search filter. During sorting, the row order is arranged according to the values in a specified column, whereby you can sort in ascending or descending order. We will demonstrate both capabilities of QSortFilterProxyModel with a small example, the FilteringView. This consists of a tree view, above which is a line edit that accepts a filter term. Next to this we place a combo box containing all the column names. Figure 8.12 shows how you can use this to select the column that is to act as the search column. Figure 8.12: QSortFilterProxyModel helps in sorting and filtering models.

8.5.1 Adjustments to the User Interface Simplifying Sorting Since our view will use a QSortFilterProxyModel instance, which can already sort, we need only to adjust the viewer accordingly. The work necessary for this is done by the constructor of the class: // addressbook/filteringview.cpp #include <QtGui> #include "filteringview.h" FilteringView::FilteringView(QAbstractItemModel *model, QWidget *parent) : QWidget(parent) { setWindowTitle(tr("Filter View")); proxyModel = new QSortFilterProxyModel(this); proxyModel->setSourceModel(model); QVBoxLayout *lay = new QVBoxLayout(this); QHBoxLayout *hlay = new QHBoxLayout; QLineEdit *edit = new QLineEdit; QComboBox *comboBox = new QComboBox; int modelIndex = model->columnCount(QModelIndex()); for(int i=0; i < modelIndex; i++)

232


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.