1
votes

I'm trying to paint a rotated image on an existing painter. The rotation as well as the location will be different each time. The rotation works fine, but I can't seem to predict the location. The weird thing is is that it works differently if I draw a text instead of an image.

The text seems to draw with the LEFT BOTTOM starting at 'point' while the image starts with the LEFT TOP at 'point'. But then after the rotation I don't understand what happens with the image....

Example:

QPoint point = QPoint( 80, 200 );
painter->rotate(45);

painter->drawImage(point, QImage("/srv/...."));  // i can't predict where this goes
painter->drawText(point, "Rotated text");        // but I can predict exactly where this goes!

I'm thinking that maybe I need to use translate(x,y) in case I'm using drawImage, but I'm at a loss to what kind of x and y I need to use depending on the rotation and why it works fine with drawText.

2

2 Answers

2
votes

To anyone reading - I had another brain leak... I should have first translated the coordinate system to the place where I wanted the image to end up, then rotate and then draw the image at 0,0...

The code becomes:

painter->translate(80,200);  // this will be our point of origin
painter->rotate(45);         // now do the rotation at this point

painter->drawImage(0,0,QImage("/srv/....")); // since we're already at our point enter 0,0
0
votes

For transformation you need to do as below for example:

painter->translate(m_bound.width()/2.0,m_bound.height()/2.0);
painter->rotate(m_angle);
painter->translate(-m_bound.width()/2.0,-m_bound.height()/2.0);
//.... Paint what you want as normal ....//

It simply translates into top-left, then rotates and finally translates back to the original position.
Please note that this transformations are Matrices so affecting is reversed (line1: translate back, line2: rotate, line3: translate to top-left)