[Development] High-dpi Qt best practices

Ziller Eike Eike.Ziller at digia.com
Tue Oct 9 11:31:27 CEST 2012

On 9 Oct 2012, at 10:59, Pritam <pritam_ghanghas at infosys.com> 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)

Afaik setting environment variables is prohibited / not working e.g. on iOS, so it would be good to have additional means to achieve this.

>> • 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.
>> • 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?

On Mac all Qt API is in "points" already before this patch.

>> Next on my agenda is high-dpi OpenGL, scene graph and Qt Quick.
>> - Morten
>> _______________________________________________
>> Development mailing list
>> Development at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/development
> -- 
> Regards,
> Pritam
> **************** CAUTION - Disclaimer *****************
> This e-mail contains PRIVILEGED AND CONFIDENTIAL INFORMATION intended solely 
> for the use of the addressee(s). If you are not the intended recipient, please 
> notify the sender by e-mail and delete the original message. Further, you are not 
> to copy, disclose, or distribute this e-mail or its contents to any other person and 
> any such actions are unlawful. This e-mail may contain viruses. Infosys has taken 
> every reasonable precaution to minimize this risk, but is not liable for any damage 
> you may sustain as a result of any virus in this e-mail. You should carry out your 
> own virus checks before opening the e-mail or attachment. Infosys reserves the 
> right to monitor and review the content of all messages sent to or from this e-mail 
> address. Messages sent to or from this e-mail address may be stored on the 
> Infosys e-mail system.
> ***INFOSYS******** End of Disclaimer ********INFOSYS***
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development

Eike Ziller, Senior Software Engineer - Digia, Qt
Digia Germany GmbH, Rudower Chaussee 13, D-12489 Berlin
Geschäftsführer: Mika Pälsi, Juha Varelius, Anja Wasenius
Sitz der Gesellschaft: Berlin, Registergericht: Amtsgericht Charlottenburg, HRB 144331 B

More information about the Development mailing list