From 19950b3267752fbde4a8845bd203172b43fbaffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Wed, 23 Nov 2016 15:16:09 +0100 Subject: [PATCH] QGraphicsView: Create high-dpi background pixmap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Scale pixmap size by the viewport devicePixelRatio, and make sure scrolling deltas and expose regions are correctly scaled. Change-Id: Ibeac34c5ecd531ca7c09802f0b5e1f45b8e31b65 Reviewed-by: Friedemann Kleint Reviewed-by: Morten Johan Sørvig --- src/widgets/graphicsview/qgraphicsview.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/widgets/graphicsview/qgraphicsview.cpp b/src/widgets/graphicsview/qgraphicsview.cpp index 705d5a0f4dd..1320545748e 100644 --- a/src/widgets/graphicsview/qgraphicsview.cpp +++ b/src/widgets/graphicsview/qgraphicsview.cpp @@ -3483,7 +3483,9 @@ void QGraphicsView::paintEvent(QPaintEvent *event) // Recreate the background pixmap, and flag the whole background as // exposed. if (d->mustResizeBackgroundPixmap) { - d->backgroundPixmap = QPixmap(viewport()->size()); + const qreal dpr = d->viewport->devicePixelRatioF(); + d->backgroundPixmap = QPixmap(viewport()->size() * dpr); + d->backgroundPixmap.setDevicePixelRatio(dpr); QBrush bgBrush = viewport()->palette().brush(viewport()->backgroundRole()); if (!bgBrush.isOpaque()) d->backgroundPixmap.fill(Qt::transparent); @@ -3679,14 +3681,20 @@ void QGraphicsView::scrollContentsBy(int dx, int dy) && X11->use_xrender #endif ) { + // Below, QPixmap::scroll() works in device pixels, while the delta values + // and backgroundPixmapExposed are in device independent pixels. + const qreal dpr = d->backgroundPixmap.devicePixelRatio(); + const qreal inverseDpr = qreal(1) / dpr; + // Scroll the background pixmap QRegion exposed; if (!d->backgroundPixmap.isNull()) - d->backgroundPixmap.scroll(dx, dy, d->backgroundPixmap.rect(), &exposed); + d->backgroundPixmap.scroll(dx * dpr, dy * dpr, d->backgroundPixmap.rect(), &exposed); // Invalidate the background pixmap d->backgroundPixmapExposed.translate(dx, dy); - d->backgroundPixmapExposed += exposed; + const QRegion exposedScaled = QTransform::fromScale(inverseDpr, inverseDpr).map(exposed); + d->backgroundPixmapExposed += exposedScaled; } // Always replay on scroll.