I need to draw the text rotated 45 degrees on the image. I use QPainter. That is method rotate. I do painter.rotate(45) , but my text completely disappears from it. Pen and Font asked (opaque color), I now painter.drawText(500, 2000, 'Hello world!') even the simplest overload painter.drawText(500, 2000, 'Hello world!') And the image is empty ... And without the rotate call, everything works fine. Where is the catch?

upd. I found the trick: the text just went beyond the borders of the image. Due to the fact that the painter is rotated, the coordinate system seems to change ... In this case, the question is: how can I recalculate the coordinates so that they are approximately in the same place (although the text is rotated, well, at least it would start from the same place)?

1 answer 1

I think everything will turn out if you first make the transfer of the origin of the coordinates QPainter, and only then turn the QPainter. For example, if we want to draw the text myTextString rotated by angle (in degrees), starting at the point with coordinates (text_x, text_y) , you can do this ( p is QPainter):

 p.translate(text_x, text_y) p.rotate(angle) p.drawText(0, 0, myTextString) 

In this case, when turning around the origin (which will now be at the point where the transfer was made), the text will not go far. If you want the middle of the line at this point, then the last line can be replaced by

 fm = p.fontMetrics() width = fm.width(myTextString) p.drawText(-width / 2, 0, myTextString) 

Probably, it may then be convenient to perform inverse coordinate transformations.