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