Results 1 to 2 of 2

Thread: Length of text in QFontMetrics

  1. #1
    Join Date
    Jan 2014
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Length of text in QFontMetrics

    Hello,

    I'm trying to draw a rect around a text. I have the problem, that the rect is much too small in comparison to my text. I'm trying to get the length of the text with QFontMetrics. Here is a small example:

    Qt Code:
    1. QPainter p(this);
    2.  
    3. QFont font;
    4. font.setFamily("Arial");
    5. font.setPixelSize(10);
    6. font.setBold(false);
    7.  
    8. QFontMetricsF metrics(font);
    9.  
    10. qreal width = metrics.width("Test");
    11. p.drawRect(0,0,width, 20);
    12. p.drawText(QRect(0,0,width,20),"Test");
    13.  
    14. qreal width1 = metrics.width("LongTest");
    15. p.drawRect(0,20,width1, 20);
    16. p.drawText(QRect(0,20,width1,20),"LongTest");
    17.  
    18. qreal width2 = metrics.width("LongLongTest");
    19. p.drawRect(0,40,width2, 20);
    20. p.drawText(QRect(0,40,width2,20),"LongLongTest");
    To copy to clipboard, switch view to plain text mode 

    The result in a QWidget you can see here:
    Bildschirmfoto 2014-01-06 um 16.23.44.png

    I'm using Qt 5 in a Kubuntu 13.04 system. Do I do anything wrong or is it a (huge) bug in Qt? I tested it with different font families and sizes. The result is always the same.

    I hope you can help me!

    Greetz
    Screamer

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Length of text in QFontMetrics

    You could be using a different font for calculation than for painting.
    Either set the font on the painter or use the painter's font/fontmetrics

    Cheers,
    _

Similar Threads

  1. Stylesheet and QFontMetrics
    By ucomesdag in forum Qt Programming
    Replies: 7
    Last Post: 18th January 2010, 07:45
  2. QFontMetrics.height()
    By Caius Aérobus in forum Qt Programming
    Replies: 8
    Last Post: 4th April 2009, 17:36
  3. QFontMetrics and HTML tags
    By vonCZ in forum Newbie
    Replies: 1
    Last Post: 14th August 2008, 12:13
  4. Replies: 1
    Last Post: 30th November 2007, 10:03
  5. QFontMetrics, i can't understand
    By pakulo in forum Qt Programming
    Replies: 11
    Last Post: 24th June 2007, 17:59

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.