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
qcheckbox.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 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
"qcheckbox.h"
6
#
include
"qapplication.h"
7
#
include
"qbitmap.h"
8
#
include
"qicon.h"
9
#
include
"qstylepainter.h"
10
#
include
"qstyle.h"
11
#
include
"qstyleoption.h"
12
#
include
"qevent.h"
13
#
if
QT_CONFIG
(
accessibility
)
14
#
include
"qaccessible.h"
15
#
endif
16
17
#
include
"private/qabstractbutton_p.h"
18
19
QT_BEGIN_NAMESPACE
20
21
class
QCheckBoxPrivate
:
public
QAbstractButtonPrivate
22
{
23
Q_DECLARE_PUBLIC(QCheckBox)
24
public
:
25
QCheckBoxPrivate
()
26
:
QAbstractButtonPrivate
(
QSizePolicy
::
CheckBox
),
tristate
(
false
),
noChange
(
false
),
27
hovering
(
true
),
publishedState
(
Qt
::
Unchecked
) {}
28
QStyle
::
State
styleButtonState
(
QStyle
::
State
state
)
const
override
;
29
30
uint
tristate
: 1;
31
uint
noChange
: 1;
32
uint
hovering
: 1;
33
Qt
::
CheckState
publishedState
: 3;
34
35
void
init
();
36
};
37
38
QStyle::State
QCheckBoxPrivate
::styleButtonState(QStyle::State state)
const
39
{
40
Q_Q(
const
QCheckBox);
41
state = QAbstractButtonPrivate::styleButtonState(state);
42
if
(tristate && noChange)
43
state |= QStyle::State_NoChange;
44
else
45
state |= (checked ? QStyle::State_On : QStyle::State_Off);
46
if
(q->testAttribute(Qt::WA_Hover) && q->underMouse())
47
state.setFlag(QStyle::State_MouseOver, hovering);
48
return
state;
49
}
50
51
/*!
52
\class QCheckBox
53
\brief The QCheckBox widget provides a checkbox with a text label.
54
55
\ingroup basicwidgets
56
\inmodule QtWidgets
57
58
\image fusion-checkbox.png {Check box for the save option}
59
60
A QCheckBox is an option button that can be switched on (checked) or off
61
(unchecked). Checkboxes are typically used to represent features in an
62
application that can be enabled or disabled without affecting others.
63
Different types of behavior can be implemented. For example, a
64
QButtonGroup can be used to group check buttons logically, allowing
65
exclusive checkboxes. However, QButtonGroup does not provide any visual
66
representation.
67
68
The image below further illustrates the differences between exclusive and
69
non-exclusive checkboxes.
70
71
\table
72
\row \li \inlineimage checkboxes-exclusive.png
73
{Check box group that allows only one option checked}
74
\li \inlineimage checkboxes-non-exclusive.png
75
{Check box group that allows multiple options checked}
76
\endtable
77
78
Whenever a checkbox is checked or cleared, it emits the signal
79
checkStateChanged(). Connect to this signal if you want to trigger an action
80
each time the checkbox changes state. You can use isChecked() to query
81
whether or not a checkbox is checked.
82
83
In addition to the usual checked and unchecked states, QCheckBox optionally
84
provides a third state to indicate "no change". This is useful whenever you
85
need to give the user the option of neither checking nor unchecking a
86
checkbox. If you need this third state, enable it with setTristate(), and
87
use checkState() to query the current toggle state.
88
89
Just like QPushButton, a checkbox displays text, and optionally a small
90
icon. The icon is set with setIcon(). The text can be set in the
91
constructor or with setText(). A shortcut key can be specified by preceding
92
the preferred character with an ampersand. For example:
93
94
\snippet code/src_gui_widgets_qcheckbox.cpp 0
95
96
In this example, the shortcut is \e{Alt+A}. See the \l{QShortcut#mnemonic}
97
{QShortcut} documentation for details. To display an actual ampersand,
98
use '&&'.
99
100
\sa QAbstractButton, QRadioButton
101
*/
102
103
/*!
104
\fn void QCheckBox::stateChanged(int state)
105
106
\deprecated [6.9] Use checkStateChanged(Qt::CheckState) instead.
107
108
This signal is emitted whenever the checkbox's state changes, i.e.,
109
whenever the user checks or unchecks it.
110
111
\a state contains the checkbox's new Qt::CheckState.
112
*/
113
114
/*!
115
\fn void QCheckBox::checkStateChanged(Qt::CheckState state)
116
\since 6.7
117
118
This signal is emitted whenever the checkbox's state changes, i.e.,
119
whenever the user checks or unchecks it.
120
121
\a state contains the checkbox's new Qt::CheckState.
122
*/
123
124
/*!
125
\property QCheckBox::tristate
126
\brief whether the checkbox is a tri-state checkbox
127
128
The default is false, i.e., the checkbox has only two states.
129
*/
130
131
void
QCheckBoxPrivate
::
init
()
132
{
133
Q_Q(QCheckBox);
134
q->setCheckable(
true
);
135
q->setMouseTracking(
true
);
136
q->setForegroundRole(QPalette::WindowText);
137
q->setAttribute(Qt::WA_MacShowFocusRect);
138
setLayoutItemMargins(QStyle::SE_CheckBoxLayoutItem);
139
}
140
141
/*!
142
Initializes \a option with the values from this QCheckBox. This method is
143
useful for subclasses that require a QStyleOptionButton, but do not want
144
to fill in all the information themselves.
145
146
\sa QStyleOption::initFrom()
147
*/
148
void
QCheckBox::initStyleOption(QStyleOptionButton *option)
const
149
{
150
if
(!option)
151
return
;
152
Q_D(
const
QCheckBox);
153
option->initFrom(
this
);
154
option->state = d->styleButtonState(option->state);
155
option->text = d->text;
156
option->icon = d->icon;
157
option->iconSize = iconSize();
158
}
159
160
/*!
161
Constructs a checkbox with the given \a parent, but with no text.
162
163
\a parent is passed on to the QAbstractButton constructor.
164
*/
165
166
QCheckBox::QCheckBox(QWidget *parent)
167
: QAbstractButton (*
new
QCheckBoxPrivate, parent)
168
{
169
Q_D(QCheckBox);
170
d->init();
171
}
172
173
/*!
174
Constructs a checkbox with the given \a parent and \a text.
175
176
\a parent is passed on to the QAbstractButton constructor.
177
*/
178
179
QCheckBox::QCheckBox(
const
QString &text, QWidget *parent)
180
: QCheckBox(parent)
181
{
182
setText(text);
183
}
184
185
/*!
186
Destructor.
187
*/
188
QCheckBox::~QCheckBox()
189
{
190
}
191
192
void
QCheckBox::setTristate(
bool
y)
193
{
194
Q_D(QCheckBox);
195
d->tristate = y;
196
}
197
198
bool
QCheckBox::isTristate()
const
199
{
200
Q_D(
const
QCheckBox);
201
return
d->tristate;
202
}
203
204
205
/*!
206
Returns the checkbox's check state. If you do not need tristate support,
207
you can also use \l QAbstractButton::isChecked(), which returns a boolean.
208
209
\sa setCheckState(), Qt::CheckState
210
*/
211
Qt::CheckState QCheckBox::checkState()
const
212
{
213
Q_D(
const
QCheckBox);
214
if
(d->tristate && d->noChange)
215
return
Qt::PartiallyChecked;
216
return
d->checked ? Qt::Checked : Qt::Unchecked;
217
}
218
219
/*!
220
Sets the checkbox's check state to \a state. If you do not need tristate
221
support, you can also use \l QAbstractButton::setChecked(), which takes a
222
boolean.
223
224
\sa checkState(), Qt::CheckState
225
*/
226
void
QCheckBox::setCheckState(Qt::CheckState state)
227
{
228
Q_D(QCheckBox);
229
#
if
QT_CONFIG
(
accessibility
)
230
bool
noChange = d->noChange;
231
#
endif
232
if
(state == Qt::PartiallyChecked) {
233
d->tristate =
true
;
234
d->noChange =
true
;
235
}
else
{
236
d->noChange =
false
;
237
}
238
d->blockRefresh =
true
;
239
setChecked(state != Qt::Unchecked);
240
d->blockRefresh =
false
;
241
d->refresh();
242
if
(state != d->publishedState) {
243
d->publishedState = state;
244
emit checkStateChanged(state);
245
#
if
QT_DEPRECATED_SINCE
(
6
,
9
)
246
QT_IGNORE_DEPRECATIONS(
247
emit stateChanged(state);
248
)
249
#
endif
250
}
251
252
#
if
QT_CONFIG
(
accessibility
)
253
if
(noChange != d->noChange) {
254
QAccessible::State s;
255
s.checkStateMixed =
true
;
256
QAccessibleStateChangeEvent event(
this
, s);
257
QAccessible::updateAccessibility(&event);
258
}
259
#
endif
260
}
261
262
263
/*!
264
\reimp
265
*/
266
QSize QCheckBox::sizeHint()
const
267
{
268
Q_D(
const
QCheckBox);
269
if
(d->sizeHint.isValid())
270
return
d->sizeHint;
271
ensurePolished();
272
QFontMetrics fm = fontMetrics();
273
QStyleOptionButton opt;
274
initStyleOption(&opt);
275
QSize sz = style()->itemTextRect(fm, QRect(), Qt::TextShowMnemonic,
false
,
276
text()).size();
277
if
(!opt.icon.isNull())
278
sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
279
d->sizeHint = style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz,
this
);
280
return
d->sizeHint;
281
}
282
283
284
/*!
285
\reimp
286
*/
287
QSize QCheckBox::minimumSizeHint()
const
288
{
289
return
sizeHint();
290
}
291
292
/*!
293
\reimp
294
*/
295
void
QCheckBox::paintEvent(QPaintEvent *)
296
{
297
QStylePainter p(
this
);
298
QStyleOptionButton opt;
299
initStyleOption(&opt);
300
p.drawControl(QStyle::CE_CheckBox, opt);
301
}
302
303
/*!
304
\reimp
305
*/
306
void
QCheckBox::mouseMoveEvent(QMouseEvent *e)
307
{
308
Q_D(QCheckBox);
309
if
(testAttribute(Qt::WA_Hover)) {
310
bool
hit =
false
;
311
if
(underMouse())
312
hit = hitButton(e->position().toPoint());
313
314
if
(hit != d->hovering) {
315
update(rect());
316
d->hovering = hit;
317
}
318
}
319
320
QAbstractButton::mouseMoveEvent(e);
321
}
322
323
324
/*!
325
\reimp
326
*/
327
bool
QCheckBox::hitButton(
const
QPoint &pos)
const
328
{
329
QStyleOptionButton opt;
330
initStyleOption(&opt);
331
return
style()->subElementRect(QStyle::SE_CheckBoxClickRect, &opt,
this
).contains(pos);
332
}
333
334
/*!
335
\reimp
336
*/
337
void
QCheckBox::checkStateSet()
338
{
339
Q_D(QCheckBox);
340
d->noChange =
false
;
341
Qt::CheckState state = checkState();
342
if
(state != d->publishedState) {
343
d->publishedState = state;
344
emit checkStateChanged(state);
345
#
if
QT_DEPRECATED_SINCE
(
6
,
9
)
346
QT_IGNORE_DEPRECATIONS(
347
emit stateChanged(state);
348
)
349
#
endif
350
}
351
}
352
353
/*!
354
\reimp
355
*/
356
void
QCheckBox::nextCheckState()
357
{
358
Q_D(QCheckBox);
359
if
(d->tristate)
360
setCheckState((Qt::CheckState)((checkState() + 1) % 3));
361
else
{
362
QAbstractButton::nextCheckState();
363
QCheckBox::checkStateSet();
364
}
365
}
366
367
/*!
368
\reimp
369
*/
370
bool
QCheckBox::event(QEvent *e)
371
{
372
Q_D(QCheckBox);
373
if
(e->type() == QEvent::StyleChange
374
#
ifdef
Q_OS_MACOS
375
|| e->type() == QEvent::MacSizeChange
376
#
endif
377
)
378
d->setLayoutItemMargins(QStyle::SE_CheckBoxLayoutItem);
379
return
QAbstractButton::event(e);
380
}
381
382
383
384
QT_END_NAMESPACE
385
386
#
include
"moc_qcheckbox.cpp"
QCheckBoxPrivate
Definition
qcheckbox.cpp:22
QCheckBoxPrivate::noChange
uint noChange
Definition
qcheckbox.cpp:31
QCheckBoxPrivate::init
void init()
Definition
qcheckbox.cpp:131
QCheckBoxPrivate::publishedState
Qt::CheckState publishedState
Definition
qcheckbox.cpp:33
QCheckBoxPrivate::tristate
uint tristate
Definition
qcheckbox.cpp:30
QCheckBoxPrivate::hovering
uint hovering
Definition
qcheckbox.cpp:32
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtbase
src
widgets
widgets
qcheckbox.cpp
Generated on
for Qt by
1.18.0