6.0.0-rc4Source

QSortFilterProxyModel Class

The QSortFilterProxyModel class provides support for sorting and filtering data passed between another model and a view. More…

Header#include <QSortFilterProxyModel>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
InheritsQAbstractProxyModel

List of all members, including inherited members

Public Functions

Reimplemented Public Functions

voidsetSourceModel(QAbstractItemModel *sourceModel)
QModelIndexmapToSource(const QModelIndex &proxyIndex) const
QModelIndexmapFromSource(const QModelIndex &sourceIndex) const
QModelIndexindex(int row, int column, const QModelIndex &parent = QModelIndex()) const
QModelIndexparent(const QModelIndex &child) const
QModelIndexsibling(int row, int column, const QModelIndex &idx) const
introwCount(const QModelIndex &parent = QModelIndex()) const
intcolumnCount(const QModelIndex &parent = QModelIndex()) const
boolhasChildren(const QModelIndex &parent = QModelIndex()) const
boolinsertRows(int row, int count, const QModelIndex &parent = QModelIndex())
boolinsertColumns(int column, int count, const QModelIndex &parent = QModelIndex())
boolremoveRows(int row, int count, const QModelIndex &parent = QModelIndex())
boolremoveColumns(int column, int count, const QModelIndex &parent = QModelIndex())
voidsort(int column, Qt::SortOrder order = Qt::AscendingOrder)

Public Slots

voidsetFilterRegularExpression(const QString &pattern)
voidsetFilterRegularExpression(const QRegularExpression &regularExpression)
voidsetFilterWildcard(const QString &pattern)
voidsetFilterFixedString(const QString &pattern)
voidinvalidate()

Signals

voiddynamicSortFilterChanged(bool dynamicSortFilter)
voidfilterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity)
voidsortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity)
voidsortLocaleAwareChanged(bool sortLocaleAware)
voidsortRoleChanged(int sortRole)
voidfilterRoleChanged(int filterRole)
voidrecursiveFilteringEnabledChanged(bool recursiveFilteringEnabled)
voidautoAcceptChildRowsChanged(bool autoAcceptChildRows)

Protected Functions

virtual boolfilterAcceptsRow(int source_row, const QModelIndex &source_parent) const
virtual boolfilterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
virtual boollessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
voidinvalidateFilter()
voidinvalidateRowsFilter()
voidinvalidateColumnsFilter()

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

[explicit] 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.

[override] 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.

[signal] 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.

[override] 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.

[signal] void QSortFilterProxyModel::dynamicSortFilterChanged(bool dynamicSortFilter)

This signal is emitted when dynamicSortFilter() changes to dynamicSortFilter.

This function was introduced in 6.0.

[virtual, protected] 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.

[virtual, protected] 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.

[signal] 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.

[signal] void QSortFilterProxyModel::filterRoleChanged(int filterRole)

This signal is emitted when the filter role changes to filterRole.

This function was introduced in 6.0.

[override] bool QSortFilterProxyModel::hasChildren(const QModelIndex &parent = QModelIndex()) const

Reimplements QAbstractProxyModel::hasChildren().

Returns true if parent has accepted child rows and columns.

[override] 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.

[override] 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.

[override] 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()).

[slot] void QSortFilterProxyModel::invalidate()

Sorts and filters the rows again.

See also invalidateFilter().

[protected] void QSortFilterProxyModel::invalidateColumnsFilter()

Filters the columns again.

This function was introduced in 6.0.

[protected] void QSortFilterProxyModel::invalidateFilter()

Filters the rows and columns again, for subclasses whose filter changed.

[protected] 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.

[virtual, protected] 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.

[override] 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.

[override] QModelIndex QSortFilterProxyModel::mapToSource(const QModelIndex &proxyIndex) const

Reimplements QAbstractProxyModel::mapToSource().

Returns the source index of proxyIndex.

[override] QModelIndex QSortFilterProxyModel::parent(const QModelIndex &child) const

Reimplements QAbstractProxyModel::parent().

Returns the parent of child.

[signal] void QSortFilterProxyModel::recursiveFilteringEnabledChanged(bool recursiveFilteringEnabled)

This signal is emitted when recursive filtering is switched to recursiveFilteringEnabled.

This function was introduced in 6.0.

[override] 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.

[override] 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.

[override] 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.

[slot] 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.

[slot] 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.

[slot] void QSortFilterProxyModel::setFilterRegularExpression(const QRegularExpression &regularExpression)

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.

[slot] 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.

[override] void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel)

Reimplements QAbstractProxyModel::setSourceModel().

Sets the source model to sourceModel and resets the proxy.

[override] QModelIndex QSortFilterProxyModel::sibling(int row, int column, const QModelIndex &idx) const

Reimplements QAbstractProxyModel::sibling().

Returns the index at row and column next to idx.

[override] 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.

[signal] 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.

[signal] 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.

[signal] void QSortFilterProxyModel::sortRoleChanged(int sortRole)

This signal is emitted when the sort role changes to sortRole.

This function was introduced in 6.0.