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