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
qqstylekit.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 "qqstylekit_p.h"
8
9#include <QtGui/QStyleHints>
10
12
13QPointer<QQStyleKitAttached> QQStyleKitAttached::s_instance;
14bool QQStyleKitAttached::s_transitionsEnabled = true;
15
16/*!
17 \qmltype StyleKit
18 \inqmlmodule Qt.labs.StyleKit
19 \brief A singleton for setting and accessing the current style.
20
21 \c StyleKit is a singleton for setting the application \l Style.
22
23 \snippet PlainStyleMain.qml 1
24
25 It also provides properties for global StyleKit configuration and debugging.
26
27 \labs
28
29 \sa Style, StyleVariation
30*/
31
32/*!
33 \qmlproperty Style StyleKit::style
34
35 The active style for the application.
36
37 Set this to a \l Style instance to apply it to all \l {Qt Quick Controls}.
38
39 \snippet PlainStyleMain.qml 1
40
41 \note For best startup performance, set the style on the root \l ApplicationWindow, before
42 any child controls.
43
44 \sa styleUrl, Style
45*/
46
47/*!
48 \qmlproperty string StyleKit::styleUrl
49
50 An alternative to \l style that loads a \l Style from a URL.
51
52 \sa style
53*/
54
55/*!
56 \qmlproperty bool StyleKit::transitionsEnabled
57
58 Controls whether transitions are enabled globally.
59
60 Set this to \c false to disable all transitions in the
61 current style.
62
63 The default value is \c true.
64
65 \sa {ControlStyle::transition}
66*/
67
68/*!
69 \qmlproperty StyleKitDebug StyleKit::debug
70 \readonly
71
72 A run-time debugging tool that can be used to trace
73 style properties for a specific \l Control.
74*/
75
76/*!
77 \qmlmethod bool StyleKit::styleLoaded()
78
79 Returns \c true when the active \l style and its current \l {Style::}{theme} have both
80 finished loading. This can be used to defer initialization logic that
81 depends on resolved style properties.
82
83 \sa style, {Style::}{theme}
84*/
85
86QQStyleKit::QQStyleKit(QObject *parent)
87 : QObject(parent)
88{
89}
90
91QQStyleKitAttached *QQStyleKit::qmlAttachedProperties(QObject *obj)
92{
93 /* QQStyleKitAttached is a singleton. It doesn't matter where it's
94 * used from in the application, it will always represent the same
95 * application global style and theme. */
96 if (!QQStyleKitAttached::s_instance)
97 QQStyleKitAttached::s_instance = new QQStyleKitAttached(QGuiApplication::instance());
98 if (!QQStyleKitAttached::s_instance->m_engine && obj)
99 QQStyleKitAttached::s_instance->m_engine = qmlEngine(obj);
100 return QQStyleKitAttached::s_instance.get();
101}
102
103QQStyleKitAttached::QQStyleKitAttached(QObject *parent)
104 : QObject(parent)
105{
106 connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, [this](){
107 if (m_style && QString::compare(m_style->themeName(), "System"_L1, Qt::CaseInsensitive) == 0)
108 m_style->recreateTheme();
109 });
110}
111
113{
114 QQStyleKitAttached::s_instance = nullptr;
115 if (m_ownsStyle) {
116 delete m_style;
117 m_style = nullptr;
118 }
119}
120
121QQStyleKitStyle *QQStyleKitAttached::style() const
122{
123 return m_style;
124}
125
126void QQStyleKitAttached::setStyle(QQStyleKitStyle *style)
127{
128 if (m_style == style)
129 return;
130
131 if (m_ownsStyle)
132 m_style->deleteLater();
133
134 m_style = style;
135
136 if (m_style && m_style->m_theme) {
137 m_style->m_theme->updateThemePalettes();
138 m_style->m_theme->updateThemeFonts();
139 }
140 if (m_style->loaded())
141 QQStyleKitReader::resetReadersForStyle(m_style);
142
143 emit styleChanged();
144}
145
147{
148 return m_styleUrl;
149}
150
151void QQStyleKitAttached::setStyleUrl(const QString &styleUrl)
152{
153 if (m_styleUrl == styleUrl)
154 return;
155
156 m_styleUrl = styleUrl;
157
158 Q_ASSERT(m_engine);
159 QQmlComponent comp(m_engine, QUrl(styleUrl), this);
160 if (!comp.errors().isEmpty()) {
161 qmlWarning(this) << "Could not create a StyleKit style: " << comp.errorString();
162 return;
163 }
164 auto *style = qobject_cast<QQStyleKitStyle *>(comp.create());
165 if (!style) {
166 qmlWarning(this) << "Could not create a StyleKit style from url: " << styleUrl;
167 return;
168 }
169
170 style->componentComplete();
171 setStyle(style);
172 m_ownsStyle = true;
173 emit styleUrlChanged();
174}
175
177{
178 return s_transitionsEnabled;
179}
180
182{
183 if (enabled == s_transitionsEnabled)
184 return;
185
186 s_transitionsEnabled = enabled;
187 emit transitionsEnabledChanged();
188}
189
191{
192 return &const_cast<QQStyleKitAttached *>(this)->m_debug;
193}
194
196{
197 return m_style && m_style->loaded();
198}
199
200QT_END_NAMESPACE
201
202#include "moc_qqstylekit_p.cpp"
Q_INVOKABLE bool styleLoaded() const
void setStyleUrl(const QString &styleUrl)
QQStyleKitStyle * style() const
void setStyle(QQStyleKitStyle *style)
void setTransitionsEnabled(bool enabled)
QQStyleKitDebug * debug() const
bool transitionsEnabled() const
QString styleUrl() const
Combined button and popup list for selecting options.