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