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
qqmlvaluefilter.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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 <QtQmlModels/private/qqmlvaluefilter_p.h>
6#include <QtQmlModels/private/qqmlsortfilterproxymodel_p.h>
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 \qmltype ValueFilter
12 \inherits RoleFilter
13 \inqmlmodule QtQml.Models
14 \since 6.10
15 \preliminary
16 \brief Filters data in a \l SortFilterProxyModel based on role name and
17 value.
18
19 ValueFilter allows the user to filter the data according to the role name
20 or specified value or both as configured in the source model. The role name
21 used to filter the data shall be based on model
22 \l{QAbstractItemModel::roleNames()}{role name}. The default value for
23 role name is \c "display".
24
25 The following snippet shows how ValueFilter can be used to only include
26 data from the source model where the value of the role name \c "favorite"
27 is \c "true":
28
29 \qml
30 SortFilterProxyModel {
31 model: sourceModel
32 filters: [
33 ValueFilter {
34 roleName: "favorite"
35 value: true
36 }
37 ]
38 }
39 \endqml
40*/
41
42QQmlValueFilter::QQmlValueFilter(QObject *parent) :
43 QQmlRoleFilter (new QQmlValueFilterPrivate, parent)
44{
45
46}
47
48/*!
49 \qmlproperty variant ValueFilter::value
50
51 This property holds the specific value that can be used to filter the data.
52*/
53const QVariant& QQmlValueFilter::value() const
54{
55 Q_D(const QQmlValueFilter);
56 return d->m_value;
57}
58
59void QQmlValueFilter::setValue(const QVariant& value)
60{
61 Q_D(QQmlValueFilter);
62 if (d->m_value == value)
63 return;
64 d->m_value = value;
65 // Update the model for the change in the role name
66 emit valueChanged();
67 // Invalidate the model for the change in the role name
68 invalidate();
69}
70
71void QQmlValueFilter::resetValue()
72{
73 Q_D(QQmlValueFilter);
74 d->m_value = QVariant();
75}
76
77/*!
78 \internal
79*/
80bool QQmlValueFilter::filterAcceptsRowInternal(int row, const QModelIndex& sourceParent, const QQmlSortFilterProxyModel *proxyModel) const
81{
82 Q_D(const QQmlValueFilter);
83 if (d->m_roleName.isEmpty())
84 return true;
85 int role = itemRole(proxyModel);
86 const bool isValidVal = (!d->m_value.isValid() || !d->m_value.isNull());
87 if (role > -1) {
88 if (column() > -1) {
89 const QModelIndex &index = proxyModel->sourceModel()->index(row, column(), sourceParent);
90 const QVariant &value = proxyModel->sourceModel()->data(index, role);
91 return (value.isValid() && (!isValidVal || d->m_value == value));
92 } else {
93 const int columnCount = proxyModel->sourceModel()->columnCount(sourceParent);
94 for (int column = 0; column < columnCount; column++) {
95 const QModelIndex &index = proxyModel->sourceModel()->index(row, column, sourceParent);
96 const QVariant &value = proxyModel->sourceModel()->data(index, role);
97 if (value.isValid() && (!isValidVal || d->m_value == value))
98 return true;
99 }
100 }
101 }
102 return false;
103}
104
105QT_END_NAMESPACE
106
107#include "moc_qqmlvaluefilter_p.cpp"