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 When the \l regExp has an empty pattern, the filter is inactive and
51 all rows pass through.
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.isValid())
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 // If the value is empty and there is a regular expresison to check, filter the row
92 if (value.isEmpty())
93 return false;
94 const QRegularExpressionMatch match =
95 d->regExp.match(value, 0, QRegularExpression::NormalMatch);
96 return match.hasMatch();
97 };
98
99 if (column() > -1) {
100 const QModelIndex index = proxyModel->sourceModel()->index(row, column(), sourceParent);
101 return filterData(index);
102 } else {
103 const int columnCount = proxyModel->sourceModel()->columnCount(sourceParent);
104 for (int column = 0; column < columnCount; ++column) {
105 if (filterData(proxyModel->sourceModel()->index(row, column, sourceParent)))
106 return true;
107 }
108 }
109
110 return false;
111}
112
113QT_END_NAMESPACE
114
115#include "moc_qqmlregexpfilter_p.cpp"