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
qqstylekitdebug.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
10
12
13/*!
14 \qmltype StyleKitDebug
15 \inqmlmodule Qt.labs.StyleKit
16 \brief Traces how style properties are resolved for a control.
17
18 StyleKitDebug is a diagnostic in-app tool that logs style property reads to the
19 debug output. It is useful for understanding why a control ends up with a
20 particular appearance, especially in complex styles that involve multiple
21 \l {Theme}{themes} or \l {StyleVariation}{style variations}.
22
23 It is accessed through \l {StyleKit.debug}.
24
25 \note This tool is experimental. Enabling it will severely degrade performance and should only
26 be used for debugging. The output format may change in future versions.
27
28 To start tracing, assign the control you want to introspect to the \l control property:
29
30 \snippet StyleKitDebug.qml trace
31
32 Each resolved property is printed as a single line showing where the value
33 came from and what it resolved to:
34
35 \code
36 [read] StyleReader[Hovered].button.background.color -> Style.Theme(Dark).button[Hovered] = #add8e6
37 \endcode
38
39 The \c StyleReader in the output refers to the \l StyleReader that the \l Button
40 uses internally to read its style property values.
41
42 Tracing the property reads of a StyleReader can produce a large amount of output. Use \l filter to
43 limit the output to properties of interest.
44
45 \labs
46
47 \sa StyleKit::debug, {StyleKit Property Resolution}
48*/
49
50/*!
51 \qmlproperty Item StyleKitDebug::control
52
53 The \l{The id Attribute}{id} of the \l Control or \l StyleReader to trace.
54
55 When set, StyleKit logs all style property reads
56 for this item to the debug output. Set to \c null to stop tracing.
57
58 \sa filter
59*/
60
61/*!
62 \qmlproperty string StyleKitDebug::filter
63
64 A regular expression used to filter the debug output. Only lines matching
65 the pattern are printed. By default, all output is shown.
66
67 For example, to only trace the background color:
68
69 \code
70 StyleKit.debug.filter: "background.color"
71 \endcode
72
73 To trace all background colors, including properties
74 such as \l {DelegateStyle.color}{background.color},
75 \l {BorderStyle.color}{background.border.color},
76 \l {ImageStyle.color}{background.image.color},
77 and \l {ShadowStyle.color}{background.shadow.color},
78 use a regular expression:
79
80 \code
81 StyleKit.debug.filter: "background.*color"
82 \endcode
83
84 \sa control
85*/
86
87const QQStyleKitPropertyGroup *QQStyleKitDebug::groupBeingRead = nullptr;
88QPointer<QQuickItem> QQStyleKitDebug::m_item;
89QString QQStyleKitDebug::m_filter;
90int QQStyleKitDebug::m_outputCount = 0;
91
92static const QChar kDot = '.'_L1;
93
94template <typename EnumType>
95QString QQStyleKitDebug::enumToString(EnumType enumValue)
96{
97 auto propertyMetaEnum = QMetaEnum::fromType<EnumType>();
98 return QString::fromUtf8(propertyMetaEnum.valueToKeys(quint64(enumValue)));
99}
100
101QString QQStyleKitDebug::objectName(const QObject *obj) {
102 if (!obj->objectName().isEmpty())
103 return obj->objectName();
104
105 QString str = QString::fromLatin1(obj->metaObject()->className());
106 int idx = str.indexOf("_QMLTYPE"_L1);
107 if (idx != -1)
108 str = str.left(idx);
109 else {
110 const QString prefix("QQStyleKit"_L1);
111 if (str.startsWith(prefix))
112 str = str.mid(prefix.length());
113 }
114
115 return str;
116}
117
118QString QQStyleKitDebug::stateToString(const QQSK::State state)
119{
120 return "["_L1 + enumToString(state) + "]"_L1;
121}
122
123QString QQStyleKitDebug::styleReaderToString(const QQStyleKitReader *reader)
124{
125 return "StyleKitReader"_L1 + stateToString(reader->controlState());
126}
127
128QString QQStyleKitDebug::controlToString(const QQStyleKitControlProperties *control)
129{
130 if (!control->objectName().isEmpty())
131 return control->objectName();
132
133 const QObject *parentObj = control->parent();
134 if (!parentObj)
135 return "<no parent>"_L1;
136
137 auto *controls = qobject_cast<const QQStyleKitControls *>(parentObj);
138 if (!controls)
139 return "<"_L1 + QString::fromUtf8(parentObj->metaObject()->className()) + ">"_L1;
140
141 const int startIndex = QQStyleKitControls::staticMetaObject.propertyOffset();
142 const int endIndex = QQStyleKitControls::staticMetaObject.propertyCount();
143
144 const QMetaObject* parentMeta = parentObj->metaObject();
145 for (int i = startIndex; i < endIndex; ++i) {
146 const QMetaProperty prop = parentMeta->property(i);
147 const QMetaObject* typeMeta = QMetaType::fromName(prop.typeName()).metaObject();
148 if (!typeMeta || !typeMeta->inherits(&QQStyleKitControl::staticMetaObject))
149 continue;
150
151 QObject *propObj = prop.read(parentObj).value<QObject *>();
152 if (propObj == control)
153 return QString::fromUtf8(prop.name());
154 }
155
156 return "<unknown control>"_L1;
157}
158
159QString QQStyleKitDebug::objectPath(const QQStyleKitControlProperties *properties, QObject *from)
160{
161 QString path;
162 const QObject *obj = properties;
163
164 while (obj) {
165 if (!path.isEmpty())
166 path.prepend(kDot);
167
168 if (auto *theme = qobject_cast<const QQStyleKitCustomTheme *>(obj)) {
169 path.prepend(theme->name() + kDot);
170 } else if (auto *theme = qobject_cast<const QQStyleKitTheme *>(obj)) {
171 // Note: only one theme is instantiated at a time
172 if (theme->style())
173 path.prepend("Theme("_L1 + theme->style()->m_effectiveThemeName + ")"_L1);
174 else
175 path.prepend("Theme(Unknown Style)"_L1);
176 } else if (auto *variation = qobject_cast<const QQStyleKitVariation *>(obj)) {
177 path.prepend("StyleVariation("_L1 + variation->name() + ")"_L1);
178 } else if (auto *control = qobject_cast<const QQStyleKitControl *>(obj)) {
179 path.prepend(controlToString(control));
180 } else if (auto *reader = qobject_cast<const QQStyleKitReader *>(obj)) {
181 path.prepend(styleReaderToString(reader));
182 } else {
183 path.prepend(objectName(obj));
184 }
185
186 if (obj == from)
187 break;
188
189 obj = obj->parent();
190 }
191
192 return path;
193}
194
195void QQStyleKitDebug::notifyPropertyRead(
196 const QQStyleKitPropertyPath property,
197 const QQStyleKitControlProperties *storage,
198 const QQSK::State state,
199 const QVariant &value)
200{
201 Q_ASSERT(enabled());
202
203 const QQStyleKitControlProperties *reader = QQStyleKitDebug::groupBeingRead->controlProperties();
204 if (reader->subclass() == QQSK::Subclass::QQStyleKitState) {
205 /* The reader is in the UnfiedStyle, and not in the users application (which can happen
206 * when e.g resolving local bindings between properties in the style). Those are not
207 * interesting to print out when inspecting control-to-style mappings. Ignore. */
208 return;
209 }
210
211 if (!insideControl(reader)) {
212 // We should only debug reads that targets m_item. So return.
213 return;
214 }
215
216 const QString _readerPath = objectPath(reader, m_item);
217 const QString _readPropertyPath = property.toString();
218 const QString queriedPath = _readerPath + kDot +_readPropertyPath;
219
220 QString storagePath;
221 if (storage->subclass() == QQSK::Subclass::QQStyleKitReader) {
222 /* We read an interpolated value stored directly in the reader itself. While this
223 * can be interesting to print out when debugging the styling engine itself, it
224 * comes across as noise when inspecting control-to-style mappings. Ignore. */
225#if 0
226 storagePath = "[local storage] "_L1;
227#else
228 return;
229#endif
230 } else {
231 const QString _controlPathInStyle = objectPath(storage, storage->style());
232 const QString _statePath = stateToString(state);
233 storagePath = _controlPathInStyle + _statePath;
234 }
235
236 QString valueString = value.toString();
237 if (!value.isValid()) // value was set, but probably to undefined
238 valueString = "<undefined>"_L1;
239 else if (valueString.isEmpty())
240 valueString = "<object>"_L1;
241
242 const QString output = queriedPath + " -> "_L1 + storagePath + " = "_L1 + valueString;
243
244 if (!QRegularExpression(m_filter).match(output).hasMatch())
245 return;
246
247 qDebug().nospace().noquote() << m_outputCount++ << " | [read] "_L1 << output;
248}
249
250void QQStyleKitDebug::notifyPropertyWrite(
251 const QQStyleKitPropertyGroup *group,
252 const QQSK::Property property,
253 const QQStyleKitControlProperties *storage,
254 const QQSK::State state,
255 const PropertyStorageId key,
256 const QVariant &value)
257{
258#if 1
259 Q_UNUSED(group);
260 Q_UNUSED(property);
261 Q_UNUSED(storage);
262 Q_UNUSED(state);
263 Q_UNUSED(key);
264 Q_UNUSED(value);
265#else
266 /* Note: in order to catch _all_ writes, we cannot depend on enabling writes from
267 * QML using a property, as that would resolve to 'true' too late. */
268 QString storagePath;
269 if (storage->subclass() == QQSK::Subclass::QQStyleKitReader) {
270 storagePath = "[local storage]"_L1;
271 } else {
272 const QString _controlPathInStyle = objectPath(storage, storage->style());
273 const QString _statePath = stateToString(state);
274 storagePath = _controlPathInStyle + _statePath;
275 }
276
277 QString valueString = value.toString();
278 if (!value.isValid()) // value was set, but probably to undefined
279 valueString = "<undefined>"_L1;
280 else if (valueString.isEmpty())
281 valueString = "<object>"_L1;
282
283 const QString path = property.toString();
284 const QString output = storagePath + kDot + path + " (storage key:"_L1 + QString::number(key) + ") = "_L1 + valueString;
285
286 qDebug().nospace().noquote() << m_outputCount++ << " | [write] "_L1 << output;
287#endif
288}
289
290void QQStyleKitDebug::notifyPropertyNotResolved(const QQStyleKitPropertyPath property)
291{
292 const QQStyleKitControlProperties *reader = QQStyleKitDebug::groupBeingRead->controlProperties();
293 if (!insideControl(reader)) {
294 // We should only debug reads that targets m_item. So return.
295 return;
296 }
297
298 const QString _readerPath = objectPath(reader, m_item);
299 const QString _propertyPath = property.toString();
300 const QString queriedPath = _readerPath + kDot +_propertyPath;
301 const QString output = queriedPath + " -> <property not set>"_L1;
302
303 if (!QRegularExpression(m_filter).match(output).hasMatch())
304 return;
305
306 qDebug().nospace().noquote() << m_outputCount++ << " | [read] "_L1 << output;
307}
308
309void QQStyleKitDebug::trace(
310 const QQStyleKitPropertyPath property,
311 const QQStyleKitControlProperties *storage,
312 const QQSK::State state,
313 const PropertyStorageId key)
314{
315#if 1
316 Q_UNUSED(property);
317 Q_UNUSED(storage);
318 Q_UNUSED(state);
319 Q_UNUSED(key);
320#else
321 const QQStyleKitControlProperties *reader = QQStyleKitDebug::groupBeingRead->controlProperties();
322 if (reader->subclass() == QQSK::Subclass::QQStyleKitState) {
323 /* The reader is in the UnfiedStyle, and not in the users application (which can happen
324 * when e.g resolving local bindings between properties in the style). Those are not
325 * interesting to print out when inspecting control-to-style mappings. Ignore. */
326 return;
327 }
328
329 if (!insideControl(reader)) {
330 // We should only debug reads that targets m_item. So return.
331 return;
332 }
333
334 const QString _readerPath = objectPath(reader, m_item);
335 const QString _readPropertyPath = property.toString();
336 const QString queriedPath = _readerPath + kDot +_readPropertyPath;
337
338 QString storagePath;
339 if (storage->subclass() == QQSK::Subclass::QQStyleKitReader) {
340 /* We read an interpolated value stored directly in the reader itself. While this
341 * can be interesting to print out whe debugging the styling engine itself, it
342 * comes across as noise when inspecting control-to-style mappings. Ignore. */
343#if 0
344 storagePath = "[local storage]"_L1;
345#else
346 return;
347#endif
348 } else {
349 const QString _controlPathInStyle = objectPath(storage, storage->style());
350 const QString _statePath = stateToString(state);
351 storagePath = _controlPathInStyle + _statePath;
352 }
353
354 const QString output = queriedPath + ", checking "_L1 + storagePath + " (storage key:"_L1 + QString::number(key)+ ")"_L1;
355
356 if (!QRegularExpression(m_filter).match(output).hasMatch())
357 return;
358
359 qDebug().nospace().noquote() << m_outputCount++ << " | [trace] "_L1 << output;
360#endif
361}
362
363QQuickItem *QQStyleKitDebug::control() const
364{
365 return m_item;
366}
367
368void QQStyleKitDebug::setControl(QQuickItem *item)
369{
370 if (m_item == item)
371 return;
372
373 m_item = item;
374 emit controlChanged();
375}
376
378{
379 return m_filter;
380}
381
382void QQStyleKitDebug::setFilter(const QString &filter)
383{
384 if (m_filter == filter)
385 return;
386
387 m_filter = filter;
388 emit filterChanged();
389}
390
391bool QQStyleKitDebug::insideControl(const QObject *child)
392{
393 if (!m_item)
394 return false;
395 const QObject *obj = child;
396 do {
397 if (obj == m_item)
398 return true;
399 obj = obj->parent();
400 } while (obj);
401 return false;
402}
403
404QT_END_NAMESPACE
405
406#include "moc_qqstylekitdebug_p.cpp"
QString filter() const
void setFilter(const QString &filter)
void setControl(QQuickItem *item)
QQStyleKitControlProperties * controlProperties() const
Combined button and popup list for selecting options.
static const QChar kDot