qtscalingdpiqfontmetricsqfont

Can QFontMetrics account for windows dpi scaling?


I was under the impression that if you do this in your application

QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication* app = new QApplication(temp, NULL);

then Fonts gets automatically scaled up on high resolution display. Same if you explicitly increase font scaling in Windows 10 (Settings-->System->Custom Scaling).

However, when running the following code with 100% and then 200% scaling in Windows 10, it does not return doubled size.

QFont font = QFont("arial", 10);
QFontMetrics fm(font);
int width = fm.width("abcdefgABCDEFG");

Strangely there is only 1 pixel difference.

100% --> width = 108 pixels
200% --> width = 109 pixels

Why is that? Can I get QFontMetrics to account for Windows scaling? Or do I need to use Logical / Physical DPI to deduce that font size must be increased by a factor 2?

Thanks


Solution

  • For the proper scaling of custom-drawn items use QScreen::physicalDotPerInch property to realize the scaling coefficient to apply to actual drawings:

    qreal myScale = pScreen->physicalDotPerInch() / constStandardPerInch;
    

    P.S. The question still needs to be revised.