本页内容

QSortFilterProxyModel Class

QSortFilterProxyModel 类为在另一个模型与视图之间传递的数据提供了排序和过滤支持。更多内容...

标题: #include <QSortFilterProxyModel>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承自: QAbstractProxyModel

公共类型

(since 6.12) enum class DataChangeRelevanceFlag { NotRelevant, RelevantForFiltering, RelevantForSorting, RelevantForFilteringAndSorting }
flags DataChangeRelevanceFlags
(since 6.10) enum class Direction { Rows, Columns, Both }
flags Directions

属性

公共函数

QSortFilterProxyModel(QObject *parent = nullptr)
virtual ~QSortFilterProxyModel()
bool autoAcceptChildRows() const
QBindable<bool> bindableAutoAcceptChildRows()
QBindable<bool> bindableDynamicSortFilter()
QBindable<Qt::CaseSensitivity> bindableFilterCaseSensitivity()
QBindable<int> bindableFilterKeyColumn()
QBindable<QRegularExpression> bindableFilterRegularExpression()
QBindable<int> bindableFilterRole()
QBindable<bool> bindableIsSortLocaleAware()
QBindable<bool> bindableRecursiveFilteringEnabled()
QBindable<Qt::CaseSensitivity> bindableSortCaseSensitivity()
QBindable<int> bindableSortRole()
bool dynamicSortFilter() const
Qt::CaseSensitivity filterCaseSensitivity() const
int filterKeyColumn() const
QRegularExpression filterRegularExpression() const
int filterRole() const
bool isRecursiveFilteringEnabled() const
bool isSortLocaleAware() const
void setAutoAcceptChildRows(bool accept)
void setDynamicSortFilter(bool enable)
void setFilterCaseSensitivity(Qt::CaseSensitivity cs)
void setFilterKeyColumn(int column)
void setFilterRole(int role)
void setRecursiveFilteringEnabled(bool recursive)
void setSortCaseSensitivity(Qt::CaseSensitivity cs)
void setSortLocaleAware(bool on)
void setSortRole(int role)
Qt::CaseSensitivity sortCaseSensitivity() const
int sortColumn() const
Qt::SortOrder sortOrder() const
int sortRole() const

重新实现的公共函数

virtual QModelIndex buddy(const QModelIndex &index) const override
virtual bool canFetchMore(const QModelIndex &parent) const override
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
virtual void fetchMore(const QModelIndex &parent) override
virtual Qt::ItemFlags flags(const QModelIndex &index) const override
virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const override
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override
virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
virtual QItemSelection mapSelectionFromSource(const QItemSelection &sourceSelection) const override
virtual QItemSelection mapSelectionToSource(const QItemSelection &proxySelection) const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const override
virtual QMimeData *mimeData(const QModelIndexList &indexes) const override
virtual QStringList mimeTypes() const override
virtual QModelIndex parent(const QModelIndex &child) const override
virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
virtual QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override
virtual QSize span(const QModelIndex &index) const override
virtual Qt::DropActions supportedDropActions() const override

公共插槽

void invalidate()
void setFilterFixedString(const QString &pattern)
void setFilterRegularExpression(const QString &pattern)
void setFilterRegularExpression(const QRegularExpression &regularExpression)
void setFilterWildcard(const QString &pattern)

信号

(since 6.0) void autoAcceptChildRowsChanged(bool autoAcceptChildRows)
void filterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity)
void filterRoleChanged(int filterRole)
void recursiveFilteringEnabledChanged(bool recursiveFilteringEnabled)
void sortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity)
void sortLocaleAwareChanged(bool sortLocaleAware)
void sortRoleChanged(int sortRole)

受保护函数

(since 6.9) void beginFilterChange()
(since 6.12) QSortFilterProxyModel::DataChangeRelevanceFlags dataChangeRelevanceFlags(const QModelIndex &sourceTopLeft, const QModelIndex &sourceBottomRight, const QList<int> &roles) const
(since 6.10) void endFilterChange(QSortFilterProxyModel::Directions directions = Direction::Both)
virtual bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
(since 6.0, until 6.13) void invalidateColumnsFilter()
(until 6.13) void invalidateFilter()
(since 6.0, until 6.13) void invalidateRowsFilter()
virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const

详细说明

QSortFilterProxyModel 可用于对项目进行排序、过滤项目,或同时执行这两种操作。 该模型通过将源模型提供的索引映射到对应不同位置的新索引,从而转换源模型的结构,供视图使用。这种方法允许从视图的角度对给定的源模型进行重组,而无需对底层数据进行任何转换,也不会导致内存中数据重复。

假设我们要对自定义模型提供的项目进行排序和过滤。如果不进行排序和过滤,用于设置模型和视图的代码如下所示:

        QTreeView *treeView = new QTreeView;
        MyItemModel *model = new MyItemModel(this);

        treeView->setModel(model);

要为MyItemModel 添加排序和过滤支持,我们需要创建一个QSortFilterProxyModel,调用setSourceModel()并将MyItemModel 作为参数传入,然后将该QSortFilterProxyModel安装到视图上:

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

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

此时,排序和过滤功能均未启用;视图中显示的是原始数据。通过 QSortFilterProxyModel 进行的任何更改都会应用到原始模型上。

QSortFilterProxyModel 充当原始模型的封装器。若需将源QModelIndex转换为已排序/已筛选的模型索引,或反之,请使用mapToSource()、mapFromSource()、mapSelectionToSource() 以及mapSelectionFromSource() 方法。

注意:默认情况下 ,每当原始模型发生变化时,该模型会动态地对数据进行重新排序和重新过滤。可以通过设置dynamicSortFilter 属性来更改此行为。

“基本排序/筛选模型”和“自定义排序/筛选模型”示例演示了如何使用 QSortFilterProxyModel 执行基本排序和筛选,以及如何通过继承其子类来实现自定义行为。

排序

QTableView 和QTreeView 都具有一个sortingEnabled 属性,用于控制用户是否可以通过点击视图的水平标题栏对视图进行排序。例如:

        treeView->setSortingEnabled(true);

当此功能开启时(默认关闭),点击表头区域即可按该列对项目进行排序。通过反复点击,用户可在升序和降序之间切换。

一个已排序的 QTreeView

在后台,视图会调用模型上的sort() 虚拟函数来重新排序模型中的数据。 要使数据可排序,您可以在模型中实现sort(),或者使用QSortFilterProxyModel来封装您的模型——QSortFilterProxyModel提供了一个通用的sort()重写实现,该实现基于项的sortRole()(默认是Qt::DisplayRole )进行操作,并支持多种数据类型,包括int 、QString 、 以及QDateTime 。对于分层模型,排序将递归应用于所有子项。默认情况下,字符串比较区分大小写;可通过设置sortCaseSensitivity 属性来更改此行为。

要实现自定义排序行为,需继承 QSortFilterProxyModel 并重写用于比较项的lessThan() 方法。例如:

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();

    static const QRegularExpression emailPattern(u"[\\w\\.]*@[\\w\\.]*"_s);

    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;
}

(此代码片段摘自“自定义排序/过滤模型”示例。)

另一种排序方法是禁用视图上的排序功能,并强制用户按照特定顺序进行操作。具体实现方式是:在 QSortFilterProxyModel 上(或者如果原始模型实现了sort() 方法,则在原始模型上)显式调用sort() 方法,并将所需的列和排序顺序作为参数传入。例如:

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

QSortFilterProxyModel 可以按列 -1 进行排序,此时它将恢复为底层源模型的排序顺序。

注意: sortColumn() 返回最近使用的排序列。默认值为 -1,这意味着该代理模型不进行排序。此外,请注意sort() 会将sortColumn() 设置为最近使用的排序列。

筛选

除了排序外,QSortFilterProxyModel 还可以用于隐藏不符合特定过滤条件的项目。过滤器通过QRegularExpression 对象指定,并针对给定列应用于每个项目的filterRole()(默认值为Qt::DisplayRole )。QRegularExpression 对象可用于匹配正则表达式、通配符模式或固定字符串。例如:

        proxyModel->setFilterRegularExpression(QRegularExpression("\\.png", QRegularExpression::CaseInsensitiveOption));
        proxyModel->setFilterKeyColumn(1);

对于分层模型,过滤器将递归地应用于所有子项。如果父项不匹配过滤条件,则其所有子项都不会显示。

一个常见的用例是让用户在QLineEdit 中指定过滤器正则表达式、通配符模式或固定字符串。要实现这一点,请将QLineEdit::textChanged()或textEdited()信号连接到setFilterRegularExpression()、setFilterWildcard()或setFilterFixedString()中的任意一个。

通过重写filterAcceptsRow()和filterAcceptsColumn()函数,可以实现自定义筛选行为。例如(摘自“自定义排序/筛选模型”示例),以下实现将忽略filterKeyColumn 属性,并对第0、1和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(filterRegularExpression())
            || sourceModel()->data(index1).toString().contains(filterRegularExpression()))
            && dateInRange(sourceModel()->data(index2).toDate());
}

(此代码片段摘自“自定义排序/过滤模型”示例。)

如果您需要处理大量过滤操作,并且必须反复调用beginFilterChange() /endFilterChange(),那么根据您模型的实现情况,使用beginResetModel() /endResetModel() 可能会更高效。但是,beginResetModel() /endResetModel() 会将代理模型恢复到原始状态,从而丢失选择信息,并导致代理模型被重新加载。

子类化

由于 `QAbstractProxyModel ` 及其子类继承自 `QAbstractItemModel`,因此关于普通模型子类化的许多建议同样适用于代理模型。此外,值得注意的是,该类中许多函数的默认实现都是这样编写的:它们会调用相关源模型中的等效函数。 对于行为更复杂的源模型,可能需要重写这一简单的代理机制;例如,如果源模型提供了自定义的hasChildren()实现,您也应在代理模型中提供相应的实现。

注意: 有关模型子类化的通用指南,请参阅《模型子类化参考》。

另请参阅 QAbstractProxyModel 、QAbstractItemModel 、Model/View 编程、基本排序/过滤模型示例、自定义排序/过滤模型示例以及QIdentityProxyModel 。

成员类型文档

[since 6.12] enum class QSortFilterProxyModel::DataChangeRelevanceFlag
flags QSortFilterProxyModel::DataChangeRelevanceFlags

此枚举用于指定源变更所涉及的操作。

常量值描述
QSortFilterProxyModel::DataChangeRelevanceFlag::NotRelevant0x00该更改与过滤和排序均无关
QSortFilterProxyModel::DataChangeRelevanceFlag::RelevantForFiltering0x01该更改与过滤相关
QSortFilterProxyModel::DataChangeRelevanceFlag::RelevantForSorting0x02该更改对排序有影响
QSortFilterProxyModel::DataChangeRelevanceFlag::RelevantForFilteringAndSortingRelevantForFiltering | RelevantForSorting该更改对筛选和排序均相关

该枚举类型在 Qt 6.12 中引入。

DataChangeRelevanceFlags 类型是QFlags<DataChangeRelevanceFlag> 的 typedef。它存储了 DataChangeRelevanceFlag 值的“或”组合。

另请参阅 dataChangeRelevanceFlags()。

[since 6.10] enum class QSortFilterProxyModel::Direction
flags QSortFilterProxyModel::Directions

该枚举用于指定在更改过滤器参数时,自定义过滤器所应用的方向。

常量值描述
QSortFilterProxyModel::Direction::Rows0x01过滤器适用于rows
QSortFilterProxyModel::Direction::Columns0x02筛选器应用于columns
QSortFilterProxyModel::Direction::BothRows | Columns该筛选器同时适用于行和列

该枚举类型在 Qt 6.10 中引入。

“Directions”类型是QFlags<Direction> 的 typedef。它存储 Direction 值的或(OR)组合。

另请参阅 beginFilterChange() 和endFilterChange()。

属性文档

[bindable, since 6.0] autoAcceptChildRows : bool

注意:此 属性支持QProperty 绑定。

如果为 true,代理模型将不会过滤掉已接受行子节点的子节点,即使这些子节点在其他情况下会被过滤掉。

默认值为 false。

该枚举在 Qt 6.0 中引入。

访问函数:

bool autoAcceptChildRows() const
void setAutoAcceptChildRows(bool accept)

通知器信号:

void autoAcceptChildRowsChanged(bool autoAcceptChildRows)

另请参阅 recursiveFilteringEnabled 和filterAcceptsRow()。

[bindable] dynamicSortFilter : bool

注意:此 属性支持QProperty 绑定。

该属性用于指定当源模型的内容发生变化时,代理模型是否会动态进行排序和过滤

请注意,当 dynamicSortFilter 为 true 时,不应通过代理模型更新源模型。 例如,如果您在QComboBox 上设置了代理模型,那么使用更新模型的函数(例如addItem())将无法按预期工作。另一种方法是将dynamicSortFilter设置为false,并在向QComboBox 添加项目后调用sort()。

默认值为 true。

访问函数:

bool dynamicSortFilter() const
void setDynamicSortFilter(bool enable)

另请参阅 sortColumn()。

[bindable] filterCaseSensitivity : Qt::CaseSensitivity

注意:此 属性支持QProperty 绑定。

该属性用于指定用于过滤源模型内容的QRegularExpression 模式是否区分大小写。

默认情况下,该过滤器区分大小写。

注意:设置 此属性会将新的区分大小写设置传播到filterRegularExpression 属性,从而导致其绑定失效。同样,显式设置filterRegularExpression 也会更改当前的区分大小写设置,从而导致其绑定失效。

访问函数:

Qt::CaseSensitivity filterCaseSensitivity() const
void setFilterCaseSensitivity(Qt::CaseSensitivity cs)

通知信号:

void filterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity)

另请参阅 filterRegularExpression 和sortCaseSensitivity 。

[bindable] filterKeyColumn : int

注意:此 属性支持QProperty 绑定。

该属性指定用于读取用于过滤源模型内容的键的列。

默认值为 0。如果值为 -1,则将从所有列中读取键。

访问函数:

int filterKeyColumn() const
void setFilterKeyColumn(int column)

[bindable] filterRegularExpression : QRegularExpression

注意:此 属性支持QProperty 绑定。

该属性存储用于过滤源模型内容的QRegularExpression

通过QRegularExpression 重载方法设置此属性将覆盖当前的filterCaseSensitivity 。默认情况下,QRegularExpression 为空字符串,表示匹配所有内容。

如果未设置QRegularExpression 或设置为空字符串,则源模型中的所有内容都将被接受。

正则表达式会与源模型中的每一行进行匹配,因此复杂或格式错误的模式可能会导致整个应用程序运行缓慢甚至死机,特别是在处理大型模型时。 建议应用程序开发人员在使用正则表达式之前,先对其性能和可扩展性进行测试。如果正则表达式来自潜在不可信的输入,请考虑在将其传递给此方法之前对其进行净化处理,或者改用setFilterFixedString()。

注意:设置 此属性会将新正则表达式的大小写敏感性传播到filterCaseSensitivity 属性,从而破坏其绑定。同样,显式设置filterCaseSensitivity 会更改当前正则表达式的大小写敏感性,从而破坏其绑定。

访问函数:

QRegularExpression filterRegularExpression() const
void setFilterRegularExpression(const QString &pattern)
void setFilterRegularExpression(const QRegularExpression &regularExpression)

另请参阅 filterCaseSensitivity 、setFilterWildcard() 和setFilterFixedString()。

[bindable] filterRole : int

注意:此 属性支持QProperty 绑定。

该属性存储用于在过滤项目时查询源模型数据的项角色。

默认值为Qt::DisplayRole 。

访问函数:

int filterRole() const
void setFilterRole(int role)

通知器信号:

void filterRoleChanged(int filterRole)

另请参阅 filterAcceptsRow()。

[bindable] isSortLocaleAware : bool

注意:此 属性支持QProperty 绑定。

该属性存储用于排序时比较字符串的“考虑本地化”设置

默认情况下,排序不考虑本地化。

访问函数:

bool isSortLocaleAware() const
void setSortLocaleAware(bool on)

通知信号:

void sortLocaleAwareChanged(bool sortLocaleAware)

另请参阅 sortCaseSensitivity 和lessThan()。

[bindable] recursiveFilteringEnabled : bool

注意:该 属性支持QProperty 绑定。

该属性控制是否对子节点递归应用过滤器;对于任何匹配的子节点,其父节点也将显示出来。

默认值为 false。

访问函数:

bool isRecursiveFilteringEnabled() const
void setRecursiveFilteringEnabled(bool recursive)

通知器信号:

void recursiveFilteringEnabledChanged(bool recursiveFilteringEnabled)

另请参阅 autoAcceptChildRows 和filterAcceptsRow()。

[bindable] sortCaseSensitivity : Qt::CaseSensitivity

注意:此 属性支持QProperty 绑定。

该属性存储在排序时用于比较字符串的大小写敏感性设置

默认情况下,排序区分大小写。

访问函数:

Qt::CaseSensitivity sortCaseSensitivity() const
void setSortCaseSensitivity(Qt::CaseSensitivity cs)

通知信号:

void sortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity)

另请参阅 filterCaseSensitivity 和lessThan()。

[bindable] sortRole : int

注意:此 属性支持QProperty 绑定。

该属性存储用于在对项目进行排序时查询源模型数据的项目角色。

默认值为Qt::DisplayRole 。

访问函数:

int sortRole() const
void setSortRole(int role)

通知器信号:

void sortRoleChanged(int sortRole)

另请参阅 lessThan()。

成员函数文档

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

根据给定的parent 构建一个排序过滤器模型。

[virtual noexcept] QSortFilterProxyModel::~QSortFilterProxyModel()

删除此排序过滤器模型。

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

当autoAcceptChildRows 属性的值发生变化时,会触发此信号。

注意: 属性 `autoAcceptChildRows`的通知 信号。

该函数在 Qt 6.0 中引入。

另请参阅 autoAcceptChildRows 。

[protected, since 6.9] void QSortFilterProxyModel::beginFilterChange()

准备更换过滤器。

如果您正在实现自定义过滤(例如filterAcceptsRow()),且过滤器参数即将发生变更,则应调用此函数。

void MySortFilterProxyModel::setFilterMaximumDate(QDate date)
{
    beginFilterChange();
    maxDate = date;
    endFilterChange(QSortFilterProxyModel::Direction::Rows);
}

过滤器更改完成后,请调用endFilterChange():若为行过滤器,则传入Direction::Rows ;若为列过滤器,则传入Direction::Columns ;若同时对行和列进行过滤,则传入Direction::Columns|Direction::Rows 。

该函数在 Qt 6.9 中引入。

另请参阅 endFilterChange()。

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

重写了:QAbstractProxyModel::buddy(const QModelIndex &index) const。

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

重写:QAbstractProxyModel::canFetchMore(const QModelIndex &parent) const。

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

重写了:QAbstractItemModel::columnCount(const QModelIndex &parent) const。

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

重写了:QAbstractProxyModel::data(const QModelIndex &proxyIndex, int role) const。

另请参阅 setData()。

[protected invokable, since 6.12] QSortFilterProxyModel::DataChangeRelevanceFlags QSortFilterProxyModel::dataChangeRelevanceFlags(const QModelIndex &sourceTopLeft, const QModelIndex &sourceBottomRight, const QList<int> &roles) const

在子类中重新实现此插槽,以指示源模型的更改何时与过滤或排序相关。此功能用于性能优化,以避免在源模型发生不影响排序和过滤结果的更改时,不必要地调用filterAcceptsRow() 和lessThan()。 参数sourceTopLeft 、sourceBottomRight 和roles 由源模型的dataChanged() 信号发出,该信号对应于正在评估相关性的变更。

注意:由于 二进制兼容性限制,在 Qt 7 之前,此方法不能是虚方法。在此期间,可在子类中将其重新声明为槽(slot)或“Q_INVOKABLE ”方法,QSortFilterProxyModel 将动态检测并调用它。

注意: 默认实现 假设任何更改都与过滤相关,而只有源排序列中的更改与排序相关。

注意:该 函数可通过元对象系统和 QML 调用。参见Q_INVOKABLE 。

该函数在 Qt 6.12 中引入。

另请参阅 DataChangeRelevanceFlags 、filterAcceptsRow() 以及lessThan()。

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

重写了:QAbstractProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)。

[protected, since 6.10] void QSortFilterProxyModel::endFilterChange(QSortFilterProxyModel::Directions directions = Direction::Both)

在过滤器参数发生变化后,使当前过滤失效。

如果您实现了自定义筛选(例如 `filterAcceptsRow()`),且筛选参数已发生变化,则应调用此函数。参数 `directions ` 指定自定义筛选影响行、列还是两者。

当过滤参数即将发生变化时,请调用 `beginFilterChange()`;待过滤参数变更完成后,请随即调用此函数。 对于行过滤器,请将directions 设置为Direction::Rows (即实现了filterAcceptsRow());对于列过滤器,请设置为Direction::Columns (即实现了filterAcceptsColumn());若同时实现了行和列过滤器,则请设置为Direction::Both 。

该函数于 Qt 6.10 中引入。

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

重写了:QAbstractProxyModel::fetchMore(const QModelIndex &parent)。

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

如果应将由给定的source_column 和source_parent 所指明的列中的项包含在模型中,则返回true ;否则返回false 。

注意: 默认实现 始终返回true 。您必须重写此方法才能获得所述的行为。

另请参阅 filterAcceptsRow()、setFilterFixedString()、setFilterRegularExpression() 以及setFilterWildcard()。

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

如果给定的source_row 和source_parent 所指向的行中的项应被纳入模型,则返回true ;否则返回false。

默认实现会在相关项的值与过滤字符串、通配符字符串或正则表达式匹配时返回 `true `。

注意:默认情况下,会 使用Qt::DisplayRole 来确定该行是否应被接受。可以通过设置filterRole 属性来更改此行为。

另请参阅 filterAcceptsColumn()、setFilterFixedString()、setFilterRegularExpression() 和setFilterWildcard()。

[signal] void QSortFilterProxyModel::filterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity)

当过滤器的大小写敏感性更改为filterCaseSensitivity 时,将发出此信号。

注意: 这是属性filterCaseSensitivity的通知 信号。

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

当过滤器角色变为filterRole 时,会发出此信号。

注意: 这是属性filterRole 的Notifier 信号。

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

重写了:QAbstractProxyModel::flags(const QModelIndex &index) const。

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

重写了:QAbstractProxyModel::hasChildren(const QModelIndex &parent) const。

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

重写了:QAbstractProxyModel::headerData(int section, Qt::Orientation orientation, int role) const。

另请参阅 setHeaderData()。

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

重写了:QAbstractItemModel::index(int row, int column, const QModelIndex &parent) const。

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

重写了:QAbstractItemModel::insertColumns(int column, int count, const QModelIndex &parent)。

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

重写了:QAbstractItemModel::insertRows(int row, int count, const QModelIndex &parent)。

[slot] void QSortFilterProxyModel::invalidate()

清除当前的排序和筛选设置。

另请参阅 beginFilterChange() 和endFilterChange()。

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

该函数计划在 6.13 版本中被废弃。

请改用beginFilterChange() 和endFilterChange(Direction::Rows)。

使列的当前过滤失效。

如果您正在实现自定义过滤(通过filterAcceptsColumn()),且过滤参数已发生变化,则应调用此函数。它与invalidateFilter() 的区别在于,它不会调用filterAcceptsRow(),而只会调用filterAcceptsColumn()。如果您想隐藏或显示行内容未发生变化的列,可以使用此函数代替invalidateFilter()。

在过滤器参数发生变化之前,请调用beginFilterChange()。

该函数在 Qt 6.0 中引入。

另请参阅 invalidate()、invalidateRowsFilter() 和beginFilterChange()。

[protected, until 6.13] void QSortFilterProxyModel::invalidateFilter()

该函数计划在 6.13 版本中被废弃。

请改用beginFilterChange() 和endFilterChange()。

使当前过滤失效。

如果您正在实现自定义过滤(例如filterAcceptsRow()),且过滤参数已发生变化,则应调用此函数。

在过滤器参数更改之前,请调用beginFilterChange()。

void MySortFilterProxyModel::setFilterMaximumDate(QDate date)
{
    beginFilterChange();
    maxDate = date;
    endFilterChange(QSortFilterProxyModel::Direction::Rows);
}

另请参阅 invalidate()、invalidateColumnsFilter()、invalidateRowsFilter() 和beginFilterChange()。

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

该函数计划在 6.13 版本中被废弃。

请改用beginFilterChange() 和endFilterChange(Direction::Columns)。

使当前对行进行的过滤失效。

如果您正在实现自定义过滤(通过filterAcceptsRow()),且过滤参数已发生变化,则应调用此函数。它与invalidateFilter() 的区别在于,它不会调用filterAcceptsColumn(),而只会调用filterAcceptsRow()。如果您想隐藏或显示列内容未发生变化的行,可以使用此函数代替invalidateFilter()。

在过滤器参数发生变化之前,请调用beginFilterChange()。

该函数在 Qt 6.0 中引入。

另请参阅 invalidate()、invalidateFilter() 和invalidateColumnsFilter()。

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

如果给定索引source_left 所指向的项的值小于给定索引source_right 所指向的项的值,则返回true ;否则返回false 。

该函数在排序时用作operator< ,并使用QAbstractItemModel::compareData()。

默认情况下,QString的比较区分大小写;这可以通过sortCaseSensitivity 属性进行更改。

默认情况下,将使用与QModelIndexes关联的Qt::DisplayRole 进行比较。可以通过设置sortRole 属性来更改这一点。

注意: 传入的索引对应于源模型。

另请参阅 sortRole 、sortCaseSensitivity 和dynamicSortFilter 。

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

重写:QAbstractProxyModel::mapFromSource(const QModelIndex &sourceIndex) const。

根据源模型中的sourceIndex ,返回QSortFilterProxyModel 中的模型索引。

另请参阅 mapToSource()。

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

重写了:QAbstractProxyModel::mapSelectionFromSource(const QItemSelection &sourceSelection) const。

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

重写了:QAbstractProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) const。

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

重写:QAbstractProxyModel::mapToSource(const QModelIndex &proxyIndex) const。

从排序过滤器模型中返回与给定的proxyIndex 对应的源模型索引。

另请参阅 mapFromSource()。

[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

重写了以下函数:QAbstractItemModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const。

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

重写了:QAbstractProxyModel::mimeData(const QModelIndexList &indexes) const。

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

重新实现了:QAbstractProxyModel::mimeTypes() const。

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

重写了:QAbstractItemModel::parent(const QModelIndex &index) const。

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

当递归过滤器设置更改为recursiveFilteringEnabled 时,会发出此信号。

注意: 这是属性recursiveFilteringEnabled的通知器 信号。

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

重写了:QAbstractItemModel::removeColumns (int column, int count, const QModelIndex &parent)。

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

重写了:QAbstractItemModel::removeRows (int row, int count, const QModelIndex &parent)。

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

重写了:QAbstractItemModel::rowCount(const QModelIndex &parent) const。

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

重写了:QAbstractProxyModel::setData (const QModelIndex &index, const QVariant &value, int role)。

另请参阅 data()。

[slot] void QSortFilterProxyModel::setFilterFixedString(const QString &pattern)

将用于过滤源模型内容的固定字符串设置为给定的pattern 。

此方法将重置正则表达式选项,但会保留大小写敏感性。

注意:调用 此方法会更新正则表达式,从而打破对filterRegularExpression 的绑定。但这不会影响filterCaseSensitivity 的绑定。

另请参阅 setFilterCaseSensitivity()、setFilterRegularExpression()、setFilterWildcard() 以及filterRegularExpression()。

[slot] void QSortFilterProxyModel::setFilterRegularExpression(const QString &pattern)

将用于过滤源模型内容的正则表达式设置为pattern 。

在新代码中应优先使用此方法,因为它会在内部使用QRegularExpression 。

此方法将重置正则表达式选项,但会保留大小写敏感性。

注意:调用 此方法会更新正则表达式,从而打破对 `filterRegularExpression` 的绑定。但这不会影响 `filterCaseSensitivity ` 的绑定。

注意: 属性filterRegularExpression 的设置 函数。

注意:此 插槽已被重载。要连接到此插槽:

// Connect using qOverload:
connect(sender, &SenderClass::signal,
        sortFilterProxyModel, qOverload(&QSortFilterProxyModel::setFilterRegularExpression));

// Or using a lambda as wrapper:
connect(sender, &SenderClass::signal,
        sortFilterProxyModel, [receiver = sortFilterProxyModel](const QString &pattern) { receiver->setFilterRegularExpression(pattern); });
有关更多示例和方法,请参阅“连接到重载的槽”。

另请参阅 setFilterCaseSensitivity()、setFilterWildcard()、setFilterFixedString() 以及filterRegularExpression()。

[slot] void QSortFilterProxyModel::setFilterWildcard(const QString &pattern)

将用于过滤源模型内容的通配符表达式设置为给定的pattern 。

此方法将重置正则表达式选项,但会保留大小写敏感性。

注意:调用 此方法会更新正则表达式,从而断开对filterRegularExpression 的绑定。但这不会影响filterCaseSensitivity 的绑定。

另请参阅 setFilterCaseSensitivity()、setFilterRegularExpression()、setFilterFixedString() 和filterRegularExpression()。

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

重写了:QAbstractProxyModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)。

另请参阅 headerData()。

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

重写了:QAbstractProxyModel::setSourceModel(QAbstractItemModel *sourceModel)。

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

重写:QAbstractProxyModel::sibling (int row, int column, const QModelIndex &idx)const。

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

重写了:QAbstractProxyModel::sort(int column, Qt::SortOrder order)。

根据给定的order ,按column 对模型进行排序。如果排序column 小于零,则模型将根据给定的order 按源模型行进行排序。

另请参阅 sortColumn()。

[signal] void QSortFilterProxyModel::sortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity)

当排序的大写小写敏感性更改为sortCaseSensitivity 时,会发出此信号。

注意: 这是属性sortCaseSensitivity的通知 信号。

int QSortFilterProxyModel::sortColumn() const

返回当前用于排序的列

此方法返回最近使用的排序列。默认值为 -1,表示该代理模型不进行排序。

另请参阅 sort()。

[signal] void QSortFilterProxyModel::sortLocaleAwareChanged(bool sortLocaleAware)

当“支持区域设置”的配置更改为sortLocaleAware 时,会发出此信号。

注意: 这是针对属性isSortLocaleAware的通知 信号。

Qt::SortOrder QSortFilterProxyModel::sortOrder() const

返回当前用于排序的排序顺序

该函数返回最近使用的排序顺序。默认值为Qt::AscendingOrder 。

另请参阅 sort()。

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

当排序角色变为sortRole 时,会发出此信号。

注意: 这是属性sortRole 的Notifier 信号。

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

重写了:QAbstractProxyModel::span(const QModelIndex &index) const。

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

重新实现了:QAbstractProxyModel::supportedDropActions() const。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.