When this feature is on, clicking on a header section sorts the items according to that column. By clicking repeatedly, the user can alternate between ascending and descending order.
Custom sorting behavior is achieved by subclassing QSortFilterProxyModel and reimplementing lessThan(), which is used to compare items. For example:
An alternative approach to sorting is to disable sorting on the view and to impose a certain order to the user. This is done by explicitly calling sort() with the desired column and order as arguments on the QSortFilterProxyModel (or on the original model if it implements sort()). For example:
Filtering
In addition to sorting, QSortFilterProxyModel can be used to hide items that don't match a certain filter. The filter is specified using a QRegExp object and is applied to the Qt::DisplayRole of each item, for a given column. The QRegExp object can be used to match a regular expression, a wildcard pattern, or a fixed string. For example:
proxyModel->setFilterRegExp(QRegExp(".png", Qt::CaseInsensitive,
QRegExp::FixedString));
proxyModel->setFilterKeyColumn(1);
For hierarchical models, the filter is applied recursively to all children. If a parent item doesn't match the filter, none of its children will be shown.
Custom filtering behavior can be achieved by reimplementing the filterAcceptsRow() and filterAcceptsColumn() functions. For example, the following implementation ignores the filterKeyColumn property and performs filtering on columns 0, 1, and 2:
bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);
return (sourceModel()->data(index0).toString().contains(filterRegExp())
|| sourceModel()->data(index1).toString().contains(filterRegExp()))
&& dateInRange(sourceModel()->data(index2).toDate());
}
See also QAbstractProxyModel, QAbstractItemModel, and Model/View Programming.
Property Documentation
This property holds the case sensitivity of the QRegExp pattern used to filter the contents of the source model.
By default, the filter is case sensistive.
Access functions:
- Qt::CaseSensitivity filterCaseSensitivity () const
- void setFilterCaseSensitivity ( Qt::CaseSensitivity cs )
See also setFilterRegExp(), setFilterWildcard(), and setFilterFixedString().
filterKeyColumn : int
This property holds the column where the key used to filter the contents of the source model is read from.
The default value is 0.
Access functions:
- int filterKeyColumn () const
- void setFilterKeyColumn ( int column )
filterRegExp : QRegExp
This property holds the QRegExp used to filter the contents of the source model.
Setting this property overwrites the current filterCaseSensitivity.
Access functions:
- QRegExp filterRegExp () const
- void setFilterRegExp ( const QRegExp & regExp )
- void setFilterRegExp ( const QString & pattern )
See also setCaseSensitivity(), setFilterWildcard(), and setFilterFixedString().
Member Function Documentation
QSortFilterProxyModel::QSortFilterProxyModel ( QObject * parent = 0 )
Constructs a sorting filter model with the given parent.
QSortFilterProxyModel::~QSortFilterProxyModel ()
Destroys this sorting filter model.
void QSortFilterProxyModel::clear () [slot]
Clears this sorting filter model, removing all mapping.
bool QSortFilterProxyModel::filterAcceptsColumn ( int source_column, const QModelIndex & source_parent ) const [virtual protected]
Returns true if the value in the item in the column indicated by the given source_column and source_parent should be included in the model.
The default implementation returns true.
See also filterAcceptsRow().
bool QSortFilterProxyModel::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const [virtual protected]
Returns true if the value in the item in the row indicated by the given source_row and source_parent should be included in the model.
The default implementation uses filterRegExp with the data returned for the Qt::DisplayRole to determine if the row should be accepted or not.
See also filterAcceptsColumn().
bool QSortFilterProxyModel::lessThan ( const QModelIndex & left, const QModelIndex & right ) const [virtual protected]
Returns true if the value of the item referred to by the given index left is less than the value of the item referred to by the given index right, otherwise returns false. This function is used as the < operator when sorting, and handles several QVariant types:
See also sort().
QModelIndex QSortFilterProxyModel::mapFromSource ( const QModelIndex & sourceIndex ) const [virtual]
Returns the model index in the QSortFilterProxyModel given the sourceIndex from the source model.
Reimplemented from QAbstractProxyModel.
See also mapToSource().
QModelIndex QSortFilterProxyModel::mapToSource ( const QModelIndex & proxyIndex ) const [virtual]
Returns the source model index corresponding to the given proxyIndex from the sorting filter model.
Reimplemented from QAbstractProxyModel.
See also mapFromSource().
void QSortFilterProxyModel::setFilterFixedString ( const QString & pattern ) [slot]
Sets the fixed string used to filter the contents of the source model to the given pattern.
See also setFilterCaseSensitivity(), setFilterRegExp(), and setFilterWildcard().
void QSortFilterProxyModel::setFilterWildcard ( const QString & pattern ) [slot]
Sets the wildcard expression used to filter the contents of the source model to the given pattern.
See also setFilterCaseSensitivity(), setFilterRegExp(), and setFilterFixedString().