PDA

View Full Version : Rotation problem trying to draw a compass widget



yellowmat
18th February 2007, 17:54
Hi everybody,

I am developping a custom widget, a compass, composed of a background pixmap and a foreground pixmap (the cursor).

My paintEvent code is a s follow :


QPainter painter(this);

painter.drawPixmap(0, 0, *m_pBackground);
painter.rotate(m_dAngle);
painter.drawPixmap(0, 0, *m_pForeground);


The problem here is that after being rotated, the foreground pixmap is not centered over the background pixmap center.

I think I must code something like this :


QPainter painter(this);

painter.drawPixmap(0, 0, *m_pBackground);
painter.rotate(m_dAngle);
painter.translate(x, y);
painter.drawPixmap(0, 0, *m_pForeground);

but how to determine x and y ?

It would be great if anybody could give me a clue.

Thanks in advance.

jacek
18th February 2007, 18:16
I think I must code something like this :
...
but how to determine x and y ?
Try:
int x = m_pBackground->width() / 2; // or width() if it's the same
int y = m_pBackground->height() / 2;

painter.drawPixmap(0, 0, *m_pBackground);

painter.translate(-x, -y);
painter.rotate(m_dAngle);
painter.translate(x, y);

painter.drawPixmap(0, 0, *m_pForeground);

yellowmat
18th February 2007, 20:03
It works fine.

Thanks