Results 1 to 3 of 3

Thread: Size in pixels of string in QLabel

  1. #1
    Join Date
    May 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Size in pixels of string in QLabel

    Hello!

    Is it possible to compute the width and height of the text in a QLabel? This size depends on the string contents and the font(s) used in QLabel.

    I am adding a dynamic toolbar to my application, and all controls have a QLabel with the name of the tool, and then the control itself... for example:

    Brightness: [ 0.0 ] Contrast: [ 0.0 ]

    And I need to find the exact size in pixels of the "Brightness:" and "Contrast:" text as shown in screen to know how big my QLabel needs to be. Right now I'm doing this using trial and error.

    Any ideas?

    Thanks!

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Size in pixels of string in QLabel

    QFontMetrics will do it: http://doc.trolltech.com/latest/qfontmetrics.html

    Example from that page:

    Qt Code:
    1. QFont font("times", 24);
    2. QFontMetrics fm(font);
    3. int pixelsWide = fm.width("What's the width of this text?");
    4. int pixelsHigh = fm.height();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to mvuori for this useful post:

    Mankua (12th May 2011)

  4. #3
    Join Date
    May 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Size in pixels of string in QLabel

    Wow... Works great... better than my wildest dream!
    Thank you!

Similar Threads

  1. Qt Designer How to mark a string in Qt Desginer (e.g. a QLabel) as not translatable
    By sebastian.zenker in forum Qt Tools
    Replies: 1
    Last Post: 12th August 2010, 22:42
  2. Replies: 1
    Last Post: 2nd August 2008, 15:46
  3. adjust font size to QLabel-size
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2008, 14:33
  4. Qlabel size string too long
    By Pharell in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2008, 15:20
  5. Real size in pixels of a widget
    By yosefm in forum Qt Programming
    Replies: 3
    Last Post: 27th October 2007, 16:01

Tags for this Thread

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.