Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
8
QT_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
sourceModel: model
32
filters: [
33
ValueFilter {
34
roleName: "favorite"
35
value: true
36
}
37
]
38
}
39
\endqml
40
*/
41
42
QQmlValueFilter::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
*/
53
const
QVariant& QQmlValueFilter::value()
const
54
{
55
Q_D(
const
QQmlValueFilter);
56
return
d->m_value;
57
}
58
59
void
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
71
void
QQmlValueFilter::resetValue()
72
{
73
Q_D(QQmlValueFilter);
74
d->m_value = QVariant();
75
}
76
77
/*!
78
\internal
79
*/
80
bool
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
if
(role < 0) {
87
if
(!d->m_roleName.isEmpty() && !d->m_roleNameValidated) {
88
qWarning(
"Provided role name %s doesn't exist in the model"
, d->m_roleName.toUtf8().constData());
89
d->m_roleNameValidated =
true
;
90
}
91
return
false
;
92
}
93
const
bool
isValidVal = (!d->m_value.isValid() || !d->m_value.isNull());
94
if
(column() > -1) {
95
const
QModelIndex &index = proxyModel->sourceModel()->index(row, column(), sourceParent);
96
const
QVariant &value = proxyModel->sourceModel()->data(index, role);
97
return
(value.isValid() && (!isValidVal || d->m_value == value));
98
}
else
{
99
const
int
columnCount = proxyModel->sourceModel()->columnCount(sourceParent);
100
for
(
int
column = 0; column < columnCount; column++) {
101
const
QModelIndex &index = proxyModel->sourceModel()->index(row, column, sourceParent);
102
const
QVariant &value = proxyModel->sourceModel()->data(index, role);
103
if
(value.isValid() && (!isValidVal || d->m_value == value))
104
return
true
;
105
}
106
}
107
return
false
;
108
}
109
110
QT_END_NAMESPACE
111
112
#
include
"moc_qqmlvaluefilter_p.cpp"
qtdeclarative
src
qmlmodels
sfpm
filters
qqmlvaluefilter.cpp
Generated on
for Qt by
1.18.0