Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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:
27
32
33 void init();
34};
35
121{
122 Q_Q(QCheckBox);
123 q->setCheckable(true);
124 q->setMouseTracking(true);
125 q->setForegroundRole(QPalette::WindowText);
126 q->setAttribute(Qt::WA_MacShowFocusRect);
128}
129
138{
139 if (!option)
140 return;
141 Q_D(const QCheckBox);
142 option->initFrom(this);
143 if (d->down)
145 if (d->tristate && d->noChange)
147 else
148 option->state |= d->checked ? QStyle::State_On : QStyle::State_Off;
150 option->state.setFlag(QStyle::State_MouseOver, d->hovering);
151 }
152 option->text = d->text;
153 option->icon = d->icon;
154 option->iconSize = iconSize();
155}
156
164 : QAbstractButton (*new QCheckBoxPrivate, parent)
165{
166 Q_D(QCheckBox);
167 d->init();
168}
169
177 : QCheckBox(parent)
178{
179 setText(text);
180}
181
188
190{
191 Q_D(QCheckBox);
192 d->tristate = y;
193}
194
196{
197 Q_D(const QCheckBox);
198 return d->tristate;
199}
200
201
209{
210 Q_D(const QCheckBox);
211 if (d->tristate && d->noChange)
213 return d->checked ? Qt::Checked : Qt::Unchecked;
214}
215
224{
225 Q_D(QCheckBox);
226#if QT_CONFIG(accessibility)
227 bool noChange = d->noChange;
228#endif
230 d->tristate = true;
231 d->noChange = true;
232 } else {
233 d->noChange = false;
234 }
235 d->blockRefresh = true;
237 d->blockRefresh = false;
238 d->refresh();
239 if (state != d->publishedState) {
240 d->publishedState = state;
242#if QT_DEPRECATED_SINCE(6, 9)
244 emit stateChanged(state);
245 )
246#endif
247 }
248
249#if QT_CONFIG(accessibility)
250 if (noChange != d->noChange) {
252 s.checkStateMixed = true;
253 QAccessibleStateChangeEvent event(this, s);
254 QAccessible::updateAccessibility(&event);
255 }
256#endif
257}
258
259
264{
265 Q_D(const QCheckBox);
266 if (d->sizeHint.isValid())
267 return d->sizeHint;
272 QSize sz = style()->itemTextRect(fm, QRect(), Qt::TextShowMnemonic, false,
273 text()).size();
274 if (!opt.icon.isNull())
275 sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
276 d->sizeHint = style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz, this);
277 return d->sizeHint;
278}
279
280
285{
286 return sizeHint();
287}
288
299
304{
305 Q_D(QCheckBox);
307 bool hit = false;
308 if (underMouse())
309 hit = hitButton(e->position().toPoint());
310
311 if (hit != d->hovering) {
312 update(rect());
313 d->hovering = hit;
314 }
315 }
316
318}
319
320
325{
328 return style()->subElementRect(QStyle::SE_CheckBoxClickRect, &opt, this).contains(pos);
329}
330
335{
336 Q_D(QCheckBox);
337 d->noChange = false;
339 if (state != d->publishedState) {
340 d->publishedState = state;
342#if QT_DEPRECATED_SINCE(6, 9)
344 emit stateChanged(state);
345 )
346#endif
347 }
348}
349
354{
355 Q_D(QCheckBox);
356 if (d->tristate)
358 else {
361 }
362}
363
368{
369 Q_D(QCheckBox);
370 if (e->type() == QEvent::StyleChange
371#ifdef Q_OS_MAC
372 || e->type() == QEvent::MacSizeChange
373#endif
374 )
375 d->setLayoutItemMargins(QStyle::SE_CheckBoxLayoutItem);
376 return QAbstractButton::event(e);
377}
378
379
380
382
383#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 checkStateChanged(Qt::CheckState)
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
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
Type type() const
Returns the event type.
Definition qcoreevent.h:304
\reentrant \inmodule QtGui
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition qicon.cpp:1019
\inmodule QtGui
Definition qevent.h:196
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
@ WindowText
Definition qpalette.h:51
constexpr QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition qpoint.h:404
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:30
QPointF position() const
Returns the position of the point in this event, relative to the widget or item that received the eve...
Definition qevent.h:119
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:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\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:548
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:2600
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
QStyleOptionButton opt
else opt state
[0]
Combined button and popup list for selecting options.
Definition qcompare.h:63
CheckState
@ Unchecked
@ Checked
@ PartiallyChecked
@ WA_Hover
Definition qnamespace.h:340
@ WA_MacShowFocusRect
Definition qnamespace.h:359
@ TextShowMnemonic
Definition qnamespace.h:173
#define QT_IGNORE_DEPRECATIONS(statement)
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLint y
struct _cl_event * event
GLdouble s
[6]
Definition qopenglext.h:235
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLfloat GLfloat p
[1]
GLuint GLenum option
#define emit
unsigned int uint
Definition qtypes.h:34