Resolve QWidget window handle without depending on QtWidgets

Change-Id: If5b2f17283193d7a1718f476b72f380e9e67d0f0
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Tor Arne Vestbø 2020-03-31 19:22:44 +02:00
parent c83ae88faf
commit 7e9481f1fb
4 changed files with 23 additions and 15 deletions

View File

@ -40,9 +40,6 @@
#include "qcocoadrag.h"
#include "qmacclipboard.h"
#include "qcocoahelpers.h"
#ifndef QT_NO_WIDGETS
#include <QtWidgets/qwidget.h>
#endif
#include <QtGui/private/qcoregraphics_p.h>
QT_BEGIN_NAMESPACE
@ -189,19 +186,17 @@ QPixmap QCocoaDrag::dragPixmap(QDrag *drag, QPoint &hotSpot) const
const int height = fm.height();
if (width > 0 && height > 0) {
qreal dpr = 1.0;
if (const QWindow *sourceWindow = qobject_cast<QWindow *>(drag->source())) {
dpr = sourceWindow->devicePixelRatio();
}
#ifndef QT_NO_WIDGETS
else if (const QWidget *sourceWidget = qobject_cast<QWidget *>(drag->source())) {
if (const QWindow *sourceWindow = sourceWidget->window()->windowHandle())
dpr = sourceWindow->devicePixelRatio();
}
#endif
else {
if (const QWindow *focusWindow = qApp->focusWindow())
dpr = focusWindow->devicePixelRatio();
QWindow *window = qobject_cast<QWindow *>(drag->source());
if (!window && drag->source()->metaObject()->indexOfMethod("_q_closestWindowHandle()") != -1) {
QMetaObject::invokeMethod(drag->source(), "_q_closestWindowHandle",
Q_RETURN_ARG(QWindow *, window));
}
if (!window)
window = qApp->focusWindow();
if (window)
dpr = window->devicePixelRatio();
pm = QPixmap(width * dpr, height * dpr);
pm.setDevicePixelRatio(dpr);
QPainter p(&pm);

View File

@ -1112,6 +1112,17 @@ QWindow *QWidgetPrivate::windowHandle(WindowHandleMode mode) const
return nullptr;
}
/*!
\internal
Used by clients outside of widgets to get a handle to the
closest QWindow without having to link to widgets.
*/
QWindow *QWidgetPrivate::_q_closestWindowHandle() const
{
return windowHandle(QWidgetPrivate::WindowHandleMode::Closest);
}
QScreen *QWidgetPrivate::associatedScreen() const
{
if (auto window = windowHandle(WindowHandleMode::Closest))

View File

@ -742,6 +742,7 @@ private:
private:
Q_DISABLE_COPY(QWidget)
Q_PRIVATE_SLOT(d_func(), void _q_showIfNotHidden())
Q_PRIVATE_SLOT(d_func(), QWindow *_q_closestWindowHandle())
QWidgetData *data;
};

View File

@ -270,6 +270,7 @@ public:
TopLevel
};
QWindow *windowHandle(WindowHandleMode mode = WindowHandleMode::Direct) const;
QWindow *_q_closestWindowHandle() const; // Private slot in QWidget
QScreen *associatedScreen() const;