Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlregexpfilter.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QtQml/qqmlinfo.h>
6#include <QtQmlModels/private/qqmlregexpfilter_p.h>
7#include <QtQmlModels/private/qqmlsortfilterproxymodel_p.h>
8
9QT_BEGIN_NAMESPACE
10
11using namespace Qt::StringLiterals;
12
13/*!
14 \qmltype RegExpFilter
15 \inherits RoleFilter
16 \inqmlmodule QtQml.Models
17 \since 6.12
18 \preliminary
19 \brief Filters data in a \l SortFilterProxyModel by matching a role value
20 against a regular expression.
21
22 RegExpFilter allows filtering model data using regular expression
23 matching on a specified role.
24
25 A JavaScript regular expression literal can be assigned directly to
26 \l regExp:
27
28 \qml
29 SortFilterProxyModel {
30 sourceModel: processModel
31 filters: RegExpFilter {
32 roleName: "userId"
33 regExp: /root/i
34 }
35 }
36 \endqml
37*/
38
39QQmlRegExpFilter::QQmlRegExpFilter(QObject *parent)
40 : QQmlRoleFilter(new QQmlRegExpFilterPrivate, parent)
41{
42}
43
44/*!
45 \qmlproperty regexp RegExpFilter::regExp
46
47 This property holds the regular expression used to filter the data.
48 A JavaScript regular expression literal can be assigned directly.
49
50 By default, this property holds a regular expression with an empty
51 pattern and no pattern options, and all rows pass through the filter.
52*/
53QRegularExpression QQmlRegExpFilter::regExp() const
54{
55 Q_D(const QQmlRegExpFilter);
56 return d->regExp;
57}
58
59void QQmlRegExpFilter::setRegExp(const QRegularExpression &regExp)
60{
61 Q_D(QQmlRegExpFilter);
62 if (d->regExp == regExp)
63 return;
64 d->regExp = regExp;
65 emit regExpChanged();
66 invalidate();
67}
68
69/*!
70 \internal
71*/
72bool QQmlRegExpFilter::filterAcceptsRowInternal(int row, const QModelIndex &sourceParent,
73 const QQmlSortFilterProxyModel *proxyModel) const
74{
75 Q_D(const QQmlRegExpFilter);
76 if (d->m_roleName.isEmpty() || d->regExp.pattern().isEmpty())
77 return true;
78
79 const int role = itemRole(proxyModel);
80 if (role < 0) {
81 if (!d->m_roleNameValidated) {
82 qWarning("Provided role name %s doesn't exist in the model",
83 d->m_roleName.toUtf8().constData());
84 d->m_roleNameValidated = true;
85 }
86 return false;
87 }
88
89 auto filterData = [&](const QModelIndex &index) -> bool {
90 const QString value = proxyModel->sourceModel()->data(index, role).toString();
91 const QRegularExpressionMatch match =
92 d->regExp.match(value, 0, QRegularExpression::NormalMatch);
93 return match.hasMatch();
94 };
95
96 if (column() > -1) {
97 const QModelIndex index = proxyModel->sourceModel()->index(row, column(), sourceParent);
98 return filterData(index);
99 } else {
100 const int columnCount = proxyModel->sourceModel()->columnCount(sourceParent);
101 for (int column = 0; column < columnCount; ++column) {
102 if (filterData(proxyModel->sourceModel()->index(row, column, sourceParent)))
103 return true;
104 }
105 }
106
107 return false;
108}
109
110QT_END_NAMESPACE
111
112#include "moc_qqmlregexpfilter_p.cpp"