Results 1 to 8 of 8

Thread: Drawing Text

  1. #1
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Drawing Text

    Hi,
    I have QtEmbedded 4.4.1. I am trying to draw text within a rectangle (as per the picture), however, there is too much spacing between the lines. I know this is probably related to the QFontMetrics::lineSpacing().
    I have a button rectangle, and a text rectangle, I am drawing the text rectangle inside the button rectangle. The text rectangle coordinates are relative to the button coordinates, not the screen coordinates, that is why I have to draw an adjusted button rectangle.
    Anyways, this works, but the font display spacing is too large, and I must also consider different translations ( accents on the chars, etc).
    image.png is actual
    spec.png is required

    Qt Code:
    1. QFontMetrics fm(txtFont);
    2. qDebug() << fm.lineSpacing() << fm.leading() << fm.height();
    3. QRectF adjRect(rect().adjusted(txtRect.x(),txtRect.y(),
    4. -txtRect.x(), -(rect().bottom() - (rect().y()+txtRect.y()+txtRect.height()))));
    5. painter->drawText(adjRect,Qt::AlignCenter|Qt::TextWordWrap,messageLabel);
    6. painter->drawRect(adjRect); // for debugging
    To copy to clipboard, switch view to plain text mode 

    1) Can I reduce the line spacing? (I would like to increase the font size)
    2) Is this the best method to draw text taking into consideration other languages? I worry that the top line will cut-off accents.
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing Text

    Quote Originally Posted by QbelcorT View Post
    1) Can I reduce the line spacing? (I would like to increase the font size)
    I don't think you can. Unless you use another font with a smaller leading value.

    2) Is this the best method to draw text taking into consideration other languages? I worry that the top line will cut-off accents.
    It's best to ask the font metrics for the geometry of a text you want drawn. QFontMetrics::boundingRect() will help you do that.
    Last edited by wysota; 7th March 2009 at 12:15. Reason: Removed redundant tag
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    QbelcorT (7th March 2009)

  4. #3
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Drawing Text

    thanks for the advice.
    I am using "Times" or "Helvetica". The leading value is always 0 or in some cases -1, it's the linespacing that changes. I thought I would include this if it gives you another idea.

    For example
    Times @ font size 15, line spacing = 17
    Times @ font size 16, line spacing = 23
    Helvetica @ font size 17, line spacing = 23
    Helvetica @ font size 16, line spacing = 22

    edit: I guess using BoundingRect will be difficult if I am word wrapping..?
    sorry, edit again.. I tried BoundingRect, seems ok regarding the wordwrapping.. still don't like the linespacing.
    Attached Images Attached Images
    Last edited by QbelcorT; 7th March 2009 at 09:52.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing Text

    Line spacing = leading + height. Note that "height" is not the point size of the font but rather ascent+descent+1.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Drawing Text

    Hi,
    I made an error yesterday when drawing the text.I forgot the draw the text in the font bounding rect.
    I had this, and it was working even when the text was too long. However this is incorrect since it isn't using the boundRectText.
    Qt Code:
    1. painter->drawText(adjRect,Qt::AlignCenter|Qt::TextWordWrap,messageLabel);
    To copy to clipboard, switch view to plain text mode 

    I changed the line to this and now the bounding rect expands whenever the text is expanded. It doesn't constrain to the adjRect.
    Qt Code:
    1. painter->drawText(boundRectText,Qt::AlignCenter|Qt::TextWordWrap,messageLabel);
    To copy to clipboard, switch view to plain text mode 


    Here is my code.
    Qt Code:
    1. QFontMetrics fm(txtFont);
    2. QRectF adjRect(rect().adjusted(txtRect.x(),txtRect.y(),-txtRect.x(), -(rect().bottom() - (rect().y()+txtRect.y()+txtRect.height()))));
    3. QRect boundRectText(fm.boundingRect(adjRect.toRect(),Qt::AlignCenter|Qt::TextWordWrap,messageLabel));
    4. painter->drawText(boundRectText,Qt::AlignCenter|Qt::TextWordWrap,messageLabel);
    5. painter->drawRect(adjRect); // debugging
    6. painter->drawRect(boundRectText); // debugging
    To copy to clipboard, switch view to plain text mode 

    Here is a debug of the size of rectangles
    QRect(2,127 473x40) = adjRect = good
    QRect(-190,122 857x50) = boundRectText = bad. I expected this to be constrained to adjRect.
    Is there something I am doing wrong?
    Thanks

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing Text

    If the text doesn't fit, the rect you pass will not be respected. You have to pass a rectangle capable of holding the text or larger. That's why we have QFontMetrics::boundingRect in the first place. It makes it possible to adjust the font if you see the text won't fit into the desired area.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Drawing Text

    OK thanks, I understand, I need to put a restriction on the text length for translations. I would rather have the text cut-off then overwrite the button.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing Text

    You can elide the text if it is too long, that's what is usually done in such cases.

    By the way, you can probably solve your problems by using QTextLayout instead of QPainter::drawText(). It's more difficult in use but gives you more control over the layout (line spacing included).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 2
    Last Post: 23rd July 2012, 08:42
  3. Drawing Rich Formatted Text with QPainter
    By millsks in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2009, 19:59
  4. Problems with QString
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2008, 08:18
  5. Drawing antialiased text on Windows very slow
    By Rawk in forum Qt Programming
    Replies: 13
    Last Post: 14th May 2007, 14:13

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.