PDA

View Full Version : Font size in percentage



pssss
9th September 2011, 15:28
How can I specify a font size for text that will take the same proportion of space regardless of screen resolution? For example I'd like the word "hello" to take half the screen's width, on an 800x600 screen and on a 1200x800 screen.

Point size doesn't seem to change the text's size when the width is smaller, is there something else I must do for point size to behave like that?

Thank you very much.

raven-worx
9th September 2011, 15:41
Font-sizes only affect the text in height. The width depends on the text itself with the given font-size.
To determine the width of a given text you can use the following:


QFontMetrics fm( widget->font() );
fm.width("Hello");

QFontMetrics::width() will return you the actual width of the given text on the screen rendered with the given font.
To get the screen resolution you can use QDesktopWidget.

pssss
9th September 2011, 16:27
Thanks, it's a bit harder than that because the labels use word wrap, can I use QFontMetrics to find out if certain text fits inside a boundingRect (in this case the QLabel's width and height) and word wrap set to true?

But for that I would still have to reimplement resizeEvent() on the parent widget. I was wondering if Qt handled this kind of thing automatically with a unit for font size like % in web development. I thought em or pt would do the trick but like px they don't adjust when the screen resolution changes.


Thanks again.