PDA

View Full Version : Scaling QPolygon or QPolygonF



tuxit
1st August 2011, 15:20
Hello,

I need to scale a polygon.

write the following


QPolygonF qpf=QPolygonF(QPolygon(4,points));
QTransform trans;
trans=trans.scale(1.5,1.5);
QPolygonF qpf2=trans.map(qpf);
path.addPolygon(qpf2);


for the points:


static const int points[8] = {
10, 80,
20, 10,
80, 30,
90, 70
};

it generates
---15,120--
---30,15--
---120,45--
---135,105--

thus it moves slightly too.

is there a way to scale from center?

Cruz
1st August 2011, 15:41
QTransform is a transformation applied to the coordinate system and not to a single object, such as a polygon. To scale your polygon the way you want, you have to first translate it into the center of the coordinate system. But it#s best if you describe what you really want to do. Do you want to implement some kind of a zooming function?

tuxit
1st August 2011, 15:49
thanks for reply, yes exactly a zooming function i want.
can you give some method or class name for


first translate it into the center of the coordinate system

thanks

Cruz
1st August 2011, 16:19
I haven't used it myself yet, but as far as I know the Graphics View Framework (http://doc.qt.nokia.com/4.7/graphicsview.html) comes with a zooming function. Without knowing your requirements and environment, my first advice would be to try and implement your software with this framework.

If you feel the need to do it yourself with coordinate transformations and widget drawing, then the QTransform is the right thing to use. However, you need to understand exactly what effect coordinate transformations have on what you see on the screen. Basically, you can use the translate() function of QTransform to translate your polygon into the origin, then scale it to the size you want and then you have to translate() it back to the position where you want to see it on the screen and thereby take into account that the coordinate frame has been scale()-d. Please note that scaling the coordinate frame will also change the relative distances between other objects on the screen.