Improve QWidget::metric for PdmDevicePixelRatio.

Use QWindow::devicePixelRatio() which is the most
accurate devicePixelRatio accessor since it can ask
the platform native window directly

Fall back to qApp->devicePixelRatio() if the window
pointer is not valid.

Task-number: QTBUG-37606
Task-number: QTBUG-38078
Change-Id: Ief1468a0c6ced07439f55329ab056883016241cc
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
This commit is contained in:
Morten Johan Sørvig 2014-04-01 14:54:24 +02:00 committed by The Qt Project
parent 2884d7c9aa
commit 687fbc1152

View File

@ -834,13 +834,16 @@ int QWidget::metric(PaintDeviceMetric m) const
{ {
Q_D(const QWidget); Q_D(const QWidget);
QWindow *topLevelWindow = 0;
QScreen *screen = 0; QScreen *screen = 0;
if (QWidget *topLevel = window()) if (QWidget *topLevel = window())
if (QWindow *topLevelWindow = topLevel->windowHandle()) { topLevelWindow = topLevel->windowHandle();
QPlatformScreen *platformScreen = QPlatformScreen::platformScreenForWindow(topLevelWindow);
if (platformScreen) if (topLevelWindow) {
screen = platformScreen->screen(); QPlatformScreen *platformScreen = QPlatformScreen::platformScreenForWindow(topLevelWindow);
} if (platformScreen)
screen = platformScreen->screen();
}
if (!screen && QGuiApplication::primaryScreen()) if (!screen && QGuiApplication::primaryScreen())
screen = QGuiApplication::primaryScreen(); screen = QGuiApplication::primaryScreen();
@ -877,7 +880,7 @@ int QWidget::metric(PaintDeviceMetric m) const
} else if (m == PdmPhysicalDpiY) { } else if (m == PdmPhysicalDpiY) {
return qRound(screen->physicalDotsPerInchY()); return qRound(screen->physicalDotsPerInchY());
} else if (m == PdmDevicePixelRatio) { } else if (m == PdmDevicePixelRatio) {
return screen->devicePixelRatio(); return topLevelWindow ? topLevelWindow->devicePixelRatio() : qApp->devicePixelRatio();
} else { } else {
val = QPaintDevice::metric(m);// XXX val = QPaintDevice::metric(m);// XXX
} }