Results 1 to 3 of 3

Thread: Font size in percentage

  1. #1
    Join Date
    Oct 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Font size in percentage

    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.

  2. #2
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Font size in percentage

    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:
    Qt Code:
    1. QFontMetrics fm( widget->font() );
    2. fm.width("Hello");
    To copy to clipboard, switch view to plain text mode 
    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.

  3. #3
    Join Date
    Oct 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Font size in percentage

    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.

Similar Threads

  1. Font Height and width based on font size
    By Ghufran in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2010, 08:02
  2. Replies: 6
    Last Post: 27th July 2010, 21:07
  3. QStyleSheet font-size as percentage?
    By dhalbert in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2010, 06:00
  4. adjust font size to QLabel-size
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2008, 14:33
  5. change font size and button size of QMessageBox
    By nass in forum Qt Programming
    Replies: 6
    Last Post: 13th September 2006, 19:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.