Search code examples
c++qtqwidgetqfontmetrics

How to interpret QFontMetrics results?


I have a problem making sense of the values I get from QFontMetrics

// 43 characters        0123456789012345678901234567890123456789012
static const QString s("The quick brown fox jumps over the lazy dog");
// Hint: basically returns w->fontMetrics(); with w being my main application window
const QFontMetrics fm = CGuiUtility::currentFontMetrics();
const int w = fm.width(s);
const int h = fm.height();
return QSize(w, h);

With the following screen resolutions I get:

  1. ) 1920/1080: 256/16
  2. ) 3840/2160: 178/10 hi DPI support qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1")
  3. ) 3840/2160: 536/32 no hi DPI support

My understanding is that I do get pixel width/height. I wonder why I get a so much smaller height with a hires resolution (see 2). I would rather expect it the other way around - using more pixels.

What I can see is that there is (almost) factor 2 between 1 and 3, which makes sense. But then (using 3, no DPI support) the UI is unreadable (too small because of hi DPI screen). So how does 2 fit in, which has some odd results.

Is anybody able to give a hint on the interpretation of those values?

Remark: Related to Style sheets / Qt Designer support for high dpi screens? What I try to find is a reasonable width/height for my UI window on different resolutions/platforms


Good comments, I see. With hi-dpi Qt scales 1:3 using a "virtual screen" of 1280/780. I wonder if I can adjust that pixel ratio manually. In my very case it happens by setting qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1"). Is there a chance to set it to 2:1?

  1. ) "Desktop w1920 w1080 - ratio: 1 | 80 chars: w560 h16 | 43 chars: w256 h16"
  2. ) "Desktop w1280 w720 hi DPI ratio: 3 | 80 chars: w400 h10 | 43 chars: w178 h10"
  3. ) "Desktop w3840 w2160 - ratio: 1 | 80 chars: w1200 h32 | 43 chars: w536 h32"

Solution

  • Based on the hints of AlexanderVX and SteackOverflow it is clear now.

    1. "Desktop w1920 w1080 - ratio: 1 | 80 chars: w560 h16 | 43 chars: w256 h16"
    2. "Desktop w1280 w720 hi DPI ratio: 3 | 80 chars: w400 h10 | 43 chars: w178 h10"
    3. "Desktop w3840 w2160 - ratio: 1 | 80 chars: w1200 h32 | 43 chars: w536 h32"

    Therefor I close the topiy