PDA

View Full Version : QPaint - Scale



jsmith
21st July 2009, 13:58
HI,
i have drawn one polygon using paint eventin QWidget. i have implemented code for zoom in/zoom out using wheelevent like below


const QPointF points[4]={(100,100),(125,67),(125,99),(100,200)};
QPaintevent(QPainter painter)
{
painter.scale(m_scale,m_scale);
painter.drawPolygon(points,4);
}

wheelevent


QWheelevent(QWheelEvent *event)
{
if(event->delta()>0
{
m_scale+=0.5;
update();}

problem is here it's zooming but position of polygon is shifting right.

please tell me why it's happening like this.

thanks in advance

caduel
21st July 2009, 23:48
Scale does ecactly that: it scales all coordinates, this obviously affects both sizes and positions - if the x-offset was at 50, and you scale by factor 2, it will be at position 100. Thus the item moves (on your screen). Relatively to other things you paint (with that painter), it does not move.
You can readjust that with a call to translate().

A common "trick" is to first move the origin with translate() and then paint whatever you want relative to your origin, that way if you scale (*after* that translation) only the size will increase, but the origin will remain.

HTH

jsmith
22nd July 2009, 02:47
Relatively to other things you paint (with that painter), it does not move.

i don't understand what is above sentence mean. Plz explain me. What ever i have drawn using that painter all are changing.



A common "trick" is to first move the origin with translate() and then paint whatever you want relative to your origin, that way if you scale (*after* that translation) only the size will increase, but the origin will remain.

that mean we translate to center of window every time constantly are changed to every time

wagmare
22nd July 2009, 09:10
translate the item in reverse order with the scale factor whenever u scale the item ..
caduel shows u the

if the x-offset was at 50, and you scale by factor 2, it will be at position 100.
u can use this to move back the item to original position using QTranslate