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 #ifdef QT_KEYPAD_NAVIGATION
76 if (widget->hasEditFocus())
77 state |= QStyle::State_HasEditFocus;
78 #endif
79
80 direction = widget->layoutDirection();
81 rect = widget->rect();
82 palette = widget->palette();
83 fontMetrics = widget->fontMetrics();
84 //! [1]
85 }
86
87 {
88 QStyle *d;
89 //! [2]
90 QStylePainter p;
91 QStyleOptionButton opt;
92 initStyleOption(&opt);
93 p.drawControl(QStyle::CE_CheckBox, opt);
94 //! [2]
95 }
96}
97
99{
100 {
101 QPainter *p;
102 QStyleOptionButton *btn;
103 QWidget *widget;
104 bool State_HasFocus;
105
106 //! [3]
107 QStyleOptionButton subopt = *btn;
108 subopt.rect = subElementRect(QStyle::SE_CheckBoxIndicator, btn, widget);
109 drawPrimitive(QStyle::PE_IndicatorCheckBox, &subopt, p, widget);
110 subopt.rect = subElementRect(QStyle::SE_CheckBoxContents, btn, widget);
111 drawControl(QStyle::CE_CheckBoxLabel, &subopt, p, widget);
112 if (btn->state & State_HasFocus) {
113 QStyleOptionFocusRect fropt;
114 fropt.QStyleOption::operator=(*btn);
115 fropt.rect = subElementRect(QStyle::SE_CheckBoxFocusRect, btn, widget);
116 drawPrimitive(QStyle::PE_FrameFocusRect, &fropt, p, widget);
117 }
118 //! [3]
119 }
120
121 {
122 QStyleOptionButton *opt;
123 QWidget *widget;
124 QPainter *p;
125
126 //! [4]
127 const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt);
128 uint alignment = visualAlignment(btn->direction, Qt::AlignLeft | Qt::AlignVCenter);
129 if (!styleHint(SH_UnderlineShortcut, btn, widget))
130 alignment |= Qt::TextHideMnemonic;
131 QPixmap pix;
132 QRect textRect = btn->rect;
133 if (!btn->icon.isNull()) {
134 const auto dpr = p->device()->devicePixelRatio();
135 pix = btn->icon.pixmap(btn->iconSize, dpr,
136 btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled);
137 drawItemPixmap(p, btn->rect, alignment, pix);
138 if (btn->direction == Qt::RightToLeft)
139 textRect.setRight(textRect.right() - btn->iconSize.width() - 4);
140 else
141 textRect.setLeft(textRect.left() + btn->iconSize.width() + 4);
142 }
143 if (!btn->text.isEmpty()){
144 drawItemText(p, textRect, alignment | Qt::TextShowMnemonic,
145 btn->palette, btn->state & State_Enabled, btn->text, QPalette::WindowText);
146 }
147 //! [4]
148 }
149}
void examples()