0

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

Octo
  • 543
  • 1
  • 5
  • 16
  • It is just about the mode your Qt app preconditioned to run in: https://stackoverflow.com/questions/39823918/how-to-approach-development-of-new-qt-5-7-high-dpi-per-monitor-dpi-aware-applic/39824445#39824445 – Alexander V Nov 06 '17 at 19:33
  • Right @AlexanderVX, I should mention I enabled auto HighDPI scaling – Octo Nov 06 '17 at 19:37
  • Then the question "Why is that? Do I need to compute the scaled size of the text myself?" answered? No, the OS is taking care of proper scaling. Don't bother then. Do you have another question? – Alexander V Nov 06 '17 at 19:45
  • It does scale up everything that is drawn by Qt (labels, comboboxes, tableviews...). But my application also include an OpenGL widget in wich we draw labels. We use a QPainter to draw text in an image. And although the whole application fonts seems to double when i increase the scaling, those labels generated with QPainter do not. – Octo Nov 06 '17 at 19:50
  • It makes sense to rephrase the question then? There is maybe a solution but I cannot answer it as is about the font metrics. – Alexander V Nov 06 '17 at 20:12
  • ok re-phrasing... – Octo Nov 06 '17 at 20:18
  • 1
    Check if `QScreen::physicalDotsPerInch` changes proportionally to actual resolution. I now am on Linux system without proper High Dpi support but that should work. That may depend on Qt platform implementation. – Alexander V Nov 06 '17 at 20:18
  • It does, [scaling 100%, Physical DPI = 92, Logical DPI = 96] [scaling 200%, Physical DPI = 46, Logical DPI = 96] I guess i could use this to compute the increased font size. I am just a bit disappointed, I was hoping QFontMetrics had a way of specifying it. – Octo Nov 06 '17 at 20:31

1 Answers1

1

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.

Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • 1
    Answer you gave is wrong. physicalDotsPerInch() is constant and depends on what video driver is reporting about monitor size. It order to find scaling factor you need to use logicalDotsPerInch() divided by 96 DPI. 96 DPI was chosen as 100% scale for historical reasons (I'm guessing - by Microsoft) and today has nothing to do with real DPI. Since Qt5.4 there is an option to inform windows manager that your app understands display scaling. Today it is default value and fonts are scaled properly. In 5.9 QFontMetrics::width returns proper pixel values. – Mariusz Zieliński Dec 06 '17 at 15:45
  • [QT article](http://doc.qt.io/qt-5/highdpi.html), [MSDN article](https://msdn.microsoft.com/en-us/library/windows/desktop/mt843498%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396) I don't want to go and edit your answer, please for correctness do it yourself. – Mariusz Zieliński Dec 06 '17 at 15:49
  • Stating that System is scaling fonts is also vague - it is job of font renderer and it does not need to be system one. For completeness 72.272 points = 254 mm = 1'. I was not able to figure DPI scale used to render fonts on the screen but it is for sure using logicalDPI as part of the equation. – Mariusz Zieliński Dec 06 '17 at 16:29