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
checkboxstyle.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtWidgets>
5
6//! [0]
8{
9public:
11
12 void drawPrimitive(PrimitiveElement element, const QStyleOption *option,
13 QPainter *painter, const QWidget *widget) const override;
14};
15//! [0]
16
17//! [1]
18void CheckBoxStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option,
19 QPainter *painter, const QWidget *widget) const
20{
21 if (element != PE_IndicatorCheckBox) {
22 QProxyStyle::drawPrimitive(element, option, painter, widget);
23 return;
24 }
25
26 const bool enabled = option->state & State_Enabled;
27 const QPalette::ColorGroup group = !enabled ? QPalette::Disabled
28 : option->state & State_Active ? QPalette::Active
29 : QPalette::Inactive;
30 const QPalette &palette = option->palette;
31 const QRect rect = option->rect.adjusted(1, 1, -1, -1);
32
33 painter->save();
34 painter->setRenderHint(QPainter::Antialiasing);
35
36 // Frame and background: highlight the frame while the mouse hovers over
37 // the indicator, and darken the background while it's pressed.
38 QColor frameColor = palette.color(group, QPalette::Mid);
39 if (enabled && (option->state & State_MouseOver))
40 frameColor = palette.color(group, QPalette::Highlight);
41 const QPalette::ColorRole fillRole =
42 option->state & State_Sunken ? QPalette::Mid : QPalette::Base;
43 painter->setPen(frameColor);
44 painter->setBrush(palette.brush(group, fillRole));
45 painter->drawRoundedRect(rect, 2, 2);
46
47 // Check mark for State_On, a filled square for the partially checked
48 // State_NoChange, nothing for State_Off.
49 const QRect inner = rect.adjusted(3, 3, -3, -3);
50 if (option->state & State_On) {
51 painter->setPen(QPen(palette.color(group, QPalette::Text), 2));
52 painter->drawLine(inner.left(), inner.center().y(),
53 inner.center().x(), inner.bottom());
54 painter->drawLine(inner.center().x(), inner.bottom(),
55 inner.right(), inner.top());
56 } else if (option->state & State_NoChange) {
57 painter->fillRect(inner, palette.brush(group, QPalette::Text));
58 }
59
60 painter->restore();
61}
62//! [1]
63
64//! [2]
65int main(int argc, char *argv[])
66{
67 QApplication::setStyle(new CheckBoxStyle);
68 QApplication app(argc, argv);
69 QCheckBox box("Send me updates");
70 box.show();
71 return app.exec();
72}
73//! [2]
void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const override
[0]
int main(int argc, char *argv[])
[ctor_close]