{
// Rectangle mapping
rotationMatrix.translate(begin.x(), begin.y());
rotationMatrix.rotate(angle);
path.
addPolygon(rotationMatrix.
map(QPolygonF(textRectangle
)));
// Text mapping
// Take text size
QSizeF textSize
= fm.
size(0, text
);
// Calculate how much to scale the text to fit the rectangle
qreal scaleX = textRectangle.width() / textSize.width();
qreal scaleY = textRectangle.height() / textSize.height();
// Apply the scale factors
rotationMatrix.scale(scaleX, -scaleY);
// Draw the text
textPath.
addText(QPointF(0,
-fm.
descent()), font, text
);
path.addPath(rotationMatrix.map(textPath));
}
{
// Rectangle mapping
QLineF rectBase = QLineF(begin, end);
qreal angle = rectBase.angle(QLineF(QPointF(0, 0), QPointF(1, 0)));
QMatrix rotationMatrix;
rotationMatrix.translate(begin.x(), begin.y());
rotationMatrix.rotate(angle);
path.addPolygon(rotationMatrix.map(QPolygonF(textRectangle)));
// Text mapping
QFont font = QFont("courier");
// Take text size
QFontMetricsF fm(font);
QSizeF textSize = fm.size(0, text);
// Calculate how much to scale the text to fit the rectangle
qreal scaleX = textRectangle.width() / textSize.width();
qreal scaleY = textRectangle.height() / textSize.height();
// Apply the scale factors
rotationMatrix.scale(scaleX, -scaleY);
// Draw the text
QPainterPath textPath;
textPath.addText(QPointF(0, -fm.descent()), font, text);
path.addPath(rotationMatrix.map(textPath));
}
To copy to clipboard, switch view to plain text mode
Bookmarks