Results 1 to 7 of 7

Thread: How draw a rotated text in a QPainterPath?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2007
    Posts
    35
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

    Thumbs up Re: How draw a rotated text in a QPainterPath?

    Thank you very much darksaga!!!
    This was exactly what I was looking for!

    Now the rotated rectangle is no more a problem.
    Probably I can use the same way to make the text fit the rectangle: I could draw a text in a generic size (say 10 pts), than I can scale it's painter path in both x and y to fill the rectangle.
    I'll post if it works or not :-)

  2. #2
    Join Date
    Jul 2007
    Posts
    35
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: How draw a rotated text in a QPainterPath?

    Thank you again because I solved the other problem too, in fact I could scale and rotate the text perfectly!
    I just draw the text with the default size and then I scale it depending on the rectangle size.

    Just I changed the rectangle mapping from a QPainterPath to a QPolygonF, because I think it is faster (QPainterPath handles curves too, while QPolygonF handles just points). I converted it to a QPainterPath after the mapping.

    Here is the code (may be it can be useful to someone else):
    Qt Code:
    1. {
    2. // Rectangle mapping
    3.  
    4. QLineF rectBase = QLineF(begin, end);
    5. qreal angle = rectBase.angle(QLineF(QPointF(0, 0), QPointF(1, 0)));
    6. QMatrix rotationMatrix;
    7. rotationMatrix.translate(begin.x(), begin.y());
    8. rotationMatrix.rotate(angle);
    9. path.addPolygon(rotationMatrix.map(QPolygonF(textRectangle)));
    10.  
    11. // Text mapping
    12.  
    13. QFont font = QFont("courier");
    14. // Take text size
    15. QFontMetricsF fm(font);
    16. QSizeF textSize = fm.size(0, text);
    17. // Calculate how much to scale the text to fit the rectangle
    18. qreal scaleX = textRectangle.width() / textSize.width();
    19. qreal scaleY = textRectangle.height() / textSize.height();
    20. // Apply the scale factors
    21. rotationMatrix.scale(scaleX, -scaleY);
    22. // Draw the text
    23. QPainterPath textPath;
    24. textPath.addText(QPointF(0, -fm.descent()), font, text);
    25. path.addPath(rotationMatrix.map(textPath));
    26. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2007
    Posts
    35
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: How draw a rotated text in a QPainterPath?

    I also added a little border to not have the text touching the rectangle:
    Qt Code:
    1. {
    2. // [...]
    3.  
    4. // Leave a 1 pixel wide border around the text
    5. const qreal borderSize = 1;
    6. const qreal doubleBorder = borderSize * 2;
    7. // Calculate how much to scale the text to fit the rectangle
    8. qreal scaleX = textRectangle.width() / (textSize.width() + doubleBorder);
    9. qreal scaleY = textRectangle.height() / (textSize.height() + doubleBorder);
    10. // Apply the scale factors
    11. rotationMatrix.scale(scaleX, -scaleY);
    12. // Center the text (because of the border)
    13. qreal offsetX = borderSize;
    14. qreal offsetY = borderSize;
    15. // Draw the text
    16. QPainterPath textPath;
    17. textPath.addText(QPointF(offsetX, -(offsetY + fm.descent())), font, text);
    18.  
    19. // [...]
    20. }
    To copy to clipboard, switch view to plain text mode 

    Now I have troubles in getting exact font size because QFontMetricsF seems to be very inaccurate and often the text goes outside the rectangle because it is bigger than what is reported.
    Probably I'll make another post for this problem , anyway I think it is not easily solvable (Qt or X11 or KDE Bug?).

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49

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
  •  
Qt is a trademark of The Qt Company.