QSortFilterProxyModel Class
The QSortFilterProxyModel class provides support for sorting and filtering data passed between another model and a view. More…
| Header | #include <QSortFilterProxyModel> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QAbstractProxyModel |
List of all members, including inherited members
Public Functions
| QSortFilterProxyModel(QObject *parent = nullptr) | |
| ~QSortFilterProxyModel() | |
| QRegularExpression | filterRegularExpression() const |
| int | filterKeyColumn() const |
| void | setFilterKeyColumn(int column) |
| Qt::CaseSensitivity | filterCaseSensitivity() const |
| void | setFilterCaseSensitivity(Qt::CaseSensitivity cs) |
| Qt::CaseSensitivity | sortCaseSensitivity() const |
| void | setSortCaseSensitivity(Qt::CaseSensitivity cs) |
| bool | isSortLocaleAware() const |
| void | setSortLocaleAware(bool on) |
| int | sortColumn() const |
| Qt::SortOrder | sortOrder() const |
| bool | dynamicSortFilter() const |
| void | setDynamicSortFilter(bool enable) |
| int | sortRole() const |
| void | setSortRole(int role) |
| int | filterRole() const |
| void | setFilterRole(int role) |
| bool | isRecursiveFilteringEnabled() const |
| void | setRecursiveFilteringEnabled(bool recursive) |
| bool | autoAcceptChildRows() const |
| void | setAutoAcceptChildRows(bool accept) |
Reimplemented Public Functions
| void | setSourceModel(QAbstractItemModel *sourceModel) |
| QModelIndex | mapToSource(const QModelIndex &proxyIndex) const |
| QModelIndex | mapFromSource(const QModelIndex &sourceIndex) const |
| QModelIndex | index(int row, int column, const QModelIndex &parent = QModelIndex()) const |
| QModelIndex | parent(const QModelIndex &child) const |
| QModelIndex | sibling(int row, int column, const QModelIndex &idx) const |
| int | rowCount(const QModelIndex &parent = QModelIndex()) const |
| int | columnCount(const QModelIndex &parent = QModelIndex()) const |
| bool | hasChildren(const QModelIndex &parent = QModelIndex()) const |
| bool | insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) |
| bool | insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) |
| bool | removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) |
| bool | removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) |
| void | sort(int column, Qt::SortOrder order = Qt::AscendingOrder) |
Public Slots
| void | setFilterRegularExpression(const QString &pattern) |
| void | setFilterRegularExpression(const QRegularExpression ®ularExpression) |
| void | setFilterWildcard(const QString &pattern) |
| void | setFilterFixedString(const QString &pattern) |
| void | invalidate() |
Signals
| void | dynamicSortFilterChanged(bool dynamicSortFilter) |
| void | filterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity) |
| void | sortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity) |
| void | sortLocaleAwareChanged(bool sortLocaleAware) |
| void | sortRoleChanged(int sortRole) |
| void | filterRoleChanged(int filterRole) |
| void | recursiveFilteringEnabledChanged(bool recursiveFilteringEnabled) |
| void | autoAcceptChildRowsChanged(bool autoAcceptChildRows) |
Protected Functions
| virtual bool | filterAcceptsRow(int source_row, const QModelIndex &source_parent) const |
| virtual bool | filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const |
| virtual bool | lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const |
| void | invalidateFilter() |
| void | invalidateRowsFilter() |
| void | invalidateColumnsFilter() |
Detailed Description
A QSortFilterProxyModel presents the rows of its source model sorted and filtered, without changing the source. Rows are filtered with a regular expression on the filterKeyColumn() and sorted by a column with sort(); subclasses customize both by reimplementing filterAcceptsRow() and lessThan().
QSortFilterProxyModel proxy;
proxy.setSourceModel(&model);
proxy.setFilterFixedString(u"an"_s);
proxy.setFilterCaseSensitivity(Qt::CaseInsensitive);
proxy.sort(0);
With dynamicSortFilter() (the default), the proxy sorts and filters again when the source changes.
See also QAbstractProxyModel and QIdentityProxyModel.
Differences from Qt
Changes to the source and to the filter are announced as layout changes (layoutAboutToBeChanged() and layoutChanged()), with persistent indexes updated, instead of the finer row insertion and removal signals of Qt. The beginFilterChange() and endFilterChange() functions and the bindable properties are not provided.
Member Function Documentation
QSortFilterProxyModel::QSortFilterProxyModel(QObject *parent = nullptr)
Constructs a sorting and filtering proxy with the given parent. It does not filter, and sorts only after sort() is called.
QSortFilterProxyModel::~QSortFilterProxyModel()
Destroys the proxy.
bool QSortFilterProxyModel::autoAcceptChildRows() const
Returns true if the children of an accepted row are accepted too; otherwise returns false.
This function was introduced in 6.0.
void QSortFilterProxyModel::autoAcceptChildRowsChanged(bool autoAcceptChildRows)
This signal is emitted when the automatic acceptance of child rows is switched to autoAcceptChildRows.
This function was introduced in 6.0.
int QSortFilterProxyModel::columnCount(const QModelIndex &parent = QModelIndex()) const
Reimplements QAbstractProxyModel::columnCount().
Returns the number of accepted columns under parent.
bool QSortFilterProxyModel::dynamicSortFilter() const
Returns true if the proxy sorts and filters again when the source data changes; otherwise returns false. The default is true.
void QSortFilterProxyModel::dynamicSortFilterChanged(bool dynamicSortFilter)
This signal is emitted when dynamicSortFilter() changes to dynamicSortFilter.
This function was introduced in 6.0.
bool QSortFilterProxyModel::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
Returns true if the source column source_column under source_parent is shown. The default accepts all columns.
bool QSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
Returns true if the source row source_row under source_parent is shown. The default accepts the row if the filter is empty or matches the data of filterKeyColumn() (any column if it is -1) for filterRole().
Qt::CaseSensitivity QSortFilterProxyModel::filterCaseSensitivity() const
Returns the case sensitivity of the filter.
void QSortFilterProxyModel::filterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity)
This signal is emitted when the filter case sensitivity changes to filterCaseSensitivity.
This function was introduced in 6.0.
int QSortFilterProxyModel::filterKeyColumn() const
Returns the source column the filter tests, or -1 for all columns. The default is 0.
See also setFilterKeyColumn().
QRegularExpression QSortFilterProxyModel::filterRegularExpression() const
Returns the regular expression used to filter rows.
See also setFilterRegularExpression().
int QSortFilterProxyModel::filterRole() const
Returns the role whose data the filter tests; the default is Qt::DisplayRole.
void QSortFilterProxyModel::filterRoleChanged(int filterRole)
This signal is emitted when the filter role changes to filterRole.
This function was introduced in 6.0.
bool QSortFilterProxyModel::hasChildren(const QModelIndex &parent = QModelIndex()) const
Reimplements QAbstractProxyModel::hasChildren().
Returns true if parent has accepted child rows and columns.
QModelIndex QSortFilterProxyModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const
Reimplements QAbstractProxyModel::index().
Returns the proxy index for row and column under parent.
bool QSortFilterProxyModel::insertColumns(int column, int count, const QModelIndex &parent = QModelIndex())
Reimplements QAbstractProxyModel::insertColumns().
Inserts count columns into the source before the source column shown at column under parent.
bool QSortFilterProxyModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex())
Reimplements QAbstractProxyModel::insertRows().
Inserts count rows into the source before the source row shown at row under parent (at the end of the source when row is rowCount()).
void QSortFilterProxyModel::invalidate()
Sorts and filters the rows again.
See also invalidateFilter().
void QSortFilterProxyModel::invalidateColumnsFilter()
Filters the columns again.
This function was introduced in 6.0.
void QSortFilterProxyModel::invalidateFilter()
Filters the rows and columns again, for subclasses whose filter changed.
void QSortFilterProxyModel::invalidateRowsFilter()
Filters the rows again.
This function was introduced in 6.0.
bool QSortFilterProxyModel::isRecursiveFilteringEnabled() const
Returns true if a row is also accepted when one of its descendants is; otherwise returns false.
This function was introduced in 5.10.
bool QSortFilterProxyModel::isSortLocaleAware() const
Returns true if strings are sorted with QCollator; otherwise returns false.
bool QSortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
Returns true if the source item source_left sorts before source_right. The default compares the data of sortRole(): numbers by value, dates and times chronologically, and everything else as strings, with sortCaseSensitivity() or, if isSortLocaleAware(), with QCollator.
QModelIndex QSortFilterProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
Reimplements QAbstractProxyModel::mapFromSource().
Returns the proxy index of sourceIndex, or an invalid index if the item is filtered out.
QModelIndex QSortFilterProxyModel::mapToSource(const QModelIndex &proxyIndex) const
Reimplements QAbstractProxyModel::mapToSource().
Returns the source index of proxyIndex.
QModelIndex QSortFilterProxyModel::parent(const QModelIndex &child) const
Reimplements QAbstractProxyModel::parent().
Returns the parent of child.
void QSortFilterProxyModel::recursiveFilteringEnabledChanged(bool recursiveFilteringEnabled)
This signal is emitted when recursive filtering is switched to recursiveFilteringEnabled.
This function was introduced in 6.0.
bool QSortFilterProxyModel::removeColumns(int column, int count, const QModelIndex &parent = QModelIndex())
Reimplements QAbstractProxyModel::removeColumns().
Removes the source columns shown at column to column + count - 1 under parent.
bool QSortFilterProxyModel::removeRows(int row, int count, const QModelIndex &parent = QModelIndex())
Reimplements QAbstractProxyModel::removeRows().
Removes the source rows shown at row to row + count - 1 under parent.
int QSortFilterProxyModel::rowCount(const QModelIndex &parent = QModelIndex()) const
Reimplements QAbstractProxyModel::rowCount().
Returns the number of accepted rows under parent.
void QSortFilterProxyModel::setAutoAcceptChildRows(bool accept)
Sets whether the children of an accepted row are accepted too to accept.
This function was introduced in 6.0.
void QSortFilterProxyModel::setDynamicSortFilter(bool enable)
Sets whether the proxy sorts and filters again when the source data changes to enable.
void QSortFilterProxyModel::setFilterCaseSensitivity(Qt::CaseSensitivity cs)
Sets the case sensitivity of the filter to cs.
void QSortFilterProxyModel::setFilterFixedString(const QString &pattern)
Sets the filter to the fixed string pattern, which matches anywhere in the text.
void QSortFilterProxyModel::setFilterKeyColumn(int column)
Sets the source column the filter tests to column; -1 tests all columns, accepting a row if any column matches.
void QSortFilterProxyModel::setFilterRegularExpression(const QString &pattern)
This is an overloaded function.
Sets the filter to a regular expression with the pattern pattern, keeping the case sensitivity.
This function was introduced in 5.12.
void QSortFilterProxyModel::setFilterRegularExpression(const QRegularExpression ®ularExpression)
Sets the filter to the regular expression regularExpression and filters again. Its case-insensitivity option sets filterCaseSensitivity().
This function was introduced in 5.12.
void QSortFilterProxyModel::setFilterRole(int role)
Sets the role whose data the filter tests to role.
void QSortFilterProxyModel::setFilterWildcard(const QString &pattern)
Sets the filter to the shell wildcard pattern, which may match anywhere in the text.
void QSortFilterProxyModel::setRecursiveFilteringEnabled(bool recursive)
Sets whether a row is also accepted when one of its descendants is to recursive.
This function was introduced in 5.10.
void QSortFilterProxyModel::setSortCaseSensitivity(Qt::CaseSensitivity cs)
Sets the case sensitivity of sorting to cs.
void QSortFilterProxyModel::setSortLocaleAware(bool on)
Sets whether strings are sorted with QCollator for the default locale to on.
void QSortFilterProxyModel::setSortRole(int role)
Sets the role whose data is compared when sorting to role.
void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
Reimplements QAbstractProxyModel::setSourceModel().
Sets the source model to sourceModel and resets the proxy.
QModelIndex QSortFilterProxyModel::sibling(int row, int column, const QModelIndex &idx) const
Reimplements QAbstractProxyModel::sibling().
Returns the index at row and column next to idx.
void QSortFilterProxyModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder)
Reimplements QAbstractProxyModel::sort().
Sorts the rows by the proxy column column in order. A column of -1 restores the order of the source.
Qt::CaseSensitivity QSortFilterProxyModel::sortCaseSensitivity() const
Returns the case sensitivity of sorting.
void QSortFilterProxyModel::sortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity)
This signal is emitted when the sort case sensitivity changes to sortCaseSensitivity.
This function was introduced in 6.0.
int QSortFilterProxyModel::sortColumn() const
Returns the proxy column used for sorting, or -1 if the rows keep the source order.
This function was introduced in 4.5.
void QSortFilterProxyModel::sortLocaleAwareChanged(bool sortLocaleAware)
This signal is emitted when locale-aware sorting is switched to sortLocaleAware.
This function was introduced in 6.0.
Qt::SortOrder QSortFilterProxyModel::sortOrder() const
Returns the sort order.
This function was introduced in 4.5.
int QSortFilterProxyModel::sortRole() const
Returns the role whose data is compared when sorting; the default is Qt::DisplayRole.
void QSortFilterProxyModel::sortRoleChanged(int sortRole)
This signal is emitted when the sort role changes to sortRole.
This function was introduced in 6.0.