[Development] High-dpi Qt best practices

Olivier Goffart olivier at woboq.com
Tue Oct 9 11:32:17 CEST 2012


On Tuesday 09 October 2012 14:29:39 Pritam wrote:
> On Tuesday 09 October 2012 02:07 PM, Sorvig Morten wrote:
> > While preparing an upcoming blog entry I've collected some best practices
> > regarding raster graphics (QImage and QPixmap). These apply to internal
> > Qt development as well. The patch is still pending so they are open for
> > discussion.
> > 
> > (I use image and pixmap interchangeably here, most points apply to both)
> > 
> > * Start by providing high-resolution artwork. You can provide 2x versions
> > only and let Qt scale them down, or for best performance and image
> > quality provide pre-scaled or hand-made 1x and 2x versions.
> > 
> > * Set the QT_HIDPI_AWARE environment variable. (Changes the behaviour of
> > QIcon::pixmap)
> > 
> > • Use QIcon to manage the artwork versions. Use QIcon::pixmap(QWindow,
> > QSize) to get an appropriate pixmap for a given window and point size. If
> > you don’t know which window you are targeting you can use
> > QIcon::pixmap(QSize), which will work correctly on single-display systems
> > and and use the “best” display on  multiple-display systems.

But QSize is already the size in pixel.
Or do you mean that QIcon::pixmap could return a pixmap that is larger than 
the given QSize, scaled with some magic heuristics.  That is not really 
intuitive. (and violate the current documentation)


> > • Alternatively, manage the versions yourself. Set a dpi scale factor of 2
> > on the high-dpi version, either by appending "@2x" to the file name or by
> > calling QImage::setDpiScaleFactor(2) yourself. Use
> > QWindow::dpiScaleFactor() or qApp->dpiScaleFactor() to get the target
> > scale factor.
> > 
> > * Don’t scale pixmaps to specific pixel sizes unless you know what you are
> > doing.> 
> > * Don’t use pixmap sizes directly in layout calculations. Divide by the 
pixmap scale factor:
> >     QSize pointSze = pixmap.size() / pixmap.dpiScaleFactor();
> > 
> > * QPainter::drawPixmap(QPoint, ...) works correctly with high-dpi pixmaps
> > if the dpi scale factor is set, that is pixmaps from QIcon::pixmap(),
> > pixmaps loaded from “@2x” files, or pixmaps where you call
> > setDpiScaleFactor().
> > 
> > * QPainter::drawPixmap(QRect, ...) works correctly for all pixmaps and
> > will draw high-dpi content if the source pixmap provides enough pixels.
> > If you calculate the rect from the pixmap remember to use
> > dpiScaleFactor() as above.
> > 
> > * 2x pixmaps on a 2x high-dpi display is an optimized case in the raster
> > paint engine, similar to 1x pixmaps on “standard” displays.
> > 
> > * Most Qt API is in points (even QStyle::PixelMetric). The main exception
> > is image and pixmap geometry, which is in pixels.

> Forgive my ignorance, but I didn't understand. AFAIK most of Qt API is
> in pixels. Do you mean after this patch, one should treat all API as points?

I share the same question.


> > Next on my agenda is high-dpi OpenGL, scene graph and Qt Quick.

-- 
Olivier

Woboq - Qt services and support - http://woboq.com




More information about the Development mailing list