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
qqstylekittheme.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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 <QtQuickTemplates2/private/qquicktheme_p.h>
5#include <QtQuick/private/qquickpalette_p.h>
6#include <QtGui/private/qguiapplication_p.h>
7#include <QtGui/qpa/qplatformtheme.h>
8
11
13
14/*!
15 \qmltype Theme
16 \inqmlmodule Qt.labs.StyleKit
17 \inherits AbstractStyle
18 \brief Defines color and style overrides for a color scheme.
19
20 A Theme overrides properties defined in a \l Style to provide a
21 distinct visual appearance for a particular color scheme. A style
22 typically configures structural properties such as implicit size,
23 padding, and radii, while a theme specifies colors, shadows, and
24 other visual attributes. However, this is not a restriction — a
25 theme can override any property that a style can set.
26
27 A style can provide a \l {Style::light}{light} and a
28 \l {Style::dark}{dark} theme. When \l {Style::themeName}{themeName}
29 is set to \c "System" (the default), the active theme follows the
30 \l {QStyleHints::colorScheme}{operating system's color scheme}.
31 You can also create additional themes using \l CustomTheme and
32 \l {Style::themeName}{switch between them} at runtime.
33
34 Properties that are not set in the theme fall back to those defined
35 in the \l Style.
36
37 The following example defines light, dark, and high-contrast
38 themes, each providing different colors for the controls:
39
40 \snippet ThemeSnippets.qml themes
41
42 For a complete example showing themes in action, see the
43 \l {StyleKit Quick Controls Example}.
44
45 \labs
46
47 \sa Style, CustomTheme, {StyleKit Property Resolution}
48*/
49
50static QQuickTheme::Scope scopeForType(QQStyleKitExtendableControlType type)
51{
52 switch (type) {
53 case QQStyleKitReader::ControlType::AbstractButton:
54 case QQStyleKitReader::ControlType::Button:
55 case QQStyleKitReader::ControlType::DelayButton:
56 case QQStyleKitReader::ControlType::FlatButton:
57 case QQStyleKitReader::ControlType::RoundButton:
58 return QQuickTheme::Button;
59 case QQStyleKitReader::ControlType::CheckBox:
60 return QQuickTheme::CheckBox;
61 case QQStyleKitReader::ControlType::CheckDelegate:
62 case QQStyleKitReader::ControlType::RadioDelegate:
63 case QQStyleKitReader::ControlType::SwipeDelegate:
64 case QQStyleKitReader::ControlType::SwitchDelegate:
65 return QQuickTheme::ListView;
66 case QQStyleKitReader::ControlType::ComboBox:
67 return QQuickTheme::ComboBox;
68 case QQStyleKitReader::ControlType::GroupBox:
69 return QQuickTheme::GroupBox;
70 case QQStyleKitReader::ControlType::HorizontalHeaderViewDelegate:
71 case QQStyleKitReader::ControlType::ItemDelegate:
72 case QQStyleKitReader::ControlType::TableViewDelegate:
73 case QQStyleKitReader::ControlType::TreeViewDelegate:
74 case QQStyleKitReader::ControlType::VerticalHeaderViewDelegate:
75 return QQuickTheme::ItemView;
76 case QQStyleKitReader::ControlType::Label:
77 return QQuickTheme::Label;
78 case QQStyleKitReader::ControlType::MenuBar:
79 case QQStyleKitReader::ControlType::MenuBarItem:
80 return QQuickTheme::MenuBar;
81 case QQStyleKitReader::ControlType::Menu:
82 case QQStyleKitReader::ControlType::MenuItem:
83 case QQStyleKitReader::ControlType::MenuSeparator:
84 return QQuickTheme::Menu;
85 case QQStyleKitReader::ControlType::RadioButton:
86 return QQuickTheme::RadioButton;
87 case QQStyleKitReader::ControlType::SpinBox:
88 return QQuickTheme::SpinBox;
89 case QQStyleKitReader::ControlType::SwitchControl:
90 return QQuickTheme::Switch;
91 case QQStyleKitReader::ControlType::TabBar:
92 case QQStyleKitReader::ControlType::TabButton:
93 return QQuickTheme::TabBar;
94 case QQStyleKitReader::ControlType::TextArea:
95 return QQuickTheme::TextArea;
96 case QQStyleKitReader::ControlType::TextInput:
97 case QQStyleKitReader::ControlType::TextField:
98 return QQuickTheme::TextField;
99 case QQStyleKitReader::ControlType::ToolBar:
100 case QQStyleKitReader::ControlType::ToolButton:
101 case QQStyleKitReader::ControlType::ToolSeparator:
102 return QQuickTheme::ToolBar;
103 case QQStyleKitReader::ControlType::ToolTip:
104 return QQuickTheme::ToolTip;
105 case QQStyleKitReader::ControlType::Tumbler:
106 return QQuickTheme::Tumbler;
107 default:
108 return QQuickTheme::System;
109 }
110 Q_UNREACHABLE();
111}
112
113QQStyleKitTheme::QQStyleKitTheme(QObject *parent)
115{
116}
117
118QQStyleKitStyle *QQStyleKitTheme::style() const
119{
120 QObject *parentObj = parent();
121 if (!parentObj)
122 return nullptr;
123 Q_ASSERT(qobject_cast<QQStyleKitStyle *>(parentObj));
124 return static_cast<QQStyleKitStyle *>(parentObj);
125}
126
127QPalette QQStyleKitTheme::paletteForControlType(QQStyleKitExtendableControlType type) const
128{
129 const QQuickTheme::Scope scope = scopeForType(type);
130 return m_effectivePalettes[int(scope)];
131}
132
133QFont QQStyleKitTheme::fontForControlType(QQStyleKitExtendableControlType type) const
134{
135 const QQuickTheme::Scope scope = scopeForType(type);
136 return m_effectiveFonts[int(scope)];
137}
138
139void QQStyleKitTheme::updateThemePalettes()
140{
141 const QQStyleKitPalette *pals = palettes();
142 if (!pals)
143 return;
144
145 // Collect palette fallback chain
146 QVector<const QQStyleKitPalette *> fbChain;
147 for (auto *fb = pals; fb; fb = fb->fallbackPalette())
148 fbChain.append(fb);
149
150 auto resolveFromPaletteChain = [&](QQuickTheme::Scope scope,
151 QQuickPalette* (QQStyleKitPalette::*getter)() const) -> QPalette
152 {
153 QPalette result;
154
155 // Apply from farthest fallback -> local
156 for (int i = fbChain.size() - 1; i >= 0; --i) {
157 const QQStyleKitPalette *fb = fbChain[i];
158 // Apply system palette first as a base, and override with scope-specific palette
159 if (scope != QQuickTheme::System && fb->isSet(QQuickTheme::System)) {
160 if (auto *sys = fb->system())
161 result = sys->toQPalette().resolve(result);
162 }
163 if (fb->isSet(scope)) {
164 if (auto *p = (fb->*getter)())
165 result = p->toQPalette().resolve(result);
166 }
167 }
168
169 return result;
170 };
171
172 auto setResolved = [&](QQuickPalette* (QQStyleKitPalette::*getter)() const,
173 QQuickTheme::Scope scope)
174 {
175 m_effectivePalettes[int(scope)] = resolveFromPaletteChain(scope, getter);
176 };
177
178 setResolved(&QQStyleKitPalette::system, QQuickTheme::System);
179 setResolved(&QQStyleKitPalette::button, QQuickTheme::Button);
180 setResolved(&QQStyleKitPalette::checkBox, QQuickTheme::CheckBox);
181 setResolved(&QQStyleKitPalette::comboBox, QQuickTheme::ComboBox);
182 setResolved(&QQStyleKitPalette::groupBox, QQuickTheme::GroupBox);
183 setResolved(&QQStyleKitPalette::itemDelegate, QQuickTheme::ItemView);
184 setResolved(&QQStyleKitPalette::label, QQuickTheme::Label);
185 setResolved(&QQStyleKitPalette::radioButton, QQuickTheme::RadioButton);
186 setResolved(&QQStyleKitPalette::spinBox, QQuickTheme::SpinBox);
187 setResolved(&QQStyleKitPalette::tabBar, QQuickTheme::TabBar);
188 setResolved(&QQStyleKitPalette::textArea, QQuickTheme::TextArea);
189 setResolved(&QQStyleKitPalette::textField, QQuickTheme::TextField);
190 setResolved(&QQStyleKitPalette::toolBar, QQuickTheme::ToolBar);
191}
192
193void QQStyleKitTheme::updateThemeFonts()
194{
195 const QQStyleKitFont *fonts = this->fonts();
196 if (!fonts)
197 return;
198
199 // Collect font fallback chain
200 QVector<const QQStyleKitFont *> fbChain;
201 for (auto *fb = fonts; fb; fb = fb->fallbackFont())
202 fbChain.append(fb);
203
204 // QFont::resolve() keeps only the left-hand operand's resolve mask.
205 // Restore the mask explicitly, or every attribute contributed by an
206 // earlier layer of the chain is dropped again by font resolution.
207 auto merge = [](const QFont &a, const QFont &b) {
208 QFont merged = a.resolve(b);
209 merged.setResolveMask(b.resolveMask() | a.resolveMask());
210 return merged;
211 };
212
213 auto resolveFromFontChain = [&](QQuickTheme::Scope scope) -> QFont
214 {
215 QFont result;
216
217 // Apply from farthest fallback -> local
218 for (int i = fbChain.size() - 1; i >= 0; --i) {
219 const QQStyleKitFont *fb = fbChain[i];
220 // Apply system font first as a base, and override with scope-specific font
221 if (scope != QQuickTheme::System && fb->isSet(QQuickTheme::System)) {
222 result = merge(fb->fontForScope(QQuickTheme::System), result);
223 }
224 if (fb->isSet(scope)) {
225 result = merge(fb->fontForScope(scope), result);
226 }
227 }
228
229 return result;
230 };
231
232 for (int i = 0; i < NScopes; ++i) {
233 const QQuickTheme::Scope scope = static_cast<QQuickTheme::Scope>(i);
234 m_effectiveFonts[i] = resolveFromFontChain(scope);
235 }
236}
237
239{
241 m_completed = true;
242}
243
244QT_END_NAMESPACE
245
246#include "moc_qqstylekittheme_p.cpp"
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
QQuickPalette * groupBox() const
QQuickPalette * checkBox() const
QQuickPalette * toolBar() const
QQuickPalette * textField() const
QQuickPalette * spinBox() const
QQuickPalette * system() const
QQuickPalette * comboBox() const
QQuickPalette * itemDelegate() const
QQuickPalette * button() const
QQuickPalette * radioButton() const
QQuickPalette * textArea() const
QQuickPalette * tabBar() const
QQuickPalette * label() const
QQStyleKitPalette * palettes()
QQStyleKitFont * fonts()
QQStyleKitStyle * style() const
QFont fontForControlType(QQStyleKitExtendableControlType type) const
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
QPalette paletteForControlType(QQStyleKitExtendableControlType type) const
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE QQuickTheme::Scope scopeForType(QQStyleKitExtendableControlType type)
\qmltype Theme \inqmlmodule Qt.labs.StyleKit \inherits AbstractStyle