diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2019-04-04 13:53:19 +0200 |
---|---|---|
committer | Morten Johan Sørvig <morten.sorvig@qt.io> | 2019-08-23 01:41:33 +0000 |
commit | d603ee689f0e3fdcfa3230b3d75cdce6c5af05c1 (patch) | |
tree | ffc037205f5bdad40adfca36abde8f7676832b3e /src/widgets/styles/qstylehelper.cpp | |
parent | f1e40dd6d6968c59885231f61f94787abd4cf783 (diff) |
Widget style: Use per-screen DPI in QStyleHelper::dpiScaled()
Pass the style option to dpiScaled() in order to get
the correct screen DPI. The style option contains the
font, which again contains the current DPI value.
Add QFontMetrics::fontDpi() accessors to get the DPI
from the QFont. This DPI will/should be updated on screen
change.
Replace hardcoded Q_OS_MAC DPI with hardcoded base
DPI. This makes per-screen DPI testable on macOS, too.
Task-number: QTBUG-45055
Change-Id: I75f8b37d45eb50c3334b46b8469a546d29712f1b
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Diffstat (limited to 'src/widgets/styles/qstylehelper.cpp')
-rw-r--r-- | src/widgets/styles/qstylehelper.cpp | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/src/widgets/styles/qstylehelper.cpp b/src/widgets/styles/qstylehelper.cpp index 0b910d46df..4e61b2d1ec 100644 --- a/src/widgets/styles/qstylehelper.cpp +++ b/src/widgets/styles/qstylehelper.cpp @@ -40,6 +40,8 @@ #include <qstyleoption.h> #include <qpainter.h> #include <qpixmapcache.h> +#include <private/qhighdpiscaling_p.h> +#include <private/qguiapplication_p.h> #include <private/qmath_p.h> #include <private/qstyle_p.h> #include <qmath.h> @@ -49,6 +51,7 @@ #include <qabstractscrollarea.h> #include <qwindow.h> +#include <qmetaobject.h> #include "qstylehelper_p.h" #include <qstringbuilder.h> @@ -79,15 +82,41 @@ QString uniqueName(const QString &key, const QStyleOption *option, const QSize & return tmp; } -qreal dpiScaled(qreal value) -{ -#ifdef Q_OS_MAC - // On mac the DPI is always 72 so we should not scale it - return value; +#ifdef Q_OS_DARWIN +static const qreal qstyleBaseDpi = 72; #else - static const qreal scale = qreal(qt_defaultDpiX()) / 96.0; - return value * scale; +static const qreal qstyleBaseDpi = 96; +#endif + +Q_WIDGETS_EXPORT qreal dpi(const QStyleOption *option) +{ +#ifndef Q_OS_DARWIN + // Prioritize the application override, except for on macOS where + // we have historically not supported the AA_Use96Dpi flag. + if (QCoreApplication::testAttribute(Qt::AA_Use96Dpi)) + return 96; #endif + + // Expect that QStyleOption::QFontMetrics::QFont has the correct DPI set + if (option) + return option->fontMetrics.fontDpi(); + + return qstyleBaseDpi; +} + +Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, qreal dpi) +{ + return value * dpi / qstyleBaseDpi; +} + +Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, const QPaintDevice *device) +{ + return dpiScaled(value, device->logicalDpiX()); +} + +Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, const QStyleOption *option) +{ + return dpiScaled(value, dpi(option)); } #ifndef QT_NO_ACCESSIBILITY |