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::FlatButton:
56 case QQStyleKitReader::ControlType::RoundButton:
57 return QQuickTheme::Button;
58 case QQStyleKitReader::ControlType::CheckBox:
59 return QQuickTheme::CheckBox;
60 case QQStyleKitReader::ControlType::ComboBox:
61 return QQuickTheme::ComboBox;
62 case QQStyleKitReader::ControlType::GroupBox:
63 return QQuickTheme::GroupBox;
64 case QQStyleKitReader::ControlType::ItemDelegate:
65 return QQuickTheme::ItemView;
66 case QQStyleKitReader::ControlType::Label:
67 return QQuickTheme::Label;
68 case QQStyleKitReader::ControlType::MenuBar:
69 case QQStyleKitReader::ControlType::MenuBarItem:
70 return QQuickTheme::MenuBar;
71 case QQStyleKitReader::ControlType::Menu:
72 case QQStyleKitReader::ControlType::MenuItem:
73 case QQStyleKitReader::ControlType::MenuSeparator:
74 return QQuickTheme::Menu;
75 case QQStyleKitReader::ControlType::RadioButton:
76 return QQuickTheme::RadioButton;
77 case QQStyleKitReader::ControlType::SpinBox:
78 return QQuickTheme::SpinBox;
79 case QQStyleKitReader::ControlType::SwitchControl:
80 return QQuickTheme::Switch;
81 case QQStyleKitReader::ControlType::TabBar:
82 case QQStyleKitReader::ControlType::TabButton:
83 return QQuickTheme::TabBar;
84 case QQStyleKitReader::ControlType::TextArea:
85 return QQuickTheme::TextArea;
86 case QQStyleKitReader::ControlType::TextInput:
87 case QQStyleKitReader::ControlType::TextField:
88 return QQuickTheme::TextField;
89 case QQStyleKitReader::ControlType::ToolBar:
90 case QQStyleKitReader::ControlType::ToolButton:
91 case QQStyleKitReader::ControlType::ToolSeparator:
92 return QQuickTheme::ToolBar;
93 default:
94 return QQuickTheme::System;
95 }
96 Q_UNREACHABLE();
97}
98
99QQStyleKitTheme::QQStyleKitTheme(QObject *parent)
101{
102}
103
104QQStyleKitStyle *QQStyleKitTheme::style() const
105{
106 QObject *parentObj = parent();
107 if (!parentObj)
108 return nullptr;
109 Q_ASSERT(qobject_cast<QQStyleKitStyle *>(parentObj));
110 return static_cast<QQStyleKitStyle *>(parentObj);
111}
112
113QPalette QQStyleKitTheme::paletteForControlType(QQStyleKitExtendableControlType type) const
114{
115 const QQuickTheme::Scope scope = scopeForType(type);
116 return m_effectivePalettes[int(scope)];
117}
118
119QFont QQStyleKitTheme::fontForControlType(QQStyleKitExtendableControlType type) const
120{
121 const QQuickTheme::Scope scope = scopeForType(type);
122 return m_effectiveFonts[int(scope)];
123}
124
125void QQStyleKitTheme::updateThemePalettes()
126{
127 const QQStyleKitPalette *pals = palettes();
128 if (!pals)
129 return;
130
131 // Collect palette fallback chain
132 QVector<const QQStyleKitPalette *> fbChain;
133 for (auto *fb = pals; fb; fb = fb->fallbackPalette())
134 fbChain.append(fb);
135
136 auto resolveFromPaletteChain = [&](QQuickTheme::Scope scope,
137 QQuickPalette* (QQStyleKitPalette::*getter)() const) -> QPalette
138 {
139 QPalette result;
140
141 // Apply from farthest fallback -> local
142 for (int i = fbChain.size() - 1; i >= 0; --i) {
143 const QQStyleKitPalette *fb = fbChain[i];
144 // Apply system palette first as a base, and override with scope-specific palette
145 if (scope != QQuickTheme::System && fb->isSet(QQuickTheme::System)) {
146 if (auto *sys = fb->system())
147 result = sys->toQPalette().resolve(result);
148 }
149 if (fb->isSet(scope)) {
150 if (auto *p = (fb->*getter)())
151 result = p->toQPalette().resolve(result);
152 }
153 }
154
155 return result;
156 };
157
158 auto setResolved = [&](QQuickPalette* (QQStyleKitPalette::*getter)() const,
159 QQuickTheme::Scope scope)
160 {
161 m_effectivePalettes[int(scope)] = resolveFromPaletteChain(scope, getter);
162 };
163
164 setResolved(&QQStyleKitPalette::system, QQuickTheme::System);
165 setResolved(&QQStyleKitPalette::button, QQuickTheme::Button);
166 setResolved(&QQStyleKitPalette::checkBox, QQuickTheme::CheckBox);
167 setResolved(&QQStyleKitPalette::comboBox, QQuickTheme::ComboBox);
168 setResolved(&QQStyleKitPalette::groupBox, QQuickTheme::GroupBox);
169 setResolved(&QQStyleKitPalette::itemDelegate, QQuickTheme::ItemView);
170 setResolved(&QQStyleKitPalette::label, QQuickTheme::Label);
171 setResolved(&QQStyleKitPalette::radioButton, QQuickTheme::RadioButton);
172 setResolved(&QQStyleKitPalette::spinBox, QQuickTheme::SpinBox);
173 setResolved(&QQStyleKitPalette::tabBar, QQuickTheme::TabBar);
174 setResolved(&QQStyleKitPalette::textArea, QQuickTheme::TextArea);
175 setResolved(&QQStyleKitPalette::textField, QQuickTheme::TextField);
176 setResolved(&QQStyleKitPalette::toolBar, QQuickTheme::ToolBar);
177}
178
179void QQStyleKitTheme::updateThemeFonts()
180{
181 const QQStyleKitFont *fonts = this->fonts();
182 if (!fonts)
183 return;
184
185 // Collect font fallback chain
186 QVector<const QQStyleKitFont *> fbChain;
187 for (auto *fb = fonts; fb; fb = fb->fallbackFont())
188 fbChain.append(fb);
189
190 auto resolveFromFontChain = [&](QQuickTheme::Scope scope) -> QFont
191 {
192 QFont result;
193
194 // Apply from farthest fallback -> local
195 for (int i = fbChain.size() - 1; i >= 0; --i) {
196 const QQStyleKitFont *fb = fbChain[i];
197 // Apply system font first as a base, and override with scope-specific font
198 if (scope != QQuickTheme::System && fb->isSet(QQuickTheme::System)) {
199 result = fb->fontForScope(QQuickTheme::System).resolve(result);
200 }
201 if (fb->isSet(scope)) {
202 result = fb->fontForScope(scope).resolve(result);
203 }
204 }
205
206 return result;
207 };
208
209 for (int i = 0; i < NScopes; ++i) {
210 const QQuickTheme::Scope scope = static_cast<QQuickTheme::Scope>(i);
211 m_effectiveFonts[i] = resolveFromFontChain(scope);
212 }
213}
214
216{
218 m_completed = true;
219}
220
221QT_END_NAMESPACE
222
223#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