PDA

View Full Version : How do I rotate a QPolygon



rawfool
28th December 2011, 12:53
I'm having a problem rotating polygon. Below is the code how I tried. Please guide me. Thank you.



void Dialog::draw_needle()
{
QPolygon needle;
needle.setPoints(4, 0,7, 14,0, 160,7, 14,14);
needle.translate((this->width()/2), (this->height()/2)+30 );

QPainter painter(this);
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.setBrush(QColor(122, 163, 39));
painter.drawConvexPolygon(needle);
painter.rotate(45);
}

void Dialog::paintEvent(QPaintEvent *event)
{
draw_dial();
draw_needle();
}

stampede
28th December 2011, 14:36
painter.drawConvexPolygon(needle);
painter.rotate(45);
You need to rotate first, then draw:


painter.rotate(45);
painter.drawConvexPolygon(needle);

rawfool
29th December 2011, 03:54
It's working now. But my rotate function is drawing the polygon in opposite direction. I gave angle of 90 degrees, but the angle it is drawing the polygon with -90 degrees. Any help here please.



painter.translate((this->width()/2), (this->height()/2)+30);
painter.rotate(90);
painter.drawPolygon(needle, Qt::OddEvenFill);

Jonny174
29th December 2011, 05:31
painter.rotate( -90 ); :)

rawfool
29th December 2011, 05:39
Dude, I tried that. But, I want to know the reason why it's taking clockwise, and not the conventional trigonometric way.

mvuori
29th December 2011, 08:31
If you want the reason, ask for the reason... You must have checked the documentation which says that rotate "Rotates the coordinate system the given angle clockwise". That is, it rotates the world where the polygons live. When you specify a polygon, you just specify points in the rotated coordinate system, not rotated coordinates. Completely conventional, but not perhaps completely documented.