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
qqstylekitcontrol.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
7
9
10/*!
11 \qmltype ControlStyle
12 \nativetype QQStyleKitControl
13 \inqmlmodule Qt.labs.StyleKit
14 \inherits ControlStateStyle
15 \brief Defines the style for a control in the \c normal state.
16
17 A ControlStyle describes how a \l Control should be styled. Its API
18 largely mirrors that of a Qt Quick Control: it provides grouped
19 properties for delegates such as
20 \l {ControlStateStyle::background}{background},
21 \l {ControlStateStyle::indicator}{indicator},
22 \l {ControlStateStyle::handle}{handle}, and
23 \l {ControlStateStyle::text}{text}, along with layout
24 properties such as
25 \l {ControlStateStyle::padding}{padding} and
26 \l {ControlStateStyle::spacing}{spacing}.
27 If you are familiar with the API of a \l Control in Qt Quick Controls,
28 you should find the ControlStyle API easy to follow.
29
30 ControlStyle inherits \l ControlStateStyle because it represents the
31 \e normal state: properties set directly on a ControlStyle describe
32 how the control looks when no other state is active. State-specific
33 overrides are set through nested states, such as \l {ControlStateStyle::}{hovered}
34 \l {ControlStateStyle::}{pressed}, and \l {ControlStateStyle::}{checked}.
35
36 \l {StylableControls}{Each stylable control} in a \l Style, \l Theme, or \l StyleVariation is a ControlStyle.
37 For example, in the snippet below, \l {StylableControls::}{control},
38 \l {StylableControls::}{button} and \l {StylableControls::}{radioButton}
39 are all ControlStyles:
40
41 \snippet ControlStyleSnippets.qml ControlStyle
42
43 \sa {StylableControls}{All stylable controls}, Style, Theme,
44 StyleVariation, ControlStateStyle, DelegateStyle
45*/
46
47/*!
48 \qmlproperty list<StyleVariation> ControlStyle::variations
49
50 A list of \l {StyleVariation}{type variations} for this control type.
51
52 A type variation provides alternate styling for controls that are
53 children (or descendants) of this control type. For example, you
54 can use it to style all \l {Button}{buttons} inside a \l {Frame}{frame}
55 differently from buttons elsewhere:
56
57 \snippet TypeVariationSnippets.qml frame with variation
58
59 Unlike instance variations — which are applied to specific control
60 instances from the application via the
61 \l {StyleVariation::variations} attached property — type
62 variations are applied to \e{all} instances of a control type from
63 the \l Style, without requiring the application to opt in.
64
65 \sa StyleVariation
66*/
67
72
73QQmlListProperty<QQStyleKitVariation> QQStyleKitControl::variations()
74{
75 const bool isWrite = subclass() == QQSK::Subclass::QQStyleKitState;
76 if (isWrite) {
77 if (!QQStyleKitPropertyResolver::hasLocalStyleProperty(this, QQSK::Property::Variations)) {
78 /* We need to handle both the setter and the getter in the getter, since QML doesn't
79 * use a separate setter for QQmlListProperty. This means that we need to initialize
80 * the storage with an empty QList, since we need to be prepared for the QML engine
81 * inserting elements into it behind our back. A negative side-effect of this logic
82 * is that a simple read of the property for a QQStyleKitState will also accidentally
83 * populate the local storage of that QQStylKitState, and thereby affect propagation.
84 * Note: since Variations is not a part of QQStyleKitProperties, it's not possible,
85 * and there is also no point, in emitting a changed signal globally, since a
86 * QQuickStyleKitReader inherits QQStyleKitProperties and not QQStyleKitControl,
87 * and as such, doesn't have a 'variations' property. */
88 auto *newList = new QList<QQStyleKitVariation *>();
89 setStyleProperty(QQSK::Property::Variations, newList);
90 }
91 }
92
93 /* Read the property, taking propagation into account. Note that since QQmlListProperty takes
94 * a pointer to a QList as argument, we need to store the list as a pointer as well */
95 const QVariant variant = QQStyleKitPropertyResolver::readStyleProperty(this, QQSK::Property::Variations);
96 if (!variant.isValid()) {
97 // Return a read-only list. Trying to change this list from the app has no effect.
98 static auto *emptyList = new QList<QQStyleKitVariation *>();
99 return QQmlListProperty<QQStyleKitVariation>(this, emptyList);
100 }
101
102 const auto value = qvariant_cast<QList<QQStyleKitVariation *> *>(variant);
103 return QQmlListProperty<QQStyleKitVariation>(this, value);
104}
105
106QVariant QQStyleKitControl::readStyleProperty(PropertyStorageId key) const
107{
108 return m_storage.value(key);
109}
110
111void QQStyleKitControl::writeStyleProperty(PropertyStorageId key, const QVariant &value)
112{
113 m_storage.insert(key, value);
114}
115
116QQStyleKitControls *QQStyleKitControl::controls() const
117{
118 Q_ASSERT(qobject_cast<QQStyleKitControls *>(parent()));
119 return static_cast<QQStyleKitControls *>(parent());
120}
121
122QQStyleKitExtendableControlType QQStyleKitControl::controlType() const
123{
124 const auto &controlsMap = controls()->m_controls;
125 Q_ASSERT(std::find(controlsMap.begin(), controlsMap.end(), this) != controlsMap.end());
126 return controlsMap.key(const_cast<QQStyleKitControl *>(this));
127}
128
129QT_END_NAMESPACE
130
131#include "moc_qqstylekitcontrol_p.cpp"
QObject * parent
Definition qobject.h:74
\inmodule QtCore
Definition qobject.h:106
Combined button and popup list for selecting options.