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
qquickcheckdelegate.cpp
Go to the documentation of this file.
1
// Copyright (C) 2017 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
"qquickcheckdelegate_p.h"
6
#
include
"qquickitemdelegate_p_p.h"
7
8
#
include
<
QtGui
/
qpa
/
qplatformtheme
.
h
>
9
#
include
<
QtQml
/
qjsvalue
.
h
>
10
11
QT_BEGIN_NAMESPACE
12
13
/*!
14
\qmltype CheckDelegate
15
\inherits ItemDelegate
16
//! \nativetype QQuickCheckDelegate
17
\inqmlmodule QtQuick.Controls
18
\since 5.7
19
\ingroup qtquickcontrols-delegates
20
\brief Item delegate with a check indicator that can be toggled on or off.
21
22
\image qtquickcontrols-checkdelegate.gif
23
{Check delegate in list showing checked and unchecked states}
24
25
CheckDelegate presents an item delegate that can be toggled on (checked) or
26
off (unchecked). Check delegates are typically used to select one or more
27
options from a set of options in a list. For smaller sets of options, or
28
for options that need to be uniquely identifiable, consider using
29
\l CheckBox instead.
30
31
CheckDelegate inherits its API from \l ItemDelegate, which is inherited
32
from AbstractButton. For instance, you can set \l {AbstractButton::text}{text},
33
and react to \l {AbstractButton::clicked}{clicks} using the AbstractButton
34
API. The state of the check delegate can be set with the
35
\l {AbstractButton::}{checked} property.
36
37
In addition to the checked and unchecked states, there is a third state:
38
partially checked. The partially checked state can be enabled using the
39
\l tristate property. This state indicates that the regular checked/unchecked
40
state can not be determined; generally because of other states that affect
41
the check delegate. This state is useful when several child nodes are selected
42
in a treeview, for example.
43
44
\code
45
ListView {
46
model: ["Option 1", "Option 2", "Option 3"]
47
delegate: CheckDelegate {
48
text: modelData
49
}
50
}
51
\endcode
52
53
\sa {Customizing CheckDelegate}, {Delegate Controls}, CheckBox
54
*/
55
56
class
QQuickCheckDelegatePrivate
:
public
QQuickItemDelegatePrivate
57
{
58
Q_DECLARE_PUBLIC(QQuickCheckDelegate)
59
60
public
:
61
void
setNextCheckState
(
const
QJSValue
&
callback
);
62
63
QPalette
defaultPalette
()
const
override
{
return
QQuickTheme::palette(QQuickTheme::ListView); }
64
65
bool
tristate
=
false
;
66
Qt
::
CheckState
checkState
=
Qt
::
Unchecked
;
67
QJSValue
nextCheckState
;
68
};
69
70
void
QQuickCheckDelegatePrivate
::setNextCheckState(
const
QJSValue &callback)
71
{
72
Q_Q(QQuickCheckDelegate);
73
nextCheckState = callback;
74
emit q->nextCheckStateChanged();
75
}
76
77
QQuickCheckDelegate::QQuickCheckDelegate(QQuickItem *parent)
78
: QQuickItemDelegate(*(
new
QQuickCheckDelegatePrivate), parent)
79
{
80
setCheckable(
true
);
81
}
82
83
/*!
84
\qmlproperty bool QtQuick.Controls::CheckDelegate::tristate
85
86
This property determines whether the check delegate has three states.
87
88
In the animation below, the first checkdelegate is tri-state:
89
90
\image qtquickcontrols-checkdelegate-tristate.gif
91
{Check delegate cycling through three states}
92
93
The default is \c false, i.e., the delegate has only two states.
94
*/
95
bool
QQuickCheckDelegate::isTristate()
const
96
{
97
Q_D(
const
QQuickCheckDelegate);
98
return
d->tristate;
99
}
100
101
void
QQuickCheckDelegate::setTristate(
bool
tristate)
102
{
103
Q_D(QQuickCheckDelegate);
104
if
(d->tristate == tristate)
105
return
;
106
107
d->tristate = tristate;
108
emit tristateChanged();
109
}
110
111
/*!
112
\qmlproperty enumeration QtQuick.Controls::CheckDelegate::checkState
113
114
This property determines the check state of the check delegate.
115
116
Available states:
117
\value Qt.Unchecked The delegate is unchecked.
118
\value Qt.PartiallyChecked The delegate is partially checked. This state is only used when \l tristate is enabled.
119
\value Qt.Checked The delegate is checked.
120
121
\sa tristate, {AbstractButton::checked}{checked}
122
*/
123
Qt::CheckState QQuickCheckDelegate::checkState()
const
124
{
125
Q_D(
const
QQuickCheckDelegate);
126
return
d->checkState;
127
}
128
129
void
QQuickCheckDelegate::setCheckState(Qt::CheckState state)
130
{
131
Q_D(QQuickCheckDelegate);
132
if
(d->checkState == state)
133
return
;
134
135
bool
wasChecked = isChecked();
136
d->checked = state == Qt::Checked;
137
d->checkState = state;
138
emit checkStateChanged();
139
if
(d->checked != wasChecked)
140
emit checkedChanged();
141
}
142
143
QFont QQuickCheckDelegate::defaultFont()
const
144
{
145
return
QQuickTheme::font(QQuickTheme::ListView);
146
}
147
148
void
QQuickCheckDelegate::buttonChange(ButtonChange change)
149
{
150
if
(change == ButtonCheckedChange)
151
setCheckState(isChecked() ? Qt::Checked : Qt::Unchecked);
152
else
153
QQuickAbstractButton::buttonChange(change);
154
}
155
156
/*!
157
\since QtQuick.Controls 2.4 (Qt 5.11)
158
\qmlproperty function QtQuick.Controls::CheckDelegate::nextCheckState
159
160
This property holds a callback function that is called to determine
161
the next check state whenever the check delegate is interactively toggled
162
by the user via touch, mouse, or keyboard.
163
164
By default, a normal check delegate cycles between \c Qt.Unchecked and
165
\c Qt.Checked states, and a tri-state check delegate cycles between
166
\c Qt.Unchecked, \c Qt.PartiallyChecked, and \c Qt.Checked states.
167
168
The \c nextCheckState callback function can override the default behavior.
169
The following example implements a tri-state check delegate that can present
170
a partially checked state depending on external conditions, but never
171
cycles to the partially checked state when interactively toggled by
172
the user.
173
174
\code
175
CheckDelegate {
176
tristate: true
177
checkState: allChildrenChecked ? Qt.Checked :
178
anyChildChecked ? Qt.PartiallyChecked : Qt.Unchecked
179
180
nextCheckState: function() {
181
if (checkState === Qt.Checked)
182
return Qt.Unchecked
183
else
184
return Qt.Checked
185
}
186
}
187
\endcode
188
*/
189
void
QQuickCheckDelegate::nextCheckState()
190
{
191
Q_D(QQuickCheckDelegate);
192
if
(d->nextCheckState.isCallable())
193
setCheckState(
static_cast
<Qt::CheckState>(d->nextCheckState.call().toInt()));
194
else
if
(d->tristate)
195
setCheckState(
static_cast
<Qt::CheckState>((d->checkState + 1) % 3));
196
else
197
QQuickItemDelegate::nextCheckState();
198
}
199
200
#
if
QT_CONFIG
(
accessibility
)
201
QAccessible::Role QQuickCheckDelegate::accessibleRole()
const
202
{
203
return
QAccessible::CheckBox;
204
}
205
#
endif
206
207
QT_END_NAMESPACE
208
209
#
include
"moc_qquickcheckdelegate_p.cpp"
QQuickCheckDelegatePrivate
Item delegate with a check indicator that can be toggled on or off.
Definition
qquickcheckdelegate.cpp:57
QQuickCheckDelegatePrivate::checkState
Qt::CheckState checkState
Definition
qquickcheckdelegate.cpp:66
QQuickCheckDelegatePrivate::nextCheckState
QJSValue nextCheckState
Definition
qquickcheckdelegate.cpp:67
QQuickCheckDelegatePrivate::defaultPalette
QPalette defaultPalette() const override
Definition
qquickcheckdelegate.cpp:63
QQuickCheckDelegatePrivate::tristate
bool tristate
Definition
qquickcheckdelegate.cpp:65
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtdeclarative
src
quicktemplates
qquickcheckdelegate.cpp
Generated on
for Qt by
1.18.0