3

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.

I would appreciate any advice.

p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
Alan Spark
  • 8,152
  • 8
  • 56
  • 91
  • You could start by reading http://doc.qt.io/qt-5/highdpi.html – Mark Ransom May 03 '18 at 14:39
  • Thanks, I have been but I don't think it describes exactly what I'm trying to achieve with regard to images and system scale factor. – Alan Spark May 03 '18 at 14:49
  • I was afraid of that, that's why I said "start". I guess I was overly optimistic, you certainly can't be the first to have this problem. – Mark Ransom May 03 '18 at 15:21
  • If you are using Qt 5.6 or greater, you can try setting the `Qt::AA_EnableHighDpiScaling` attribute on your `QApplication`. – RA. May 03 '18 at 17:59

2 Answers2

3

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).

Sergio Monteleone
  • 2,776
  • 9
  • 21
  • Thanks, it looks like computing the scale factor might be the answer. I'm having issues with the whole window (including title bar and close button) doubling in size when dragged to my second monitor which is standard DPI. I think this may be better covered in another question so I'm marking yours as the answer. – Alan Spark May 04 '18 at 11:20
  • My pleasure. That is a known limitation on Windows. It should resize correctly once the window is entirely on the other screen though. – Sergio Monteleone May 04 '18 at 11:31
2

About high DPI screens: I think you should look at the QScreen::devicePixelRatio property, which

holds the screen's ratio between physical pixels and device-independent pixels

and use the returned value to set the pixmap ratio with QPixmap::setDevicePixelRatio.

For example:

#include <QApplication>
#include <QDebug>
#include <QScreen>
#include <QPixmap>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QPixmap p(32, 32);
    QScreen * screen = a.primaryScreen();
    p.setDevicePixelRatio(screen->devicePixelRatio());

    //...

    return a.exec();
}

About system scaling, you can have a clue testing the QScreen::logicalDotsPerInch property, e.g.:

int scaling_percent = (screen->logicalDotsPerInch() / 96) * 100;
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
  • Thanks, I think computing the scale factor like this is the only it can be done. Yours and Sergio's answer was very helpful. – Alan Spark May 04 '18 at 11:21