summaryrefslogtreecommitdiffstats
path: root/src/widgets/styles/qwindowsstyle.cpp
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2016-07-18 10:53:41 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2016-07-27 17:20:51 +0000
commit29c6e39086831f6811e94364273c1f4bff119bef (patch)
tree4a4d36af0d994e72a59e39d88c1eb7dc4e9804bf /src/widgets/styles/qwindowsstyle.cpp
parent0e40781c1637f575acc842044b67da412c8e2026 (diff)
Windows style: Scale native metrics per monitor
The native sizes returned by the metrics and theme functions refer to the primary monitor. They need adaption when showing on a secondary monitor with differing logical DPI. Introduce a helper function QWindowsStylePrivate::nativeMetricScaleFactor() to calculate the total factor. Task-number: QTBUG-49374 Change-Id: I34c843ff34108424e1ef0aafcf9f563d17ebbc89 Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Diffstat (limited to 'src/widgets/styles/qwindowsstyle.cpp')
-rw-r--r--src/widgets/styles/qwindowsstyle.cpp47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/widgets/styles/qwindowsstyle.cpp b/src/widgets/styles/qwindowsstyle.cpp
index 536ef2d49a..a0830a1f92 100644
--- a/src/widgets/styles/qwindowsstyle.cpp
+++ b/src/widgets/styles/qwindowsstyle.cpp
@@ -59,8 +59,12 @@
#include "qlistview.h"
#include <private/qmath_p.h>
#include <qmath.h>
+#include <QtGui/qscreen.h>
+#include <QtGui/qwindow.h>
#include <qpa/qplatformtheme.h>
+#include <qpa/qplatformscreen.h>
#include <private/qguiapplication_p.h>
+#include <private/qhighdpiscaling_p.h>
#include <private/qstylehelper_p.h>
#include <private/qstyleanimation_p.h>
@@ -395,6 +399,47 @@ int QWindowsStylePrivate::fixedPixelMetric(QStyle::PixelMetric pm)
return QWindowsStylePrivate::InvalidMetric;
}
+static QWindow *windowOf(const QWidget *w)
+{
+ QWindow *result = Q_NULLPTR;
+ if (w) {
+ result = w->windowHandle();
+ if (!result) {
+ if (const QWidget *np = w->nativeParentWidget())
+ result = np->windowHandle();
+ }
+ }
+ return result;
+}
+
+static QScreen *screenOf(const QWidget *w)
+{
+ if (const QWindow *window = windowOf(w))
+ return window->screen();
+ return QGuiApplication::primaryScreen();
+}
+
+// Calculate the overall scale factor to obtain Qt Device Independent
+// Pixels from a native Windows size. Divide by devicePixelRatio
+// and account for secondary screens with differing logical DPI.
+qreal QWindowsStylePrivate::nativeMetricScaleFactor(const QWidget *widget)
+{
+ if (!QHighDpiScaling::isActive())
+ return 1;
+ qreal result = qreal(1) / QWindowsStylePrivate::devicePixelRatio(widget);
+ if (QGuiApplicationPrivate::screen_list.size() > 1) {
+ const QScreen *primaryScreen = QGuiApplication::primaryScreen();
+ const QScreen *screen = screenOf(widget);
+ if (screen != primaryScreen) {
+ const qreal primaryLogicalDpi = primaryScreen->handle()->logicalDpi().first;
+ const qreal logicalDpi = screen->handle()->logicalDpi().first;
+ if (!qFuzzyCompare(primaryLogicalDpi, logicalDpi))
+ result *= logicalDpi / primaryLogicalDpi;
+ }
+ }
+ return result;
+}
+
/*!
\reimp
*/
@@ -402,7 +447,7 @@ int QWindowsStyle::pixelMetric(PixelMetric pm, const QStyleOption *opt, const QW
{
int ret = QWindowsStylePrivate::pixelMetricFromSystemDp(pm, opt, widget);
if (ret != QWindowsStylePrivate::InvalidMetric)
- return qRound(qreal(ret) / QWindowsStylePrivate::devicePixelRatio(widget));
+ return qRound(qreal(ret) * QWindowsStylePrivate::nativeMetricScaleFactor(widget));
ret = QWindowsStylePrivate::fixedPixelMetric(pm);
if (ret != QWindowsStylePrivate::InvalidMetric)