91static QQuickAttachedPropertyPropagator *attachedObject(
const QMetaObject *type, QObject *object,
bool create =
false)
95 auto func = qmlAttachedPropertiesFunction(object, type);
96 return qobject_cast<QQuickAttachedPropertyPropagator *>(qmlAttachedPropertiesObject(object, func, create));
112 qCDebug(lcAttached).noquote() <<
"findAttachedParent called with" << ourAttachedType->className() << objectWeAreAttachedTo;
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 auto popupItem = qobject_cast<QQuickPopupItem *>(objectWeAreAttachedTo);
134 qCDebug(lcAttached).noquote() <<
"- attachee is a popup item" << popupItem <<
"- checking if it has an attached object";
135 auto popupItemPrivate = QQuickPopupItemPrivate::get(popupItem);
136 QQuickAttachedPropertyPropagator *popupAttached = attachedObject(ourAttachedType, popupItemPrivate->popup);
138 qCDebug(lcAttached).noquote() <<
"- popup item has attached object" << popupAttached <<
"- returning";
139 return popupAttached;
141 qCDebug(lcAttached).noquote() <<
"- popup item does not have attached object";
144 qCDebug(lcAttached).noquote() <<
"- attachee is not a popup item";
147 QQuickItem *item = qobject_cast<QQuickItem *>(objectWeAreAttachedTo);
149 qCDebug(lcAttached).noquote() <<
"- attachee is an item; checking its parent items and popups";
151 QQuickItem *parent = item->parentItem();
153 qCDebug(lcAttached).noquote() <<
" - checking parent item" << parent;
154 QQuickAttachedPropertyPropagator *attached = attachedObject(ourAttachedType, parent);
156 qCDebug(lcAttached).noquote() <<
" - parent item has attached object" << attached <<
"- returning";
160 QQuickPopup *popup = qobject_cast<QQuickPopup *>(parent->parent());
162 qCDebug(lcAttached).noquote() <<
" - parent popup has attached object" << attached <<
"- returning";
163 return attachedObject(ourAttachedType, popup);
166 parent = parent->parentItem();
170 qCDebug(lcAttached).noquote() <<
"- checking parent window" << item->window();
171 QQuickAttachedPropertyPropagator *attached = attachedObject(ourAttachedType, item->window());
173 qCDebug(lcAttached).noquote() <<
"- parent window has attached object" << attached <<
"- returning";
178 QQuickPopup *popup = qobject_cast<QQuickPopup *>(objectWeAreAttachedTo);
180 qCDebug(lcAttached).noquote() <<
"- attachee is a popup; checking its window";
181 auto* popupWindow = popup->popupItem()->window();
182 auto *object = attachedObject(ourAttachedType, popupWindow);
185 if (!object && qobject_cast<QQuickPopupWindow *>(popupWindow))
186 return attachedObject(ourAttachedType, popupWindow->transientParent());
192 QQuickWindow *window = qobject_cast<QQuickWindow *>(objectWeAreAttachedTo);
195 QQuickWindow *parentWindow = qobject_cast<QQuickWindow *>(window->transientParent());
196 qCDebug(lcAttached).noquote() <<
"- attachee is a window; checking its parent window" << parentWindow;
198 QQuickAttachedPropertyPropagator *attached = attachedObject(ourAttachedType, parentWindow);
200 qCDebug(lcAttached).noquote() <<
"- parent window has attached object" << attached <<
"- returning";
207 if (objectWeAreAttachedTo) {
208 QQmlEngine *engine = qmlEngine(objectWeAreAttachedTo);
209 qCDebug(lcAttached).noquote() <<
"- falling back to engine" << engine;
211 QByteArray name = QByteArray(
"_q_") + ourAttachedType->className();
212 QQuickAttachedPropertyPropagator *attached = engine->property(name).value<QQuickAttachedPropertyPropagator *>();
214 attached = attachedObject(ourAttachedType, engine,
true);
215 engine->setProperty(name, QVariant::fromValue(attached));
226 QList<QQuickAttachedPropertyPropagator *> children;
228 QQuickItem *item = qobject_cast<QQuickItem *>(object);
230 QQuickWindow *window = qobject_cast<QQuickWindow *>(object);
232 item = window->contentItem();
243 const auto childItems = item->childItems();
244 for (QQuickItem *child : childItems) {
245 QQuickAttachedPropertyPropagator *attached = attachedObject(type, child);
247 children += attached;
249 children += findAttachedChildren(type, child);
256 const auto &windowChildren = item->children();
257 for (QObject *child : windowChildren) {
258 QQuickWindow *childWindow = qobject_cast<QQuickWindow *>(child);
260 QQuickAttachedPropertyPropagator *attached = attachedObject(type, childWindow);
262 children += attached;
305 if (QQuickItem *item = findAttachedItem(object)) {
306 connect(item, &QQuickItem::windowChanged,
this, &QQuickAttachedPropertyPropagatorPrivate::itemWindowChanged);
307 QQuickItemPrivate::get(item)->addItemChangeListener(
this, QQuickItemPrivate::Parent);
308 }
else if (
auto *window = qobject_cast<QQuickWindow *>(object)) {
309 QObjectPrivate::connect(window, &QWindow::transientParentChanged,
this,
310 &QQuickAttachedPropertyPropagatorPrivate::transientParentWindowChanged);
316 if (QQuickItem *item = findAttachedItem(object)) {
317 disconnect(item, &QQuickItem::windowChanged,
this, &QQuickAttachedPropertyPropagatorPrivate::itemWindowChanged);
318 QQuickItemPrivate::get(item)->removeItemChangeListener(
this, QQuickItemPrivate::Parent);
319 }
else if (
auto *window = qobject_cast<QQuickWindow *>(object)) {
320 QObjectPrivate::disconnect(window, &QWindow::transientParentChanged,
321 this, &QQuickAttachedPropertyPropagatorPrivate::transientParentWindowChanged);
345 Q_Q(QQuickAttachedPropertyPropagator);
346 if (attachedParent == parent)
349 QQuickAttachedPropertyPropagator *oldParent = attachedParent;
350 qCDebug(lcAttached).noquote() <<
"setAttachedParent called on" << q <<
"with parent" << parent;
351 if (attachedParent) {
352 qCDebug(lcAttached).noquote() <<
"- removing ourselves as an attached child of" << attachedParent;
353 QQuickAttachedPropertyPropagatorPrivate::get(attachedParent)->attachedChildren.removeOne(q);
355 attachedParent = parent;
357 qCDebug(lcAttached).noquote() <<
"- adding ourselves as an attached child of" << parent;
358 QQuickAttachedPropertyPropagatorPrivate::get(parent)->attachedChildren.append(q);
360 q->attachedParentChange(parent, oldParent);
393 Q_Q(QQuickAttachedPropertyPropagator);
394 QQuickAttachedPropertyPropagator *attachedParent =
nullptr;
395 qCDebug(lcAttached).noquote() <<
"transient parent window of" << q <<
"changed to" << newTransientParent;
396 attachedParent = findAttachedParent(firstCppMetaObject(q), q->parent());
398 attachedParent = attachedObject(firstCppMetaObject(q), newTransientParent);
399 setAttachedParent(attachedParent);
482void QQuickAttachedPropertyPropagator::initialize()
484 Q_D(QQuickAttachedPropertyPropagator);
485 qCDebug(lcAttached) <<
"initialize called for" << parent() <<
"- looking for attached parent...";
486 QQuickAttachedPropertyPropagator *attachedParent = findAttachedParent(metaObject(), parent());
488 d->setAttachedParent(attachedParent);
490 const QList<QQuickAttachedPropertyPropagator *> attachedChildren = findAttachedChildren(metaObject(), parent());
491 qCDebug(lcAttached) <<
"- found" << attachedChildren.size() <<
"attached children:";
492 for (QQuickAttachedPropertyPropagator *child : attachedChildren) {
493 qCDebug(lcAttached) <<
" -" << child->parent();
494 QQuickAttachedPropertyPropagatorPrivate::get(child)->setAttachedParent(
this);
497 qCDebug(lcAttached) <<
"... finished initializing";