Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qquickattachedpropertypropagator.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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
5
6#include <QtCore/qpointer.h>
7#include <QtQuick/qquickwindow.h>
8#include <QtQuick/private/qquickitem_p.h>
9#include <QtQuick/private/qquickitemchangelistener_p.h>
10#include <QtQuickTemplates2/private/qquickpopup_p.h>
11
13
53{
54 if (!object)
55 return nullptr;
57 return qobject_cast<QQuickAttachedPropertyPropagator *>(qmlAttachedPropertiesObject(object, func, create));
58}
59
71static QQuickAttachedPropertyPropagator *findAttachedParent(const QMetaObject *ourAttachedType, QObject *objectWeAreAttachedTo)
72{
73 QQuickItem *item = qobject_cast<QQuickItem *>(objectWeAreAttachedTo);
74 if (item) {
75 // lookup parent items and popups
77 while (parent) {
78 QQuickAttachedPropertyPropagator *attached = attachedObject(ourAttachedType, parent);
79 if (attached)
80 return attached;
81
82 QQuickPopup *popup = qobject_cast<QQuickPopup *>(parent->parent());
83 if (popup)
84 return attachedObject(ourAttachedType, popup);
85
86 parent = parent->parentItem();
87 }
88
89 // fallback to item's window
90 QQuickAttachedPropertyPropagator *attached = attachedObject(ourAttachedType, item->window());
91 if (attached)
92 return attached;
93 } else {
94 // lookup popup's window
95 QQuickPopup *popup = qobject_cast<QQuickPopup *>(objectWeAreAttachedTo);
96 if (popup)
97 return attachedObject(ourAttachedType, popup->popupItem()->window());
98 }
99
100 // lookup parent window
101 QQuickWindow *window = qobject_cast<QQuickWindow *>(objectWeAreAttachedTo);
102 if (window) {
103 // It doesn't seem like a parent window can be anything but transient in Qt Quick.
104 QQuickWindow *parentWindow = qobject_cast<QQuickWindow *>(window->transientParent());
105 if (parentWindow) {
106 QQuickAttachedPropertyPropagator *attached = attachedObject(ourAttachedType, parentWindow);
107 if (attached)
108 return attached;
109 }
110 }
111
112 // fallback to engine (global)
113 if (objectWeAreAttachedTo) {
114 QQmlEngine *engine = qmlEngine(objectWeAreAttachedTo);
115 if (engine) {
116 QByteArray name = QByteArray("_q_") + ourAttachedType->className();
118 if (!attached) {
119 attached = attachedObject(ourAttachedType, engine, true);
121 }
122 return attached;
123 }
124 }
125
126 return nullptr;
127}
128
130{
132
134 if (!item) {
135 QQuickWindow *window = qobject_cast<QQuickWindow *>(object);
136 if (window)
137 item = window->contentItem();
138 }
139
140 if (!item)
141 return children;
142
143 // At this point, "item" could either be an item that the attached object is
144 // attached to directly, or the contentItem of a window that the attached object
145 // is attached to.
146
147 // Look for attached properties on items.
148 const auto childItems = item->childItems();
149 for (QQuickItem *child : childItems) {
151 if (attached)
152 children += attached;
153 else
154 children += findAttachedChildren(type, child);
155 }
156
157 // Look for attached properties on windows. Windows declared in QML
158 // as children of a Window are QObject-parented to the contentItem (see
159 // QQuickWindowPrivate::data_append()). Windows declared as children
160 // of items are QObject-parented to those items.
161 const auto &windowChildren = item->children();
162 for (QObject *child : windowChildren) {
163 QQuickWindow *childWindow = qobject_cast<QQuickWindow *>(child);
164 if (childWindow) {
165 QQuickAttachedPropertyPropagator *attached = attachedObject(type, childWindow);
166 if (attached)
167 children += attached;
168 }
169 }
170
171 return children;
172}
173
175{
177 if (!item) {
178 QQuickPopup *popup = qobject_cast<QQuickPopup *>(parent);
179 if (popup)
180 item = popup->popupItem();
181 }
182 return item;
183}
184
186{
187public:
188 Q_DECLARE_PUBLIC(QQuickAttachedPropertyPropagator)
189
191 {
192 return attachedObject->d_func();
193 }
194
195 void attachTo(QObject *object);
196 void detachFrom(QObject *object);
198
201
204};
205
207{
209 if (item) {
212 }
213}
214
216{
218 if (item) {
221 }
222}
223
243{
245 if (attachedParent == parent)
246 return;
247
249 if (attachedParent)
252 if (parent)
254 q->attachedParentChange(parent, oldParent);
255}
256
258{
262 if (item)
263 attachedParent = findAttachedParent(q->metaObject(), item);
264 if (!attachedParent)
265 attachedParent = attachedObject(q->metaObject(), window);
267}
268
270{
274}
275
286{
288 d->attachTo(parent);
289}
290
295{
297 d->detachFrom(parent());
298 d->setAttachedParent(nullptr);
299}
300
312{
314 return d->attachedChildren;
315}
316
327{
329 return d->attachedParent;
330}
331
349{
352 if (attachedParent)
353 d->setAttachedParent(attachedParent);
354
358}
359
373{
374 Q_UNUSED(newParent);
375 Q_UNUSED(oldParent);
376}
377
379
380#include "moc_qquickattachedpropertypropagator.cpp"
\inmodule QtCore
Definition qbytearray.h:57
QList< QGraphicsItem * > childItems() const
QGraphicsWidget * window() const
QGraphicsItem * parentItem() const
Returns a pointer to this item's parent item.
Definition qlist.h:74
bool removeOne(const AT &t)
Definition qlist.h:581
void append(parameter_type t)
Definition qlist.h:441
QObject * parent
Definition qobject.h:61
static QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
Definition qobject_p.h:298
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
QVariant property(const char *name) const
Returns the value of the object's name property.
Definition qobject.cpp:4187
bool setProperty(const char *name, const QVariant &value)
Sets the value of the object's name property to value.
\inmodule QtCore
Definition qpointer.h:18
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
QList< QQuickAttachedPropertyPropagator * > attachedChildren
QPointer< QQuickAttachedPropertyPropagator > attachedParent
static QQuickAttachedPropertyPropagatorPrivate * get(QQuickAttachedPropertyPropagator *attachedObject)
void itemParentChanged(QQuickItem *item, QQuickItem *parent) override
void setAttachedParent(QQuickAttachedPropertyPropagator *parent)
The QQuickAttachedPropertyPropagator class provides a way to propagate attached properties.
QQuickAttachedPropertyPropagator * attachedParent() const
This function returns the attached parent of this attached object.
~QQuickAttachedPropertyPropagator()
Destroys the QQuickAttachedPropertyPropagator.
QQuickAttachedPropertyPropagator(QObject *parent=nullptr)
Constructs a QQuickAttachedPropertyPropagator with the given parent.
QList< QQuickAttachedPropertyPropagator * > attachedChildren() const
This function returns the attached children of this attached object.
void initialize()
Finds and sets the attached parent for this attached object, and then does the same for its children.
virtual void attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent)
This function is called whenever the attached parent of this QQuickAttachedPropertyPropagator changes...
void removeItemChangeListener(QQuickItemChangeListener *, ChangeTypes types)
void addItemChangeListener(QQuickItemChangeListener *listener, ChangeTypes types)
static QQuickItemPrivate * get(QQuickItem *item)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
QQuickWindow * window() const
Returns the window in which this item is rendered.
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
T value() const &
Definition qvariant.h:511
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:531
Combined button and popup list for selecting options.
GLenum type
GLuint name
GLenum func
Definition qopenglext.h:663
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
QQmlAttachedPropertiesFunc qmlAttachedPropertiesFunction(QObject *object, const QMetaObject *attachedMetaObject)
Definition qqml.cpp:102
QQmlEngine * qmlEngine(const QObject *obj)
Definition qqml.cpp:76
QObject * qmlAttachedPropertiesObject(QObject *object, QQmlAttachedPropertiesFunc func, bool create)
Definition qqml.cpp:110
static QList< QQuickAttachedPropertyPropagator * > findAttachedChildren(const QMetaObject *type, QObject *object)
static QQuickAttachedPropertyPropagator * attachedObject(const QMetaObject *type, QObject *object, bool create=false)
static QQuickItem * findAttachedItem(QObject *parent)
static QQuickAttachedPropertyPropagator * findAttachedParent(const QMetaObject *ourAttachedType, QObject *objectWeAreAttachedTo)
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:483
#define Q_UNUSED(x)
obj metaObject() -> className()
myObject disconnect()
[26]
QGraphicsItem * item
QLayoutItem * child
[0]
aWidget window() -> setWindowTitle("New Window Title")
[2]
view create()
QJSEngine engine
[0]
\inmodule QtCore
const char * className() const
Returns the class name.
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent