I also added a little border to not have the text touching the rectangle:
{
// [...]
// Leave a 1 pixel wide border around the text
const qreal borderSize = 1;
const qreal doubleBorder = borderSize * 2;
// Calculate how much to scale the text to fit the rectangle
qreal scaleX = textRectangle.width() / (textSize.width() + doubleBorder);
qreal scaleY = textRectangle.height() / (textSize.height() + doubleBorder);
// Apply the scale factors
rotationMatrix.scale(scaleX, -scaleY);
// Center the text (because of the border)
qreal offsetX = borderSize;
qreal offsetY = borderSize;
// Draw the text
textPath.
addText(QPointF(offsetX,
-(offsetY
+ fm.
descent())), font, text
);
// [...]
}
{
// [...]
// Leave a 1 pixel wide border around the text
const qreal borderSize = 1;
const qreal doubleBorder = borderSize * 2;
// Calculate how much to scale the text to fit the rectangle
qreal scaleX = textRectangle.width() / (textSize.width() + doubleBorder);
qreal scaleY = textRectangle.height() / (textSize.height() + doubleBorder);
// Apply the scale factors
rotationMatrix.scale(scaleX, -scaleY);
// Center the text (because of the border)
qreal offsetX = borderSize;
qreal offsetY = borderSize;
// Draw the text
QPainterPath textPath;
textPath.addText(QPointF(offsetX, -(offsetY + fm.descent())), font, text);
// [...]
}
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?).
Bookmarks