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
doc_src_styles.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QStyle>
5#include <QWidget>
6#include <QStyleOptionButton>
7#include <QStylePainter>
8#include <QRect>
9#include <QPalette>
10#include <QFontMetrics>
11#include <QCheckBox>
12#include <QCommonStyle>
13
14class CheckBox : public QCheckBox
15{
16public:
17 void examples();
18};
19
21{
22public:
23 void examples();
24};
25
27{
28 {
29 QWidget *q;
30 QStyleOptionButton opt;
31 bool down, tristate, noChange, checked, hovering;
32 const QString text;
33 const QIcon icon;
34
35 //! [0]
36 opt.initFrom(q);
37 if (down)
38 opt.state |= QStyle::State_Sunken;
39 if (tristate && noChange)
40 opt.state |= QStyle::State_NoChange;
41 else
42 opt.state |= checked ? QStyle::State_On : QStyle::State_Off;
43 if (q->testAttribute(Qt::WA_Hover) && q->underMouse()) {
44 if (hovering)
45 opt.state |= QStyle::State_MouseOver;
46 else
47 opt.state &= ~QStyle::State_MouseOver;
48 }
49 opt.text = text;
50 opt.icon = icon;
51 opt.iconSize = q->size();
52 //! [0]
53 }
54
55 {
56 QStyle::State state;
57 QWidget *widget;
58 Qt::LayoutDirection direction;
59 QRect rect;
60 QPalette palette;
61 QFontMetrics fontMetrics = widget->fontMetrics();
62
63 //! [1]
64 state = QStyle::State_None;
65 if (widget->isEnabled())
66 state |= QStyle::State_Enabled;
67 if (widget->hasFocus())
68 state |= QStyle::State_HasFocus;
69 if (widget->window()->testAttribute(Qt::WA_KeyboardFocusChange))
70 state |= QStyle::State_KeyboardFocusChange;
71 if (widget->underMouse())
72 state |= QStyle::State_MouseOver;
73 if (widget->window()->isActiveWindow())
74 state |= QStyle::State_Active;
75
76 direction = widget->layoutDirection();
77 rect = widget->rect();
78 palette = widget->palette();
79 fontMetrics = widget->fontMetrics();
80 //! [1]
81 }
82
83 {
84 QStyle *d;
85 //! [2]
86 QStylePainter p;
87 QStyleOptionButton opt;
88 initStyleOption(&opt);
89 p.drawControl(QStyle::CE_CheckBox, opt);
90 //! [2]
91 }
92}
93
95{
96 {
97 QPainter *p;
98 QStyleOptionButton *btn;
99 QWidget *widget;
100 bool State_HasFocus;
101
102 //! [3]
103 QStyleOptionButton subopt = *btn;
104 subopt.rect = subElementRect(QStyle::SE_CheckBoxIndicator, btn, widget);
105 drawPrimitive(QStyle::PE_IndicatorCheckBox, &subopt, p, widget);
106 subopt.rect = subElementRect(QStyle::SE_CheckBoxContents, btn, widget);
107 drawControl(QStyle::CE_CheckBoxLabel, &subopt, p, widget);
108 if (btn->state & State_HasFocus) {
109 QStyleOptionFocusRect fropt;
110 fropt.QStyleOption::operator=(*btn);
111 fropt.rect = subElementRect(QStyle::SE_CheckBoxFocusRect, btn, widget);
112 drawPrimitive(QStyle::PE_FrameFocusRect, &fropt, p, widget);
113 }
114 //! [3]
115 }
116
117 {
118 QStyleOptionButton *opt;
119 QWidget *widget;
120 QPainter *p;
121
122 //! [4]
123 const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt);
124 uint alignment = visualAlignment(btn->direction, Qt::AlignLeft | Qt::AlignVCenter);
125 if (!styleHint(SH_UnderlineShortcut, btn, widget))
126 alignment |= Qt::TextHideMnemonic;
127 QPixmap pix;
128 QRect textRect = btn->rect;
129 if (!btn->icon.isNull()) {
130 const auto dpr = p->device()->devicePixelRatio();
131 pix = btn->icon.pixmap(btn->iconSize, dpr,
132 btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled);
133 drawItemPixmap(p, btn->rect, alignment, pix);
134 if (btn->direction == Qt::RightToLeft)
135 textRect.setRight(textRect.right() - btn->iconSize.width() - 4);
136 else
137 textRect.setLeft(textRect.left() + btn->iconSize.width() + 4);
138 }
139 if (!btn->text.isEmpty()){
140 drawItemText(p, textRect, alignment | Qt::TextShowMnemonic,
141 btn->palette, btn->state & State_Enabled, btn->text, QPalette::WindowText);
142 }
143 //! [4]
144 }
145}
void examples()