Results 1 to 2 of 2

Thread: QFontMetrics boundingRect vs horizontalAdvance with monospaced fonts

  1. #1
    Join Date
    Jul 2012
    Thanked 14 Times in 13 Posts
    Qt products

    Default QFontMetrics boundingRect vs horizontalAdvance with monospaced fonts


    I am trying to understand the differences between

    fontmetrics.horizontalAdvance(str) and fontmetrics.boundingRect(str).width().

    I understand it's related to kerning, but with my experiments, the bounding rect just doesnt "bound" the string.
    Also, with monospaced fonts kerning should not be a concern, right?

    Consider this code:

    Qt Code:
    1. {
    2. QFont fixedfont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
    3. fixedfont.setPointSize(9);
    4. QFontMetrics fontmetrics = QFontMetrics(fixedfont);
    5. QString str = "abcdefghijk 0123456789";
    7. int a = fontmetrics.horizontalAdvance(str);
    8. int b = fontmetrics.boundingRect(str).width();
    9. int c = fontmetrics.averageCharWidth() * str.length();
    10. assert(b == c);
    12. QPixmap pixmap(QSize(b, fontmetrics.height()));
    13. pixmap.fill(Qt::white);
    14. QPainter painter(&pixmap);
    15. painter.setFont(fixedfont);
    16. painter.drawText(QPoint(0,fontmetrics.height()), str);
    17."fontm.bmp", "bmp");
    19. }
    To copy to clipboard, switch view to plain text mode 

    Whereas horizontalAdvance gives the correct result:


    boundingRect.width() truncates the text:


    I would understand if HA had some extra space at the end. But as is, BR just doesnt bound the text. Why not?

    In other words, I dont understand why with a fixed-width font, averageCharWidth * str.length() doesnt equal the pixels the string occupies when drawn at (0,0).

  2. #2
    Join Date
    Mar 2009
    Brisbane, Australia
    Thanked 1,608 Times in 1,535 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows
    Wiki edits

    Default Re: QFontMetrics boundingRect vs horizontalAdvance with monospaced fonts

    QFontMetrics::boundingRect() is returning a rectangle width from the leftmost black pixel to the rightmost black pixel (Assuming a black pen).

    The horizontalAdvance includes the whitespace that precedes/follows the end characters when rendered.
    If you use QPainter::boundingRect() you will get a rectangle that contains the string neatly

    The averageCharWidth() is that of all the glyphs in the font (not just the ones in your string), looks to be the rendered pixels only, and may be subject to rounding.

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

    tuli (9th May 2021)

Similar Threads

  1. Replies: 1
    Last Post: 22nd April 2014, 15:58
  2. Adding Arabic fonts and CJK (China Japan Korean) fonts
    By kishore7771 in forum Qt Programming
    Replies: 0
    Last Post: 30th July 2013, 18:33
  3. Replies: 2
    Last Post: 27th February 2013, 12:24
  4. A crash in QFontMetrics::boundingRect()
    By npirocanac in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 5th February 2013, 11:47
  5. How to specify QRect argument to QFontMetrics::boundingRect()
    By John Mcgehee in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2010, 04:50

Tags for this Thread


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.