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
qquickcheckbox.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 CheckBox
15 \inherits AbstractButton
16//! \nativetype QQuickCheckBox
17 \inqmlmodule QtQuick.Controls
18 \since 5.7
19 \ingroup qtquickcontrols-buttons
20 \brief Check button that can be toggled on or off.
21
22 \image qtquickcontrols-checkbox.gif
23 {Checkbox in unchecked, checked, and partially checked states}
24
25 CheckBox presents an option button that can be toggled on (checked) or
26 off (unchecked). Check boxes are typically used to select one or more
27 options from a set of options. For larger sets of options, such as those
28 in a list, consider using \l CheckDelegate instead.
29
30 CheckBox inherits its API from \l AbstractButton. For instance, the
31 state of the checkbox can be set with the \l {AbstractButton::}{checked} property.
32
33 In addition to the checked and unchecked states, there is a third state:
34 partially checked. The partially checked state can be enabled using the
35 \l tristate property. This state indicates that the regular checked/unchecked
36 state can not be determined; generally because of other states that affect
37 the checkbox. This state is useful when several child nodes are selected
38 in a treeview, for example.
39
40 \code
41 ColumnLayout {
42 CheckBox {
43 checked: true
44 text: qsTr("First")
45 }
46 CheckBox {
47 text: qsTr("Second")
48 }
49 CheckBox {
50 checked: true
51 text: qsTr("Third")
52 }
53 }
54 \endcode
55
56 Hierarchical checkbox groups can be managed with a non-exclusive
57 \l ButtonGroup.
58
59 \image qtquickcontrols-checkbox-group.png
60 {Group of checkboxes for multiple selection}
61
62 The following example illustrates how the combined check state of
63 children can be bound to the check state of the parent checkbox:
64
65 \snippet qtquickcontrols-checkbox-group.qml 1
66
67 \sa {Customizing CheckBox}, ButtonGroup, {Button Controls}
68*/
69
71{
72 Q_DECLARE_PUBLIC(QQuickCheckBox)
73
74public:
76
77 bool tristate = false;
80};
81
82QQuickCheckBox::QQuickCheckBox(QQuickItem *parent)
83 : QQuickAbstractButton(*(new QQuickCheckBoxPrivate), parent)
84{
85 setCheckable(true);
86}
87
88/*!
89 \qmlproperty bool QtQuick.Controls::CheckBox::tristate
90
91 This property holds whether the checkbox is a tri-state checkbox.
92
93 In the animation below, the first checkbox is tri-state:
94
95 \image qtquickcontrols-checkbox-tristate.gif
96 {Checkbox cycling through three states}
97
98 The default is \c false, i.e., the checkbox has only two states.
99*/
100bool QQuickCheckBox::isTristate() const
101{
102 Q_D(const QQuickCheckBox);
103 return d->tristate;
104}
105
106void QQuickCheckBox::setTristate(bool tristate)
107{
108 Q_D(QQuickCheckBox);
109 if (d->tristate == tristate)
110 return;
111
112 d->tristate = tristate;
113 emit tristateChanged();
114}
115
116/*!
117 \qmlproperty enumeration QtQuick.Controls::CheckBox::checkState
118
119 This property holds the check state of the checkbox.
120
121 Available states:
122 \value Qt.Unchecked The checkbox is unchecked.
123 \value Qt.PartiallyChecked The checkbox is partially checked. This state is only used when \l tristate is enabled.
124 \value Qt.Checked The checkbox is checked.
125
126 \sa tristate, {AbstractButton::checked}{checked}
127*/
128Qt::CheckState QQuickCheckBox::checkState() const
129{
130 Q_D(const QQuickCheckBox);
131 return d->checkState;
132}
133
134void QQuickCheckBox::setCheckState(Qt::CheckState state)
135{
136 Q_D(QQuickCheckBox);
137 if (d->checkState == state)
138 return;
139
140 bool wasChecked = isChecked();
141 d->checked = state == Qt::Checked;
142 d->checkState = state;
143 emit checkStateChanged();
144 if (d->checked != wasChecked)
145 emit checkedChanged();
146}
147
148QJSValue QQuickCheckBox::getNextCheckState() const
149{
150 Q_D(const QQuickCheckBox);
151 return d->nextCheckState;
152}
153
154void QQuickCheckBox::setNextCheckState(const QJSValue &callback)
155{
156 Q_D(QQuickCheckBox);
157 d->nextCheckState = callback;
158 emit nextCheckStateChanged();
159}
160
161QFont QQuickCheckBox::defaultFont() const
162{
163 return QQuickTheme::font(QQuickTheme::CheckBox);
164}
165
166void QQuickCheckBox::buttonChange(ButtonChange change)
167{
168 if (change == ButtonCheckedChange)
169 setCheckState(isChecked() ? Qt::Checked : Qt::Unchecked);
170 else
171 QQuickAbstractButton::buttonChange(change);
172}
173
174/*!
175 \since QtQuick.Controls 2.4 (Qt 5.11)
176 \qmlproperty function QtQuick.Controls::CheckBox::nextCheckState
177
178 This property holds a callback function that is called to determine
179 the next check state whenever the checkbox is interactively toggled
180 by the user via touch, mouse, or keyboard.
181
182 By default, a normal checkbox cycles between \c Qt.Unchecked and
183 \c Qt.Checked states, and a tri-state checkbox cycles between
184 \c Qt.Unchecked, \c Qt.PartiallyChecked, and \c Qt.Checked states.
185
186 The \c nextCheckState callback function can override the default behavior.
187 The following example implements a tri-state checkbox that can present
188 a partially checked state depending on external conditions, but never
189 cycles to the partially checked state when interactively toggled by
190 the user.
191
192 \code
193 CheckBox {
194 tristate: true
195 checkState: allChildrenChecked ? Qt.Checked :
196 anyChildChecked ? Qt.PartiallyChecked : Qt.Unchecked
197
198 nextCheckState: function() {
199 if (checkState === Qt.Checked)
200 return Qt.Unchecked
201 else
202 return Qt.Checked
203 }
204 }
205 \endcode
206*/
207void QQuickCheckBox::nextCheckState()
208{
209 Q_D(QQuickCheckBox);
210 if (d->nextCheckState.isCallable())
211 setCheckState(static_cast<Qt::CheckState>(d->nextCheckState.call().toInt()));
212 else if (d->tristate)
213 setCheckState(static_cast<Qt::CheckState>((d->checkState + 1) % 3));
214 else
215 QQuickAbstractButton::nextCheckState();
216}
217
218QT_END_NAMESPACE
219
220#include "moc_qquickcheckbox_p.cpp"
Check button that can be toggled on or off.
Qt::CheckState checkState
Combined button and popup list for selecting options.