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
qqstylekitdelegatecontainer.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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 <QtQuick/private/qquickitem_p.h>
5
7#include "../qqstylekitcontrolproperties_p.h"
8
10
11QQmlComponent* QQStyleKitDelegateContainer::s_defaultDelegateComponent = nullptr;
12QQmlComponent* QQStyleKitDelegateContainer::s_defaultShadowComponent = nullptr;
13
14QQStyleKitDelegateContainer::QQStyleKitDelegateContainer(QQuickItem *parent)
15 : QQuickItem(parent)
16{
17}
18
20{
21 // Disconnect before QQuickItem::~QQuickItem() reparents children, which emits
22 // signals. At that point our vtable would already be replaced with QQuickItem's,
23 // causing assertObjectType to fail.
24 if (m_delegateInstance)
25 disconnect(m_delegateInstance, nullptr, this, nullptr);
26}
27
28QQStyleKitDelegateProperties *QQStyleKitDelegateContainer::delegateProperties() const
29{
30 return m_delegateProperties;
31}
32
33void QQStyleKitDelegateContainer::setDelegateProperties(QQStyleKitDelegateProperties *delegateProperties)
34{
35 if (m_delegateProperties == delegateProperties)
36 return;
37
38 if (m_delegateProperties) {
39 /* We don't expect delegateProperties to change after componentCompleted(), since it's bound
40 * to a controls StyleKitReader, which is not supposed to change. So, considering that there
41 * might be hundreds of delegate instances in an application, we try to save some connections
42 * this way. But note, this is only an optimization, not a technical limitation. */
43 qmlWarning(this) << "Changing delegateProperties on StyleKitContainer is not supported.";
44 return;
45 }
46
47 m_delegateProperties = delegateProperties;
48 emit delegatePropertiesChanged();
49}
50
52{
53 return m_control;
54}
55
57{
58 if (m_control == control)
59 return;
60 m_control = control;
61 emit parentControlChanged();
62}
63
65{
66 return m_delegateInstance;
67}
68
69void QQStyleKitDelegateContainer::updateImplicitSize()
70{
71 setImplicitWidth(m_delegateInstance ? m_delegateInstance->implicitWidth() : 0);
72 setImplicitHeight(m_delegateInstance ? m_delegateInstance->implicitHeight() : 0);
73}
74
75void QQStyleKitDelegateContainer::maybeCreateDelegate()
76{
77 if (m_delegateInstance)
78 return;
79 if (!m_delegateProperties)
80 return;
81
82 if (!m_delegateProperties->visible()) {
83 connect(m_delegateProperties, &QQStyleKitDelegateProperties::visibleChanged,
84 this, &QQStyleKitDelegateContainer::maybeCreateDelegate, Qt::UniqueConnection);
85 return;
86 }
87
88 disconnect(m_delegateProperties, &QQStyleKitDelegateProperties::visibleChanged,
89 this, &QQStyleKitDelegateContainer::maybeCreateDelegate);
90
91 if (QQmlComponent *delegateComponent = m_delegateProperties->delegate()) {
92 m_delegateComponent = delegateComponent;
93 } else {
94 if (!s_defaultDelegateComponent || s_defaultDelegateComponent->engine() != qmlEngine(this)) {
95 delete s_defaultDelegateComponent;
96 QQmlEngine *engine = qmlEngine(this);
97 s_defaultDelegateComponent = new QQmlComponent(engine);
98 const QString qmlCode = QString::fromUtf8(R"(
99 import QtQuick
100 import Qt.labs.StyleKit
101 StyleKitDelegate {
102 width: parent.width
103 height: parent.height
104 }
105 )");
106 s_defaultDelegateComponent->setData(qmlCode.toUtf8(), QUrl());
107 Q_ASSERT_X(!s_defaultDelegateComponent->isError(), __FUNCTION__,
108 s_defaultDelegateComponent->errorString().toUtf8().constData());
109 }
110 m_delegateComponent = s_defaultDelegateComponent;
111 }
112
113 QQmlContext *ctx = QQmlEngine::contextForObject(this);
114 m_delegateInstance = qobject_cast<QQuickItem*>(m_delegateComponent->beginCreate(ctx));
115 Q_ASSERT(m_delegateInstance);
116 m_delegateInstance->setParent(this);
117 m_delegateInstance->setParentItem(this);
118 m_delegateInstance->setProperty("control", QVariant::fromValue(m_control.get()));
119 m_delegateInstance->setProperty("delegateProperties", QVariant::fromValue(m_delegateProperties.get()));
120 m_delegateComponent->completeCreate();
121
122 updateImplicitSize();
123 connect(m_delegateInstance, &QQuickItem::implicitWidthChanged, this, &QQStyleKitDelegateContainer::updateImplicitSize);
124 connect(m_delegateInstance, &QQuickItem::implicitHeightChanged, this, &QQStyleKitDelegateContainer::updateImplicitSize);
125}
126
127void QQStyleKitDelegateContainer::maybeCreateShadow()
128{
129 if (m_shadowInstance)
130 return;
131 if (!m_delegateProperties)
132 return;
133
134 if (!m_delegateProperties->visible()) {
135 connect(m_delegateProperties, &QQStyleKitDelegateProperties::visibleChanged,
136 this, &QQStyleKitDelegateContainer::maybeCreateShadow, Qt::UniqueConnection);
137 return;
138 }
139 if (!m_delegateProperties->shadow()->visible()) {
140 connect(m_delegateProperties->shadow(), &QQStyleKitShadowProperties::visibleChanged,
141 this, &QQStyleKitDelegateContainer::maybeCreateShadow, Qt::UniqueConnection);
142 return;
143 }
144 if (m_delegateProperties->shadow()->color().alpha() == 0) {
145 connect(m_delegateProperties->shadow(), &QQStyleKitShadowProperties::colorChanged,
146 this, &QQStyleKitDelegateContainer::maybeCreateShadow, Qt::UniqueConnection);
147 return;
148 }
149 if (m_delegateProperties->shadow()->opacity() == 0) {
150 connect(m_delegateProperties->shadow(), &QQStyleKitShadowProperties::opacityChanged,
151 this, &QQStyleKitDelegateContainer::maybeCreateShadow, Qt::UniqueConnection);
152 return;
153 }
154
155 disconnect(m_delegateProperties, &QQStyleKitDelegateProperties::visibleChanged,
156 this, &QQStyleKitDelegateContainer::maybeCreateShadow);
157 disconnect(m_delegateProperties->shadow(), &QQStyleKitShadowProperties::visibleChanged,
158 this, &QQStyleKitDelegateContainer::maybeCreateShadow);
159 disconnect(m_delegateProperties->shadow(), &QQStyleKitShadowProperties::colorChanged,
160 this, &QQStyleKitDelegateContainer::maybeCreateShadow);
161 disconnect(m_delegateProperties->shadow(), &QQStyleKitShadowProperties::opacityChanged,
162 this, &QQStyleKitDelegateContainer::maybeCreateShadow);
163
164 if (QQmlComponent *shadowComponent = m_delegateProperties->shadow()->delegate()) {
165 m_shadowComponent = shadowComponent;
166 } else {
167 if (!s_defaultShadowComponent || s_defaultShadowComponent->engine() != qmlEngine(this)) {
168 delete s_defaultShadowComponent;
169 QQmlEngine *engine = qmlEngine(this);
170 s_defaultShadowComponent = new QQmlComponent(engine);
171 const QString qmlCode = QString::fromUtf8(R"(
172 import Qt.labs.StyleKit.impl
173 Shadow {}
174 )");
175 s_defaultShadowComponent->setData(qmlCode.toUtf8(), QUrl());
176 Q_ASSERT_X(!s_defaultShadowComponent->isError(), __FUNCTION__,
177 s_defaultShadowComponent->errorString().toUtf8().constData());
178 }
179 m_shadowComponent = s_defaultShadowComponent;
180 }
181
182 QQmlContext *ctx = QQmlEngine::contextForObject(this);
183 m_shadowInstance = qobject_cast<QQuickItem*>(m_shadowComponent->beginCreate(ctx));
184 Q_ASSERT(m_shadowInstance);
185 m_shadowInstance->setParent(this);
186 m_shadowInstance->setParentItem(this);
187 m_shadowInstance->setZ(-1);
188 m_shadowInstance->setProperty("control", QVariant::fromValue(m_control.get()));
189 m_shadowInstance->setProperty("delegateProperties", QVariant::fromValue(m_delegateProperties.get()));
190 m_shadowComponent->completeCreate();
191}
192
194{
195 QQuickItem::componentComplete();
196 Q_ASSERT(m_delegateProperties);
197 Q_ASSERT(m_control);
198
199 maybeCreateDelegate();
200 connect(m_delegateProperties, &QQStyleKitDelegateProperties::delegateChanged, this, [this]{
201 if (!m_delegateInstance) {
202 maybeCreateDelegate();
203 } else {
204 const QQmlComponent *newDelegateComp = m_delegateProperties->delegate();
205 if (m_delegateComponent == newDelegateComp)
206 return;
207 if (!newDelegateComp && m_delegateComponent == s_defaultDelegateComponent) {
208 /* If newDelegateComp is nullptr, it means that we should use the default
209 * delegate instead, which we already do. */
210 return;
211 }
212
213 delete m_delegateInstance;
214 maybeCreateDelegate();
215 emit delegateInstanceChanged();
216 }
217 });
218
219 maybeCreateShadow();
220 connect(m_delegateProperties->shadow(), &QQStyleKitShadowProperties::delegateChanged, this, [this]{
221 if (!m_shadowInstance) {
222 maybeCreateShadow();
223 } else {
224 const QQmlComponent *newShadowComp = m_delegateProperties->shadow()->delegate();
225 if (m_shadowComponent == newShadowComp)
226 return;
227 if (!newShadowComp && m_shadowComponent == s_defaultShadowComponent) {
228 /* If newShadowComp is nullptr, it means that we should use the default
229 * delegate instead, which we already do. */
230 return;
231 }
232
233 delete m_shadowInstance;
234 maybeCreateShadow();
235 }
236 });
237}
238
239QT_END_NAMESPACE
240
241#include "moc_qqstylekitdelegatecontainer_p.cpp"
QQStyleKitDelegateProperties * delegateProperties() const
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
void setDelegateProperties(QQStyleKitDelegateProperties *delegateProperties)
Combined button and popup list for selecting options.