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