Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
4#include "qcheckbox.h"
5#include "qapplication.h"
6#include "qbitmap.h"
7#include "qicon.h"
8#include "qstylepainter.h"
9#include "qstyle.h"
10#include "qstyleoption.h"
11#include "qevent.h"
12#if QT_CONFIG(accessibility)
13#include "qaccessible.h"
14#endif
15
16#include "private/qabstractbutton_p.h"
17
19
21{
22 Q_DECLARE_PUBLIC(QCheckBox)
23public:
26 hovering(true), publishedState(Qt::Unchecked) {}
27
32
33 void init();
34};
35
95
110{
111 Q_Q(QCheckBox);
112 q->setCheckable(true);
113 q->setMouseTracking(true);
114 q->setForegroundRole(QPalette::WindowText);
115 q->setAttribute(Qt::WA_MacShowFocusRect);
117}
118
127{
128 if (!option)
129 return;
130 Q_D(const QCheckBox);
131 option->initFrom(this);
132 if (d->down)
134 if (d->tristate && d->noChange)
136 else
137 option->state |= d->checked ? QStyle::State_On : QStyle::State_Off;
139 option->state.setFlag(QStyle::State_MouseOver, d->hovering);
140 }
141 option->text = d->text;
142 option->icon = d->icon;
143 option->iconSize = iconSize();
144}
145
154{
155 Q_D(QCheckBox);
156 d->init();
157}
158
167{
168 setText(text);
169}
170
175{
176}
177
179{
180 Q_D(QCheckBox);
181 d->tristate = y;
182}
183
185{
186 Q_D(const QCheckBox);
187 return d->tristate;
188}
189
190
198{
199 Q_D(const QCheckBox);
200 if (d->tristate && d->noChange)
202 return d->checked ? Qt::Checked : Qt::Unchecked;
203}
204
213{
214 Q_D(QCheckBox);
215#if QT_CONFIG(accessibility)
216 bool noChange = d->noChange;
217#endif
219 d->tristate = true;
220 d->noChange = true;
221 } else {
222 d->noChange = false;
223 }
224 d->blockRefresh = true;
226 d->blockRefresh = false;
227 d->refresh();
228 if (state != d->publishedState) {
229 d->publishedState = state;
231 }
232
233#if QT_CONFIG(accessibility)
234 if (noChange != d->noChange) {
236 s.checkStateMixed = true;
237 QAccessibleStateChangeEvent event(this, s);
238 QAccessible::updateAccessibility(&event);
239 }
240#endif
241}
242
243
248{
249 Q_D(const QCheckBox);
250 if (d->sizeHint.isValid())
251 return d->sizeHint;
256 QSize sz = style()->itemTextRect(fm, QRect(), Qt::TextShowMnemonic, false,
257 text()).size();
258 if (!opt.icon.isNull())
259 sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
260 d->sizeHint = style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz, this);
261 return d->sizeHint;
262}
263
264
269{
270 return sizeHint();
271}
272
277{
278 QStylePainter p(this);
281 p.drawControl(QStyle::CE_CheckBox, opt);
282}
283
288{
289 Q_D(QCheckBox);
291 bool hit = false;
292 if (underMouse())
293 hit = hitButton(e->position().toPoint());
294
295 if (hit != d->hovering) {
296 update(rect());
297 d->hovering = hit;
298 }
299 }
300
302}
303
304
309{
313}
314
319{
320 Q_D(QCheckBox);
321 d->noChange = false;
323 if (state != d->publishedState) {
324 d->publishedState = state;
326 }
327}
328
333{
334 Q_D(QCheckBox);
335 if (d->tristate)
337 else {
340 }
341}
342
347{
348 Q_D(QCheckBox);
349 if (e->type() == QEvent::StyleChange
350#ifdef Q_OS_MAC
351 || e->type() == QEvent::MacSizeChange
352#endif
353 )
354 d->setLayoutItemMargins(QStyle::SE_CheckBoxLayoutItem);
356}
357
358
359
361
362#include "moc_qcheckbox.cpp"
The QAbstractButton class is the abstract base class of button widgets, providing functionality commo...
void mouseMoveEvent(QMouseEvent *e) override
\reimp
bool event(QEvent *e) override
\reimp
void setText(const QString &text)
QSize iconSize
the icon size used for this button.
virtual void nextCheckState()
This virtual handler is called when a button is clicked.
QString text
the text shown on the button
\inmodule QtGui
Qt::CheckState publishedState
Definition qcheckbox.cpp:31
The QCheckBox widget provides a checkbox with a text label.
Definition qcheckbox.h:19
void setTristate(bool y=true)
QSize minimumSizeHint() const override
\reimp
~QCheckBox()
Destructor.
QSize sizeHint() const override
\reimp
QCheckBox(QWidget *parent=nullptr)
Constructs a checkbox with the given parent, but with no text.
void setCheckState(Qt::CheckState state)
Sets the checkbox's check state to state.
void mouseMoveEvent(QMouseEvent *) override
\reimp
virtual void initStyleOption(QStyleOptionButton *option) const
Initializes option with the values from this QCheckBox.
void checkStateSet() override
\reimp
Qt::CheckState checkState() const
Returns the checkbox's check state.
void paintEvent(QPaintEvent *) override
\reimp
void nextCheckState() override
\reimp
bool hitButton(const QPoint &pos) const override
\reimp
void stateChanged(Qt::CheckState)
bool isTristate() const
bool event(QEvent *e) override
\reimp
\inmodule QtCore
Definition qcoreevent.h:45
@ StyleChange
Definition qcoreevent.h:136
@ MacSizeChange
Definition qcoreevent.h:217
\reentrant \inmodule QtGui
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition qicon.cpp:973
\inmodule QtGui
Definition qevent.h:195
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:485
@ WindowText
Definition qpalette.h:50
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore\reentrant
Definition qrect.h:30
bool contains(const QRect &r, bool proper=false) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qrect.cpp:851
constexpr QSize size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:241
The QSizePolicy class is a layout attribute describing horizontal and vertical resizing policy.
Definition qsizepolicy.h:18
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:132
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:129
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\variable QStyleOptionHeaderV2::textElideMode
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget.
@ State_MouseOver
Definition qstyle.h:80
@ State_Sunken
Definition qstyle.h:69
@ State_Off
Definition qstyle.h:70
@ State_NoChange
Definition qstyle.h:71
@ State_On
Definition qstyle.h:72
@ CT_CheckBox
Definition qstyle.h:546
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w=nullptr) const =0
Returns the size of the element described by the specified option and type, based on the provided con...
virtual QRect itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const
Returns the area within the given rectangle in which to draw the provided text according to the speci...
Definition qstyle.cpp:510
@ CE_CheckBox
Definition qstyle.h:175
virtual QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget=nullptr) const =0
Returns the sub-area for the given element as described in the provided style option.
@ SE_CheckBoxLayoutItem
Definition qstyle.h:289
@ SE_CheckBoxClickRect
Definition qstyle.h:249
void setLayoutItemMargins(int left, int top, int right, int bottom)
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QPoint pos
the position of the widget within its parent widget
Definition qwidget.h:111
QFontMetrics fontMetrics() const
Returns the font metrics for the widget's current font.
Definition qwidget.h:847
QRect rect
the internal geometry of the widget excluding any window frame
Definition qwidget.h:116
int y
the y coordinate of the widget relative to its parent and including any window frame
Definition qwidget.h:110
void ensurePolished() const
Ensures that the widget and its children have been polished by QStyle (i.e., have a proper font and p...
void update()
Updates the widget unless updates are disabled or the widget is hidden.
QStyle * style() const
Definition qwidget.cpp:2607
bool underMouse() const
Returns true if the widget is under the mouse cursor; otherwise returns false.
Definition qwidget.h:859
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition qwidget.h:910
QString text
double e
QStyleOptionButton opt
else opt state
[0]
Combined button and popup list for selecting options.
CheckState
@ Unchecked
@ Checked
@ PartiallyChecked
@ WA_Hover
Definition qnamespace.h:339
@ WA_MacShowFocusRect
Definition qnamespace.h:358
@ TextShowMnemonic
Definition qnamespace.h:172
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLint y
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLdouble s
[6]
Definition qopenglext.h:235
GLfloat GLfloat p
[1]
GLuint GLenum option
#define emit
unsigned int uint
Definition qtypes.h:29
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent