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
16QQStyleKit::QQStyleKit(QObject *parent)
17 : QObject(parent)
18{
19}
20
21QQStyleKitAttached *QQStyleKit::qmlAttachedProperties(QObject *obj)
22{
23 /* QQStyleKitAttached is a singleton. It doesn't matter where it's
24 * used from in the application, it will always represent the same
25 * application global style and theme. */
26 if (!QQStyleKitAttached::s_instance)
27 QQStyleKitAttached::s_instance = new QQStyleKitAttached(QGuiApplication::instance());
28 if (!QQStyleKitAttached::s_instance->m_engine && obj)
29 QQStyleKitAttached::s_instance->m_engine = qmlEngine(obj);
30 return QQStyleKitAttached::s_instance.get();
31}
32
33QQStyleKitAttached::QQStyleKitAttached(QObject *parent)
34 : QObject(parent)
35{
36 connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, [this](){
37 if (m_style)
38 m_style->recreateTheme();
39 });
40}
41
43{
44 QQStyleKitAttached::s_instance = nullptr;
45 if (m_ownsStyle) {
46 delete m_style;
47 m_style = nullptr;
48 }
49}
50
52{
53 return m_style;
54}
55
57{
58 if (m_style == style)
59 return;
60
61 if (m_ownsStyle)
62 m_style->deleteLater();
63
64 m_style = style;
65
66 if (m_style && m_style->m_theme) {
67 m_style->m_theme->updateThemePalettes();
68 m_style->m_theme->updateThemeFonts();
69 }
70 if (m_style->loaded())
72
73 emit styleChanged();
74}
75
77{
78 return m_styleUrl;
79}
80
81void QQStyleKitAttached::setStyleUrl(const QString &styleUrl)
82{
83 if (m_styleUrl == styleUrl)
84 return;
85
86 m_styleUrl = styleUrl;
87
88 Q_ASSERT(m_engine);
89 QQmlComponent comp(m_engine, QUrl(styleUrl), this);
90 if (!comp.errors().isEmpty()) {
91 qmlWarning(this) << "Could not create a StyleKit style: " << comp.errorString();
92 return;
93 }
94 auto *style = qobject_cast<QQStyleKitStyle *>(comp.create());
95 if (!style) {
96 qmlWarning(this) << "Could not create a StyleKit style from url: " << styleUrl;
97 return;
98 }
99
100 style->componentComplete();
101 setStyle(style);
102 m_ownsStyle = true;
103 emit styleUrlChanged();
104}
105
107{
108 return s_transitionsEnabled;
109}
110
112{
113 if (enabled == s_transitionsEnabled)
114 return;
115
116 s_transitionsEnabled = enabled;
117 emit transitionsEnabledChanged();
118}
119
121{
122 return &const_cast<QQStyleKitAttached *>(this)->m_debug;
123}
124
126{
127 return m_style && m_style->loaded();
128}
129
130QT_END_NAMESPACE
131
132#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.