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