IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QSortFilterProxyModel Class

The https:// class provides support for sorting and filtering data passed between another model and a view.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QSortFilterProxyModel Class▲

  • Header: QSortFilterProxyModel

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

  • Inherits: https://

  • Group: QSortFilterProxyModel is part of model-view

Detailed Description▲

https:// can be used for sorting items, filtering out items, or both. The model transforms the structure of a source model by mapping the model indexes it supplies to new indexes, corresponding to different locations, for views to use. This approach allows a given source model to be restructured as far as views are concerned without requiring any transformations on the underlying data, and without duplicating the data in memory.

Let's assume that we want to sort and filter the items provided by a custom model. The code to set up the model and the view, without sorting and filtering, would look like this:

 
Sélectionnez
        QTreeView *treeView = new QTreeView;
        MyItemModel *model = new MyItemModel(this);

        treeView->setModel(model);

To add sorting and filtering support to MyItemModel, we need to create a https://, call https://() with the MyItemModel as argument, and install the https:// on the view:

 
Sélectionnez
        QTreeView *treeView = new QTreeView;
        MyItemModel *sourceModel = new MyItemModel(this);
        QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);

        proxyModel->setSourceModel(sourceModel);
        treeView->setModel(proxyModel);

At this point, neither sorting nor filtering is enabled; the original data is displayed in the view. Any changes made through the https:// are applied to the original model.

The https:// acts as a wrapper for the original model. If you need to convert source https://es to sorted/filtered model indexes or vice versa, use https://(), https://(), https://(), and https://().

By default, the model dynamically re-sorts and re-filters data whenever the original model changes. This behavior can be changed by setting the https:// property.

The https:// and Custom Sort/Filter Model examples illustrate how to use QSortFilterProxyModel to perform basic sorting and filtering and how to subclass it to implement custom behavior.

Sorting▲

QTableView and QTreeView have a sortingEnabled property that controls whether the user can sort the view by clicking the view's horizontal header. For example:

 
Sélectionnez
        treeView->setSortingEnabled(true);

When this feature is on (the default is off), clicking on a header section sorts the items according to that column. By clicking repeatedly, the user can alternate between ascending and descending order.

A sorted QTreeView

Behind the scene, the view calls the https://() virtual function on the model to reorder the data in the model. To make your data sortable, you can either implement https://() in your model, or use a https:// to wrap your model – https:// provides a generic https://() reimplementation that operates on the https://() (https:// by default) of the items and that understands several data types, including int, https://, and https://. For hierarchical models, sorting is applied recursively to all child items. String comparisons are case sensitive by default; this can be changed by setting the https:// property.

Custom sorting behavior is achieved by subclassing https:// and reimplementing https://(), which is used to compare items. For example:

 
Sélectionnez
bool MySortFilterProxyModel::lessThan(const QModelIndex &left,
                                      const QModelIndex &right) const
{
    QVariant leftData = sourceModel()->data(left);
    QVariant rightData = sourceModel()->data(right);

    if (leftData.userType() == QMetaType::QDateTime) {
        return leftData.toDateTime() < rightData.toDateTime();
    } else {
        static const QRegularExpression emailPattern("[\\w\\.]*@[\\w\\.]*");

        QString leftString = leftData.toString();
        if (left.column() == 1) {
            const QRegularExpressionMatch match = emailPattern.match(leftString);
            if (match.hasMatch())
                leftString = match.captured(0);
        }
        QString rightString = rightData.toString();
        if (right.column() == 1) {
            const QRegularExpressionMatch match = emailPattern.match(rightString);
            if (match.hasMatch())
                rightString = match.captured(0);
        }

        return QString::localeAwareCompare(leftString, rightString) < 0;
    }
}

(This code snippet comes from the Custom Sort/Filter Model 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 https://() with the desired column and order as arguments on the https:// (or on the original model if it implements https://()). For example:

 
Sélectionnez
        proxyModel->sort(2, Qt::AscendingOrder);

https:// can be sorted by column -1, in which case it returns to the sort order of the underlying source model.

Filtering▲

In addition to sorting, https:// can be used to hide items that do not match a certain filter. The filter is specified using a https:// object and is applied to the https://() (https:// by default) of each item, for a given column. The https:// object can be used to match a regular expression, a wildcard pattern, or a fixed string. For example:

 
Sélectionnez
        proxyModel->setFilterRegularExpression(QRegularExpression("\.png", QRegularExpression::CaseInsensitiveOption));
        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.

A common use case is to let the user specify the filter regular expression, wildcard pattern, or fixed string in a QLineEdit and to connect the https:// signal to setFilterRegularExpression(), https://(), or https://() to reapply the filter.

Custom filtering behavior can be achieved by reimplementing the https://() and https://() functions. For example (from the https:// example), the following implementation ignores the filterKeyColumn property and performs filtering on columns 0, 1, and 2:

 
Sélectionnez
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(filterRegularExpression())
            || sourceModel()->data(index1).toString().contains(filterRegularExpression()))
            && dateInRange(sourceModel()->data(index2).toDate());
}

(This code snippet comes from the Custom Sort/Filter Model example.)

If you are working with large amounts of filtering and have to invoke https://() repeatedly, using https://() / https://() may be more efficient, depending on the implementation of your model. However, https://() / https://() returns the proxy model to its original state, losing selection information, and will cause the proxy model to be repopulated.

Subclassing▲

Since https:// and its subclasses are derived from https://, much of the same advice about subclassing normal models also applies to proxy models. In addition, it is worth noting that many of the default implementations of functions in this class are written so that they call the equivalent functions in the relevant source model. This simple proxying mechanism may need to be overridden for source models with more complex behavior; for example, if the source model provides a custom https://() implementation, you should also provide one in the proxy model.

Some general guidelines for subclassing models are available in the Model Subclassing Reference.

See Also▲

See also https://, https://, https://, Basic Sort/Filter Model Example, Custom Sort/Filter Model Example, QIdentityProxyModel

Property Documentation▲

 

[bindable, since 6.0] autoAcceptChildRows : bool▲

This property supports https:// bindings.

if true the proxy model will not filter out children of accepted rows, even if they themselves would be filtered out otherwise.

The default value is false.

This property was introduced in Qt 6.0.

See Also▲

See also https://, https://()

[bindable] dynamicSortFilter : bool▲

This property supports https:// bindings.

This property holds whether the proxy model is dynamically sorted and filtered whenever the contents of the source model change

Note that you should not update the source model through the proxy model when dynamicSortFilter is true. For instance, if you set the proxy model on a QComboBox, then using functions that update the model, e.g., addItem(), will not work as expected. An alternative is to set dynamicSortFilter to false and call https://() after adding items to the QComboBox.

The default value is true.

See Also▲

See also https://()

[bindable] filterCaseSensitivity : Qt::CaseSensitivity▲

This property supports https:// bindings.

This property holds the case sensitivity of the https:// pattern used to filter the contents of the source model.

By default, the filter is case sensitive.

Setting this property propagates the new case sensitivity to the https:// property, and so breaks its binding. Likewise explicitly setting https:// changes the current case sensitivity, thereby breaking its binding.

See Also▲

See also https://, https://

[bindable] filterKeyColumn : int▲

This property supports https:// bindings.

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. If the value is -1, the keys will be read from all columns.

[bindable, since 5.12] filterRegularExpression : QRegularExpression▲

This property supports https:// bindings.

This property holds the https:// used to filter the contents of the source model

Setting this property through the https:// overload overwrites the current https://. By default, the https:// is an empty string matching all contents.

If no https:// or an empty string is set, everything in the source model will be accepted.

Setting this property propagates the case sensitivity of the new regular expression to the https:// property, and so breaks its binding. Likewise explicitly setting https:// changes the case sensitivity of the current regular expression, thereby breaking its binding.

This property was introduced in Qt 5.12.

See Also▲

See also https://, https://(), https://()

[bindable] filterRole : int▲

This property supports https:// bindings.

This property holds the item role that is used to query the source model's data when filtering items.

The default value is https://.

See Also▲

See also https://()

[bindable] isSortLocaleAware : bool▲

This property supports https:// bindings.

This property holds the local aware setting used for comparing strings when sorting

By default, sorting is not local aware.

See Also▲

See also https://, https://()

[bindable, since 5.10] recursiveFilteringEnabled : bool▲

This property supports https:// bindings.

This property holds whether the filter to be applied recursively on children, and for any matching child, its parents will be visible as well.

The default value is false.

This property was introduced in Qt 5.10.

See Also▲

See also https://, https://()

[bindable] sortCaseSensitivity : Qt::CaseSensitivity▲

This property supports https:// bindings.

This property holds the case sensitivity setting used for comparing strings when sorting

By default, sorting is case sensitive.

See Also▲

See also https://, https://()

[bindable] sortRole : int▲

This property supports https:// bindings.

This property holds the item role that is used to query the source model's data when sorting items.

The default value is https://.

See Also▲

See also https://()

Member Function Documentation▲

 

[explicit] QSortFilterProxyModel::QSortFilterProxyModel(QObject *parent = nullptr)▲

Constructs a sorting filter model with the given parent.

[virtual] QSortFilterProxyModel::~QSortFilterProxyModel()▲

Destroys this sorting filter model.

[since 6.0] void QSortFilterProxyModel::autoAcceptChildRowsChanged(bool autoAcceptChildRows)▲

This signals is emitted when the value of the autoAcceptChildRows property is changed.

Notifier signal for property https://.

This function was introduced in Qt 6.0.

See Also▲

See also https://

[override virtual] QModelIndex QSortFilterProxyModel::buddy(const QModelIndex &index) const▲

Reimplements: https://.

[override virtual] bool QSortFilterProxyModel::canFetchMore(const QModelIndex &parent) const▲

Reimplements: https://.

[override virtual] int QSortFilterProxyModel::columnCount(const QModelIndex &parent = QModelIndex()) const▲

Reimplements: https://.

[override virtual] QVariant QSortFilterProxyModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const▲

Reimplements: https://.

See Also▲

See also https://()

[override virtual] bool QSortFilterProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)▲

Reimplements: https://.

[override virtual] void QSortFilterProxyModel::fetchMore(const QModelIndex &parent)▲

Reimplements: https://.

[virtual protected] bool QSortFilterProxyModel::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const▲

Returns true if the item in the column indicated by the given source_column and source_parent should be included in the model; otherwise returns false.

The default implementation always returns true. You must reimplement this method to get the described behavior.

See Also▲

See also https://(), https://(), https://(), https://()

[virtual protected] bool QSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const▲

Returns true if the item in the row indicated by the given source_row and source_parent should be included in the model; otherwise returns false.

The default implementation returns true if the value held by the relevant item matches the filter string, wildcard string or regular expression.

By default, the https:// is used to determine if the row should be accepted or not. This can be changed by setting the https:// property.

See Also▲

See also https://(), https://(), https://(), https://()

[since 5.15] void QSortFilterProxyModel::filterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity)▲

This signal is emitted when the case sensitivity of the filter changes to filterCaseSensitivity.

Notifier signal for property https://.

This function was introduced in Qt 5.15.

[since 5.15] void QSortFilterProxyModel::filterRoleChanged(int filterRole)▲

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

Notifier signal for property https://.

This function was introduced in Qt 5.15.

[override virtual] Qt::ItemFlags QSortFilterProxyModel::flags(const QModelIndex &index) const▲

Reimplements: https://.

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

Reimplements: https://.

[override virtual] QVariant QSortFilterProxyModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const▲

Reimplements: https://.

See Also▲

See also https://()

[override virtual] QModelIndex QSortFilterProxyModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const▲

Reimplements: https://.

[override virtual] bool QSortFilterProxyModel::insertColumns(int column, int count, const QModelIndex &parent = QModelIndex())▲

Reimplements: https://.

[override virtual] bool QSortFilterProxyModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex())▲

Reimplements: https://.

void QSortFilterProxyModel::invalidate()▲

Invalidates the current sorting and filtering.

See Also▲

See also https://()

[protected, since 6.0] void QSortFilterProxyModel::invalidateColumnsFilter()▲

Invalidates the current filtering for the columns.

This function should be called if you are implementing custom filtering (by https://()), and your filter parameters have changed. This differs from https://() in that it will not invoke https://(), but only https://(). You can use this instead of https://() if you want to hide or show a column where the rows don't change.

This function was introduced in Qt 6.0.

See Also▲

See also https://(), https://(), https://()

[protected] void QSortFilterProxyModel::invalidateFilter()▲

Invalidates the current filtering.

This function should be called if you are implementing custom filtering (e.g. https://()), and your filter parameters have changed.

See Also▲

See also https://(), https://(), https://()

[protected, since 6.0] void QSortFilterProxyModel::invalidateRowsFilter()▲

Invalidates the current filtering for the rows.

This function should be called if you are implementing custom filtering (by https://()), and your filter parameters have changed. This differs from https://() in that it will not invoke https://(), but only https://(). You can use this instead of https://() if you want to hide or show a row where the columns don't change.

This function was introduced in Qt 6.0.

See Also▲

See also https://(), https://(), https://()

[virtual protected] bool QSortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const▲

Returns true if the value of the item referred to by the given index source_left is less than the value of the item referred to by the given index source_right, otherwise returns false.

This function is used as the < operator when sorting, and handles the following https:// types:

  • https://

  • https://

  • https://

  • https://

  • https://

  • https://

  • https://

  • https://

  • https://

  • https://

  • https://

Any other type will be converted to a https:// using https://().

Comparison of https://s is case sensitive by default; this can be changed using the https:// property.

By default, the https:// associated with the https://es is used for comparisons. This can be changed by setting the https:// property.

The indices passed in correspond to the source model.

See Also▲

See also https://, https://, https://

[override virtual] QModelIndex QSortFilterProxyModel::mapFromSource(const QModelIndex &sourceIndex) const▲

Reimplements: https://.

Returns the model index in the https:// given the sourceIndex from the source model.

See Also▲

See also https://()

[override virtual] QItemSelection QSortFilterProxyModel::mapSelectionFromSource(const QItemSelection &sourceSelection) const▲

Reimplements: https://.

[override virtual] QItemSelection QSortFilterProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) const▲

Reimplements: https://.

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

Reimplements: https://.

Returns the source model index corresponding to the given proxyIndex from the sorting filter model.

See Also▲

See also https://()

[override virtual] QModelIndexList QSortFilterProxyModel::match(const QModelIndex &start, int role, const QVariant &value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const▲

Reimplements: https://.

[override virtual] QMimeData *QSortFilterProxyModel::mimeData(const QModelIndexList &indexes) const▲

Reimplements: https://.

[override virtual] QStringList QSortFilterProxyModel::mimeTypes() const▲

Reimplements: https://.

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

Reimplements: https://.

[since 5.15] void QSortFilterProxyModel::recursiveFilteringEnabledChanged(bool recursiveFilteringEnabled)▲

This signal is emitted when the recursive filter setting is changed to recursiveFilteringEnabled.

Notifier signal for property https://.

This function was introduced in Qt 5.15.

[override virtual] bool QSortFilterProxyModel::removeColumns(int column, int count, const QModelIndex &parent = QModelIndex())▲

Reimplements: https://.

[override virtual] bool QSortFilterProxyModel::removeRows(int row, int count, const QModelIndex &parent = QModelIndex())▲

Reimplements: https://.

[override virtual] int QSortFilterProxyModel::rowCount(const QModelIndex &parent = QModelIndex()) const▲

Reimplements: https://.

[override virtual] bool QSortFilterProxyModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)▲

Reimplements: https://.

See Also▲

See also https://()

void QSortFilterProxyModel::setFilterFixedString(const QString &pattern)▲

Sets the fixed string used to filter the contents of the source model to the given pattern.

This method will reset the regular expression options but respect case sensitivity.

Calling this method updates the regular expression, thereby breaking the binding for https://. However it has no effect on the https:// bindings.

See Also▲

See also https://(), https://(), https://(), https://()

[since 5.12] void QSortFilterProxyModel::setFilterRegularExpression(const QString &pattern)▲

Sets the regular expression used to filter the contents of the source model to pattern.

This method should be preferred for new code as it will use https:// internally.

This method will reset the regular expression options but respect case sensitivity.

Calling this method updates the regular expression, thereby breaking the binding for https://. However it has no effect on the https:// bindings.

Setter function for property https://.

This function was introduced in Qt 5.12.

See Also▲

See also https://(), https://(), https://(), https://()

void QSortFilterProxyModel::setFilterWildcard(const QString &pattern)▲

Sets the wildcard expression used to filter the contents of the source model to the given pattern.

This method will reset the regular expression options but respect case sensitivity.

Calling this method updates the regular expression, thereby breaking the binding for https://. However it has no effect on the https:// bindings.

See Also▲

See also https://(), https://(), https://(), https://()

[override virtual] bool QSortFilterProxyModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole)▲

Reimplements: https://.

See Also▲

See also https://()

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

Reimplements: https://.

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

Reimplements: https://.

[override virtual] void QSortFilterProxyModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder)▲

Reimplements: https://.

[since 5.15] void QSortFilterProxyModel::sortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity)▲

This signal is emitted when the case sensitivity for sorting changes to sortCaseSensitivity.

Notifier signal for property https://.

This function was introduced in Qt 5.15.

int QSortFilterProxyModel::sortColumn() const▲

Returns the column currently used for sorting

This returns the most recently used sort column. The default value is -1, which means that this proxy model does not sort.

See Also▲

See also https://()

[since 5.15] void QSortFilterProxyModel::sortLocaleAwareChanged(bool sortLocaleAware)▲

This signal is emitted when the locale aware setting changes to sortLocaleAware.

Notifier signal for property https://.

This function was introduced in Qt 5.15.

Qt::SortOrder QSortFilterProxyModel::sortOrder() const▲

Returns the order currently used for sorting

This returns the most recently used sort order. The default value is https://.

See Also▲

See also https://()

[since 5.15] void QSortFilterProxyModel::sortRoleChanged(int sortRole)▲

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

Notifier signal for property https://.

This function was introduced in Qt 5.15.

[override virtual] QSize QSortFilterProxyModel::span(const QModelIndex &index) const▲

Reimplements: https://.

[override virtual] Qt::DropActions QSortFilterProxyModel::supportedDropActions() const▲

Reimplements: https://.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.