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