Problem with painter.drawText, transform and translate
-
All is well with the below code as long as both offsets are 0. This is even true when the text is rotated and mirrored. However when there are offsets, drawText is skewed when the rotation is not 0 degrees. I thought that QTransform did all the math? Is my implementation wrong? Do I need to do the math to compensate for the skewing?
@
void paintImage(QGraphicsTextItem &txtItem, int offsetX, int offsetY)
{
QPainter painter(&myImage);
painter.setPen(myPen);
painter.setFont(txtItem.font());QTransform trans = txtItem.sceneTransform();
trans.translate(offsetX, offsetY);painter.setTransform(trans);
painter.drawText(txtItem.boundingRect(), Qt::AlignCenter | Qt::AlignJustify, txtItem.toPlainText());
}@
-
The order of transformations matters. For example translate => rotate => translate will give totally different results than translate => translate => rotate.
It's not usually possible to gradually add transformations.
I would store the desired offsets, scale and rotation and recreate transform matrix in the correct order each time any of those changes. -
Chris, Thanks for your response. You're right, the order does matter. After playing around with it for a little bit, the following is the solution that I came up with.
@
void paintImage(QGraphicsTextItem &txtItem, int offsetX, int offsetY) { QPainter painter(&myImage); painter.setPen(myPen); painter.setFont(txtItem.font()); QTransform trans; trans.setMatrix(txtItem.sceneTransformation().m11(),
txtItem.sceneTransformation().m12(), txtItem.sceneTransformation().m13(), txtItem.sceneTransformation().m21(), txtItem.sceneTransformation().m22(), txtItem.sceneTransformation().m23(), txtItem.sceneTransformation().m31()-offsetX, txtItem.sceneTransformation().m32()-offsetY, txtItem.sceneTransformation().m33());
painter.setTransform(trans);
painter.drawText(txtItem.boundingRect(), Qt::AlignCenter | Qt::AlignJustify, txtItem.toPlainText()); }
@