From d70e5ebe11a8eb22fe5e71945982d81991dc9f09 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 9 Sep 2014 17:11:03 +0200 Subject: Fix menu popups positioning with window containers Task-number: QTBUG-40883 Change-Id: I5c46c049b5890259d65f325adb59e15ebf29f84a Reviewed-by: Robert Loehning Reviewed-by: Gabriel de Dietrich --- src/controls/qquickmenupopupwindow.cpp | 11 +++++++++-- src/controls/qquickmenupopupwindow_p.h | 1 + src/controls/qquickpopupwindow.cpp | 19 ++++++------------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/controls/qquickmenupopupwindow.cpp b/src/controls/qquickmenupopupwindow.cpp index e07a41d6..bdd1cd6b 100644 --- a/src/controls/qquickmenupopupwindow.cpp +++ b/src/controls/qquickmenupopupwindow.cpp @@ -50,8 +50,10 @@ QT_BEGIN_NAMESPACE QQuickMenuPopupWindow::QQuickMenuPopupWindow() : - QQuickPopupWindow(), m_itemAt(0) -{ } + m_itemAt(0), + m_logicalParentWindow(0) +{ +} void QQuickMenuPopupWindow::show() { @@ -59,6 +61,10 @@ void QQuickMenuPopupWindow::show() // show() will reposition the popup at the last moment, // so its initial position must be captured after the call. m_initialPos = position(); + if (m_logicalParentWindow && m_logicalParentWindow->parent()) { + // This must be a QQuickWindow embedded via createWindowContainer. + m_initialPos += m_logicalParentWindow->geometry().topLeft(); + } } void QQuickMenuPopupWindow::setParentItem(QQuickItem *item) @@ -92,6 +98,7 @@ void QQuickMenuPopupWindow::setParentWindow(QWindow *effectiveParentWindow, QQui effectiveParentWindow = effectiveParentWindow->parent(); if (transientParent() != effectiveParentWindow) setTransientParent(effectiveParentWindow); + m_logicalParentWindow = parentWindow; if (parentWindow) { connect(parentWindow, SIGNAL(destroyed()), this, SLOT(dismissPopup())); if (QQuickMenuPopupWindow *pw = qobject_cast(parentWindow)) diff --git a/src/controls/qquickmenupopupwindow_p.h b/src/controls/qquickmenupopupwindow_p.h index a550daa2..7da861d9 100644 --- a/src/controls/qquickmenupopupwindow_p.h +++ b/src/controls/qquickmenupopupwindow_p.h @@ -69,6 +69,7 @@ private: QQuickItem *m_itemAt; QPointF m_oldItemPos; QPointF m_initialPos; + QQuickWindow *m_logicalParentWindow; }; QT_END_NAMESPACE diff --git a/src/controls/qquickpopupwindow.cpp b/src/controls/qquickpopupwindow.cpp index eefeadb7..fe697d31 100644 --- a/src/controls/qquickpopupwindow.cpp +++ b/src/controls/qquickpopupwindow.cpp @@ -68,23 +68,16 @@ void QQuickPopupWindow::show() { qreal posx = x(); qreal posy = y(); - if (QQuickWindow *parentWindow = qobject_cast(transientParent())) { + // transientParent may not be a QQuickWindow when embedding into widgets + if (QWindow *tp = transientParent()) { if (m_parentItem) { - QPointF pos = m_parentItem->mapToItem(parentWindow->contentItem(), QPointF(posx, posy)); + QPointF pos = m_parentItem->mapToItem(m_parentItem->window()->contentItem(), QPointF(posx, posy)); posx = pos.x(); posy = pos.y(); } - - if (parentWindow->parent()) { - // If the parent window is embedded in another window, the offset needs to be relative to - // its top-level window container, or to global coordinates, which is the same in the end. - QPoint parentWindowOffset = parentWindow->mapToGlobal(QPoint()); - posx += parentWindowOffset.x(); - posy += parentWindowOffset.y(); - } else { - posx += parentWindow->geometry().left(); - posy += parentWindow->geometry().top(); - } + QPoint tlwOffset = tp->mapToGlobal(QPoint()); + posx += tlwOffset.x(); + posy += tlwOffset.y(); } else if (m_parentItem && m_parentItem->window()) { QPoint offset; QQuickWindow *quickWindow = m_parentItem->window(); -- cgit v1.2.1