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
qquickpopupwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 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// Qt-Security score:significant reason:default
4
6#if QT_CONFIG(quicktemplates2_container)
7#include "qquickdialog_p.h"
8#endif
11
12#include "qquickmenu_p_p.h"
13#if QT_CONFIG(quicktemplates2_container)
14#include "qquickmenubar_p_p.h"
15#endif
16
18#include <QtGui/private/qguiapplication_p.h>
19
20#include <QtCore/qloggingcategory.h>
21#include <QtGui/private/qeventpoint_p.h>
22#include <QtQuick/qquickrendercontrol.h>
23#include <QtQuick/private/qquickitem_p.h>
24#include <QtQuick/private/qquickwindowmodule_p.h>
25#include <QtQuick/private/qquickwindowmodule_p_p.h>
26#include <qpa/qplatformwindow_p.h>
27
29
30Q_STATIC_LOGGING_CATEGORY(lcPopupWindow, "qt.quick.controls.popup.window")
31
32static bool s_popupGrabOk = false;
34
35#ifdef QT_BUILD_INTERNAL
36// Lets auto tests observe which window holds the popup grab, since the grab
37// itself is only visible to the window system.
38Q_AUTOTEST_EXPORT QWindow *qt_quickpopupwindow_grabbedWindow()
39{
40 return s_popupGrabOk ? s_grabbedWindow.data() : nullptr;
41}
42#endif
43
44/*!
45 \internal
46
47 Returns \c true if \a flags describe a Qt::Popup window.
48 Various other window types have the Qt::Popup bit set, but only pure Qt::Popup windows
49 are appended to the QGuiApplicationPrivate::popup_list. Pure Qt::Popup windows are expected
50 to do a global pointer grab in Qt, while other window types are not.
51*/
52static bool isPopupWindowType(Qt::WindowFlags flags)
53{
54 return (flags & Qt::WindowType_Mask) == Qt::Popup;
55}
56
58{
59 Q_DECLARE_PUBLIC(QQuickPopupWindow)
60
61public:
65 bool m_inHideEvent = false;
66
67protected:
68 void setVisible(bool visible) override;
69
70public:
72
73private:
74 bool filterPopupSpecialCases(QEvent *event);
75};
76
77QQuickPopupWindow::QQuickPopupWindow(QQuickPopup *popup, QWindow *parent)
78 : QQuickWindowQmlImpl(*(new QQuickPopupWindowPrivate), nullptr)
79{
80 Q_D(QQuickPopupWindow);
81
82 d->m_popup = popup;
83 d->m_popupItem = popup->popupItem();
84 setTransientParent(parent);
85
86 connect(d->m_popup, &QQuickPopup::windowChanged, this, &QQuickPopupWindow::windowChanged);
87 connect(d->m_popup, &QQuickPopup::implicitWidthChanged, this, &QQuickPopupWindow::implicitWidthChanged);
88 connect(d->m_popup, &QQuickPopup::implicitHeightChanged, this, &QQuickPopupWindow::implicitHeightChanged);
89 if (QQuickWindow *nearestParentItemWindow = d->m_popup->window()) {
90 d->m_popupParentItemWindow = nearestParentItemWindow;
91 connect(d->m_popupParentItemWindow, &QWindow::xChanged, this, &QQuickPopupWindow::parentWindowXChanged);
92 connect(d->m_popupParentItemWindow, &QWindow::yChanged, this, &QQuickPopupWindow::parentWindowYChanged);
93 }
94 setWidth(d->m_popupItem->implicitWidth());
95 setHeight(d->m_popupItem->implicitHeight());
96
97 const auto flags = QQuickPopupPrivate::get(popup)->popupWindowFlags();
98
99 // For popup windows, we'll need to draw everything, in order to have enough control over the styling.
100 if (flags & Qt::Popup)
101 setColor(QColorConstants::Transparent);
102
103 setFlags(flags);
104
105 qCDebug(lcPopupWindow) << "Created popup window" << this << "with parent" << parent;
106}
107
108QQuickPopup *QQuickPopupWindow::popup() const
109{
110 Q_D(const QQuickPopupWindow);
111 return d->m_popup;
112}
113
114void QQuickPopupWindow::hideEvent(QHideEvent *e)
115{
116 Q_D(QQuickPopupWindow);
117 QQuickWindow::hideEvent(e);
118 // Avoid potential infinite recursion, between QWindowPrivate::setVisible(false) and this function.
119 QScopedValueRollback<bool>inHideEventRollback(d->m_inHideEvent, true);
120 if (d->transientParentIsGoingAway())
121 return;
122
123 if (QQuickPopup *popup = d->m_popup) {
124#if QT_CONFIG(quicktemplates2_container)
125 QQuickDialog *dialog = qobject_cast<QQuickDialog *>(popup);
126 if (dialog && QQuickPopupPrivate::get(dialog)->visible)
127 dialog->reject();
128 else
129#endif
130 popup->setVisible(false);
131 }
132}
133
134void QQuickPopupWindow::moveEvent(QMoveEvent *e)
135{
136 handlePopupPositionChangeFromWindowSystem(e->pos());
137}
138
139void QQuickPopupWindow::resizeEvent(QResizeEvent *e)
140{
141 Q_D(QQuickPopupWindow);
142 QQuickWindowQmlImpl::resizeEvent(e);
143
144 if (!d->m_popupItem)
145 return;
146
147 qCDebug(lcPopupWindow).nospace() << "A window system event changed the popup's ("
148 << d->m_popup << ") size to " << e->size();
149 QQuickPopupPrivate *popupPrivate = QQuickPopupPrivate::get(d->m_popup);
150
151 const auto topLeftFromSystem = global2Local(d->geometry.topLeft());
152 // We need to use the current topLeft position here, so that reposition()
153 // does not move the window
154 const auto oldX = popupPrivate->x;
155 const auto oldY = popupPrivate->y;
156
157 if (Q_LIKELY(topLeftFromSystem)) {
158 popupPrivate->x = topLeftFromSystem->x();
159 popupPrivate->y = topLeftFromSystem->y();
160 }
161
162 const QMarginsF windowInsets = popupPrivate->windowInsets();
163
164 const QRectF popupItemGeometry(windowInsets.left(),
165 windowInsets.top(),
166 e->size().width() - windowInsets.left() - windowInsets.right(),
167 e->size().height() - windowInsets.top() - windowInsets.bottom());
168
169 d->m_popupItem->setWidth(popupItemGeometry.width());
170 d->m_popupItem->setHeight(popupItemGeometry.height());
171
172#if QT_CONFIG(wayland)
173 if (popupPrivate->extendedWindowType != QNativeInterface::Private::QWaylandWindow::Default
174 && QGuiApplication::platformName().startsWith(QLatin1String("wayland"))) {
175 QRegion inputRegion(popupItemGeometry.toRect());
176 setMask(inputRegion);
177 }
178#endif
179
180 // and restore the actual x and y afterwards
181 popupPrivate->x = oldX;
182 popupPrivate->y = oldY;
183}
184
186{
187 Q_Q(const QQuickPopupWindow);
188 QWindow *transientParent = q->transientParent();
189 if (!transientParent)
190 return true;
191
192 auto *quickWindow = qobject_cast<QQuickWindow *>(transientParent);
193 return quickWindow && QQuickWindowPrivate::get(quickWindow)->inDestructor;
194}
195
197{
198 if (m_inHideEvent)
199 return;
200
201 // Showing is pointless once the transient parent is going away, but hiding
202 // must never be skipped: it is the base implementation that updates the
203 // visible state and sends the hide event, and the hide event is what makes
204 // QSGThreadedRenderLoop stop rendering into this window. Skipping it leaves
205 // the render thread believing the window is still exposed, so it re-creates
206 // a swapchain for a window that is about to lose its platform window, and
207 // that swapchain is then leaked. (QTBUG-149041)
208 if (visible && transientParentIsGoingAway())
209 return;
210
211 const bool visibleChanged = QWindowPrivate::visible != visible;
212
213 // Release the grab when the last popup closes.
214 if (!visible && visibleChanged && isPopupWindowType(windowFlags)
215 && QGuiApplicationPrivate::popupCount() == 1) {
216 if (s_grabbedWindow) {
217 s_grabbedWindow->setMouseGrabEnabled(false);
218 s_grabbedWindow->setKeyboardGrabEnabled(false);
219 qCDebug(lcPopupWindow) << "The window " << s_grabbedWindow
220 << "has disabled global mouse and keyboard grabs.";
221 }
222 s_popupGrabOk = false;
223 s_grabbedWindow = nullptr;
224 }
225
226#if QT_CONFIG(wayland)
227 Q_Q(QQuickPopupWindow);
228 // The parent control geoemtry is used by the wayland compositor when flipping menus and comboboxes.
229 if (auto waylandWindow = dynamic_cast<QNativeInterface::Private::QWaylandWindow *>(platformWindow); waylandWindow && visible) {
230 waylandWindow->setParentControlGeometry(q->parentControlGeometry());
231 QQuickPopupPrivate *popupPrivate = QQuickPopupPrivate::get(m_popup);
232 waylandWindow->setCustomMargins(popupPrivate->windowInsets().toMargins());
233 }
234#endif
235
236 QQuickWindowQmlImplPrivate::setVisible(visible);
237
238 // Similar logic to grabForPopup(QWidget *popup)
239 // If the user clicks outside, popups with CloseOnPressOutside*/CloseOnReleaseOutside* need to be able to react,
240 // in order to determine if they should close.
241 // Pointer press and release events should also be filtered by the top-most popup window, and only be delivered to other windows in rare cases.
242 if (visible && visibleChanged && QGuiApplicationPrivate::popupCount() == 1 && !s_popupGrabOk) {
243 QWindow *win = m_popup->window();
244 if (QGuiApplication::platformName() == QStringLiteral("offscreen"))
245 return; // workaround for QTBUG-134009
246 s_popupGrabOk = win->setKeyboardGrabEnabled(true);
247 if (s_popupGrabOk) {
248 s_popupGrabOk = win->setMouseGrabEnabled(true);
249 if (!s_popupGrabOk)
250 win->setKeyboardGrabEnabled(false);
251 s_grabbedWindow = win;
252 qCDebug(lcPopupWindow) << "The window" << win << "has enabled global mouse" << (s_popupGrabOk ? "and keyboard" : "") << "grabs.";
253 }
254 }
255}
256
257/*! \internal
258 Even if all pointer events are sent to the active popup, there are cases
259 where we need to take several popups, or even the menu bar, into account
260 to figure out what the event should do.
261
262 - When clicking outside a popup, the closePolicy should determine whether the
263 popup should close or not. When closing a menu this way, all other menus
264 that are grouped together should also close.
265
266 - We want all open menus and sub menus that belong together to almost act as
267 a single popup WRT hover event delivery. This will allow the user to hover
268 and highlight MenuItems inside all of them, not just this menu. This function
269 will therefore find the menu, or menu bar, under the event's position, and
270 forward hover events to it.
271
272 Note that we for most cases want to return false from this function, even if
273 the event was actually handled. That way it will be also sent to the DA, to
274 let normal event delivery to any potential grabbers happen the usual way. It
275 will also allow QGuiApplication to forward the event to the window under the
276 pointer if the event was outside of any popups (if supported by e.g
277 QPlatformIntegration::ReplayMousePressOutsidePopup).
278 */
279bool QQuickPopupWindowPrivate::filterPopupSpecialCases(QEvent *event)
280{
281 Q_UNUSED(event);
282#if QT_CONFIG(quicktemplates2_container)
283 Q_Q(QQuickPopupWindow);
284
285 if (!event->isPointerEvent())
286 return false;
287
288 QQuickPopup *popup = m_popup;
289 if (!popup)
290 return false;
291
292 auto *pe = static_cast<QPointerEvent *>(event);
293 const QPointF globalPos = pe->points().first().globalPosition();
294 QQuickPopup *targetPopup = QQuickPopupPrivate::get(popup)->contains(contentItem->mapFromGlobal(globalPos)) ? popup : nullptr;
295
296 // Resolve the Menu or MenuBar under the mouse, if any
297 QQuickMenu *menu = qobject_cast<QQuickMenu *>(popup);
298 QQuickMenuBar *targetMenuBar = nullptr;
299 QObject *menuParent = menu;
300 while (menuParent) {
301 if (auto *parentMenu = qobject_cast<QQuickMenu *>(menuParent)) {
302 QQuickPopupWindow *popupWindow = QQuickMenuPrivate::get(parentMenu)->popupWindow;
303 auto *popup_d = QQuickPopupPrivate::get(popupWindow->popup());
304 QPointF scenePos = popupWindow->contentItem()->mapFromGlobal(globalPos);
305 if (popup_d->contains(scenePos)) {
306 targetPopup = parentMenu;
307 break;
308 }
309 } else if (auto *menuBar = qobject_cast<QQuickMenuBar *>(menuParent)) {
310 const QPointF menuBarPos = menuBar->mapFromGlobal(globalPos);
311 if (menuBar->contains(menuBarPos))
312 targetMenuBar = menuBar;
313 break;
314 }
315
316 menuParent = menuParent->parent();
317 }
318
319 auto closePopups = [q, globalPos](QQuickPopup::ClosePolicy closePolicy) {
320 static const QQuickPopup::ClosePolicy outsideParentFlags =
321 QQuickPopup::CloseOnPressOutsideParent | QQuickPopup::CloseOnReleaseOutsideParent;
322
323 QQuickPopup *current = q->popup();
324 while (current && current->closePolicy().testAnyFlags(closePolicy)
325 && QQuickPopupPrivate::get(current)->popupWindow) {
326 auto *currentPrivate = QQuickPopupPrivate::get(current);
327 const QPointF scenePos = currentPrivate->popupWindow->contentItem()->mapFromGlobal(globalPos);
328 if (currentPrivate->contains(scenePos))
329 break; // clicked inside the popup itself: never closes, regardless of policy
330
331 // CloseOnPress/ReleaseOutsideParent closes the popup only when the click is
332 // outside its *parent* too, not merely outside the popup itself - e.g. a click
333 // landing on a sibling/ancestor popup's own content must not close this one
334 // unless the plain Outside flag (checked above) is also what's asked for here.
335 if (current->closePolicy().testAnyFlags(closePolicy & outsideParentFlags)) {
336 const QQuickItem *parentItem = current->parentItem();
337 if (parentItem && parentItem->contains(parentItem->mapFromGlobal(globalPos)))
338 break;
339 }
340
341 qCDebug(lcPopupWindow)
342 << "Closing" << current << "from an outside pointer press or release event.";
343 current->close();
344 auto *transientParentPopupWindow = qobject_cast<QQuickPopupWindow *>(
345 currentPrivate->popupWindow->transientParent());
346 current = current->closePolicy().testAnyFlag(QQuickPopup::CloseMultiple)
347 && transientParentPopupWindow
348 ? transientParentPopupWindow->popup()
349 : nullptr;
350 }
351 };
352
353 if (pe->isBeginEvent()) {
354 if (targetMenuBar) {
355 // If the press was on top of the menu bar, we close all menus and return
356 // true. The latter will stop QGuiApplication from propagating the event
357 // to the window under the pointer, and therefore also to the MenuBar.
358 // The latter would otherwise cause a menu to reopen again immediately, and
359 // undermine that we want to close all popups.
360 closePopups(QQuickPopup::CloseOnPressOutside | QQuickPopup::CloseOnPressOutsideParent);
361 return true;
362 } else if (!targetPopup) {
363 // Pressed outside either a popup window, or a menu or menubar that owns a menu using popup windows.
364 // Note that A QQuickPopupWindow can be bigger than the
365 // menu itself, to make room for a drop-shadow. But if the press was on top
366 // of the shadow, targetMenu will still be nullptr.
367 // On WASM in particular, it's possible for dialogs to receive the event, when clicking in the non-client area. Don't close in those cases.
368 if (event->type() != QEvent::NonClientAreaMouseButtonPress && event->type() != QEvent::NonClientAreaMouseButtonDblClick) {
369 const bool isTouchEventFromTouchPad = event->type() == QEvent::TouchBegin
370 && pe->deviceType() == QInputDevice::DeviceType::TouchPad;
371 if (!isTouchEventFromTouchPad)
372 closePopups(QQuickPopup::CloseOnPressOutside | QQuickPopup::CloseOnPressOutsideParent);
373
374 // A modal popup must consume the press event so that forwardToPopup() sees the event as handled
375 // so that it doesn't propagate to items behind the popup window (QTBUG-131786 etc.)
376 // A QTabletEvent in particular is not accepted by default
377 if (popup->isModal()) {
378 pe->accept();
379 return true;
380 }
381 }
382 return false;
383 }
384 } else if (pe->isUpdateEvent()){
385 QQuickWindow *targetWindow = nullptr;
386 if (targetPopup)
387 targetWindow = QQuickPopupPrivate::get(targetPopup)->popupWindow;
388 else if (targetMenuBar)
389 targetWindow = targetMenuBar->window();
390 else
391 return false;
392
393 // If the target is this popup window itself, normal event delivery will
394 // handle it (e.g. an exclusive grabber like a Slider). Forwarding to the
395 // same DA with the grabber cleared would break drag operations.
396 if (targetWindow == q)
397 return false;
398
399 // Forward move events to the target window
400 const auto scenePos = pe->point(0).scenePosition();
401 const auto translatedScenePos = targetWindow->mapFromGlobal(globalPos);
402 QMutableEventPoint::setScenePosition(pe->point(0), translatedScenePos);
403 auto *grabber = pe->exclusiveGrabber(pe->point(0));
404
405 if (grabber) {
406 // Temporarily disable the grabber, to stop the delivery agent inside
407 // targetWindow from forwarding the event to an item outside the menu
408 // or menubar. This is especially important to support a press on e.g
409 // a MenuBarItem, followed by a drag-and-release on top of a MenuItem.
410 pe->setExclusiveGrabber(pe->point(0), nullptr);
411 }
412
413 qCDebug(lcPopupWindow) << "forwarding" << pe << "to popup menu:" << targetWindow;
414 QQuickWindowPrivate::get(targetWindow)->deliveryAgent->event(pe);
415
416 // Restore the event before we return
417 QMutableEventPoint::setScenePosition(pe->point(0), scenePos);
418 if (grabber)
419 pe->setExclusiveGrabber(pe->point(0), grabber);
420 } else if (pe->isEndEvent()) {
421 if (!targetPopup && !targetMenuBar) {
422 // Released outside either a popup window, or a menu or menubar that owns a menu using popup windows.
423 // This should normally close the current popup window, unless it's inside the non-client area, which can happen in WASM dialogs.
424 if (event->type() != QEvent::NonClientAreaMouseButtonRelease)
425 closePopups(QQuickPopup::CloseOnReleaseOutside | QQuickPopup::CloseOnReleaseOutsideParent);
426 return false;
427 }
428
429 // To support opening a Menu on press (e.g on a MenuBarItem), followed by
430 // a drag and release on a MenuItem inside the Menu, we ask the Menu to
431 // perform a click on the active MenuItem, if any.
432 if (QQuickMenu *targetMenu = qobject_cast<QQuickMenu *>(targetPopup)) {
433 qCDebug(lcPopupWindow) << "forwarding" << pe << "to popup menu:" << targetMenu;
434 QQuickMenuPrivate::get(targetMenu)->handleReleaseWithoutGrab(pe->point(0));
435 }
436 }
437#endif
438
439 return false;
440}
441
442bool QQuickPopupWindow::event(QEvent *e)
443{
444 Q_D(QQuickPopupWindow);
445 if (e->type() == QEvent::PlatformSurface && static_cast<QPlatformSurfaceEvent *>(e)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceCreated) {
446#if QT_CONFIG(wayland)
447 if (auto *waylandWindow = dynamic_cast<QNativeInterface::Private::QWaylandWindow *>(handle())) {
448 auto *popupPrivate = QQuickPopupPrivate::get(d->m_popup);
449 waylandWindow->setExtendedWindowType(popupPrivate->extendedWindowType);
450 waylandWindow->setParentControlGeometry(parentControlGeometry());
451 waylandWindow->setCustomMargins(popupPrivate->windowInsets().toMargins());
452 }
453#endif
454#if QT_CONFIG(xcb)
455 if (auto *xcbWindow = dynamic_cast<QNativeInterface::Private::QXcbWindow *>(handle())) {
456 const auto xcbWindowType = QQuickPopupPrivate::get(d->m_popup)->wmWindowType;
457 if (xcbWindowType != QNativeInterface::Private::QXcbWindow::None)
458 xcbWindow->setWindowType(xcbWindowType);
459 }
460#endif
461 }
462
463 if (d->filterPopupSpecialCases(e))
464 return true;
465
466 if (QQuickPopup *popup = d->m_popup) {
467 // Popups without focus should not consume keyboard events.
468 if (!popup->hasFocus() && (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
469#if QT_CONFIG(shortcut)
470 && (!static_cast<QKeyEvent *>(e)->matches(QKeySequence::Cancel)
471#if defined(Q_OS_ANDROID)
472 || static_cast<QKeyEvent *>(e)->key() != Qt::Key_Back
473#endif
474 )
475#endif
476 ) return false;
477 }
478
479 return QQuickWindowQmlImpl::event(e);
480}
481
482void QQuickPopupWindow::windowChanged(QWindow *window)
483{
484 Q_D(QQuickPopupWindow);
485 if (!d->m_popupParentItemWindow.isNull()) {
486 disconnect(d->m_popupParentItemWindow, &QWindow::xChanged, this, &QQuickPopupWindow::parentWindowXChanged);
487 disconnect(d->m_popupParentItemWindow, &QWindow::yChanged, this, &QQuickPopupWindow::parentWindowYChanged);
488 }
489 if (window) {
490 d->m_popupParentItemWindow = window;
491 connect(window, &QWindow::xChanged, this, &QQuickPopupWindow::parentWindowXChanged);
492 connect(window, &QWindow::yChanged, this, &QQuickPopupWindow::parentWindowYChanged);
493 } else {
494 d->m_popupParentItemWindow.clear();
495 }
496 setTransientParent(window);
497}
498
499std::optional<QPoint> QQuickPopupWindow::global2Local(const QPoint &pos) const
500{
501 Q_D(const QQuickPopupWindow);
502 QQuickPopup *popup = d->m_popup;
503 Q_ASSERT(popup);
504 QWindow *mainWindow = d->m_popupParentItemWindow;
505 if (!mainWindow)
506 mainWindow = transientParent();
507 if (Q_UNLIKELY((!mainWindow || mainWindow != popup->window())))
508 return std::nullopt;
509
510 const QPoint scenePos = mainWindow->mapFromGlobal(pos);
511 // Popup's coordinates are relative to the nearest parent item.
512 return popup->parentItem() ? popup->parentItem()->mapFromScene(scenePos).toPoint() : scenePos;
513}
514
515void QQuickPopupWindow::parentWindowXChanged(int newX)
516{
517 const auto popupLocalPos = global2Local({x(), y()});
518 if (Q_UNLIKELY(!popupLocalPos))
519 return;
520 handlePopupPositionChangeFromWindowSystem({ newX + popupLocalPos->x(), y() });
521}
522
523void QQuickPopupWindow::parentWindowYChanged(int newY)
524{
525 const auto popupLocalPos = global2Local({x(), y()});
526 if (Q_UNLIKELY(!popupLocalPos))
527 return;
528 handlePopupPositionChangeFromWindowSystem({ x(), newY + popupLocalPos->y() });
529}
530
531void QQuickPopupWindow::handlePopupPositionChangeFromWindowSystem(const QPoint &pos)
532{
533 Q_D(QQuickPopupWindow);
534 QQuickPopup *popup = d->m_popup;
535 if (!popup)
536 return;
537
538 const auto windowPos = global2Local(pos);
539 if (Q_LIKELY(windowPos)) {
540 qCDebug(lcPopupWindow).nospace() << "A window system event changed the popup's"
541 << " (" << d->m_popup << ") position to " << *windowPos;
542 QQuickPopupPrivate::get(popup)->setEffectivePosFromWindowPos(*windowPos);
543 }
544}
545
546void QQuickPopupWindow::implicitWidthChanged()
547{
548 Q_D(QQuickPopupWindow);
549 if (auto popup = d->m_popup) {
550 auto *popupPrivate = QQuickPopupPrivate::get(popup);
551 // Include window insets so that it works for styles like FluentWinUI3
552 // that have insets.
553 // Use qCeil to avoid sub-pixel truncation that causes text wrapping
554 // (see QTBUG-130683). This matches the initial sizing logic in
555 // QQuickPopupPrivate::adjustPopupItemParentAndWindow().
556 const QMarginsF insets = popupPrivate->windowInsets();
557 if (!popupPrivate->hasWidth) {
558 setWidth(qCeil(popup->implicitWidth() + insets.left() + insets.right()));
559 d->m_popupItem->setWidth(popup->implicitWidth());
560 }
561 popupPrivate->reposition();
562 }
563}
564
565void QQuickPopupWindow::implicitHeightChanged()
566{
567 Q_D(QQuickPopupWindow);
568 if (auto popup = d->m_popup) {
569 auto *popupPrivate = QQuickPopupPrivate::get(popup);
570 const QMarginsF insets = popupPrivate->windowInsets();
571 if (!popupPrivate->hasHeight) {
572 setHeight(qCeil(popup->implicitHeight() + insets.top() + insets.bottom()));
573 d->m_popupItem->setHeight(popup->implicitHeight());
574 }
575 popupPrivate->reposition();
576 }
577}
578
579#if QT_CONFIG(wayland)
580QRect QQuickPopupWindow::parentControlGeometry() const
581{
582 const QQuickItem *parent = popup()->parentItem();
583 // Menus have an overlap property that we want to honor,
584 // as long as it's not wider than half the width of the parent item.
585 qreal overlap = 0;
586 if (QQuickMenu *menu = qobject_cast<QQuickMenu *>(popup())) {
587 if (menu->cascade() && QQuickMenuPrivate::get(menu)->parentMenu)
588 overlap = menu->overlap();
589 }
590 QRectF parentItemBoundingRect = parent->boundingRect();
591 QPointF parentItemMappedPosition = parent->mapToScene(parentItemBoundingRect.topLeft());
592 QPoint offset;
593 if (QQuickRenderControl::renderWindowFor(parent->window(), &offset))
594 parentItemMappedPosition += offset.toPointF();
595
596 return { qFloor(parentItemMappedPosition.x() + overlap), qFloor(parentItemMappedPosition.y()),
597 qCeil(qMax<qreal>(qAbs(parentItemBoundingRect.width() - overlap * 2), parentItemBoundingRect.width() / 4)), qCeil(parentItemBoundingRect.height()) };
598}
599#endif
600
601QT_END_NAMESPACE
QPointer< QQuickPopup > m_popup
QPointer< QWindow > m_popupParentItemWindow
void setVisible(bool visible) override
Combined button and popup list for selecting options.
static QPointer< QWindow > s_grabbedWindow
static bool isPopupWindowType(Qt::WindowFlags flags)