c++windowsqtscalinghighdpi

How to scale images for high DPI in Qt5 properly?


We have various images in our application (displayed from a QPixmap in a QLabel) that appear at a reasonable size on non-high DPI screens but are shrunken on high DPI. The other UI elements look fine.

I've looked at the devicePixelRatioF() function, but it always returns 1. On my system I have 150% scaling enabled, so if I hardcode 1.5 as the scale factor then the images are of the expected dimensions relative to the window. The question is, how can I get the system scale factor so that I can make the application look consistent across systems with different DPI / scaling? As well as Windows, the application is built for Linux.


Solution

  • This issue beat me as well. It is also reported in Qt's bug tracker here.

    The problem is that 150% scaling on Windows is not performed by scaling all the pixels by a factor of 1.5, but rather scaling the fonts and "adjusting" the UI accordingly. That is why spacing and layouts look very weird when a 150% scaling is selected.

    Since devicePixelRatio() queries the actual pixel ratio, and this is unchanged when setting it to 150% on Windows, it still returns 1.

    If you really need the 1.5 value to scale your pixmaps correctly, you can compute the actual factor yourself by querying the screen's DPI (have a look at the documentation here).