QT Development (15)--qt coordinate system
Introduction ofQT coordinate system
Every window in QT has a coordinate system, the upper left corner of the default window is the origin of the coordinates, then the horizontal to the right, then the horizontal to the left, and then the vertical downward, and then vertically up and down in turn. The origin point is the (0,0) point, which is added or subtracted in pixels.
Second, coordinate system transformation
the transformation of the coordinate system is done by using the transformation matrix . usually using the Qtransform class to set the transformation matrix . the Qpainter class provides transformation functions such as translation, scaling, rotation, and twisting of the coordinate system .
void Translate (const qpointf & offset)
void Translate (const Qpoint & offset)
void Translate (qreal dx, qreal dy)
void Scale (qreal sx, Qreal Sy)
void Shear (qreal sh, qreal sv)
void Rotate (qreal angle)
void Save ()
void Restore ()
1. Translation Transformation
QT translation Transformation using the Translate () function .
Translation Transformation Code:
qpainter painter (this);
Painter.setbrush (Qt::yellow);
Painter.drawrect (0,0,50,50);
Sets the point (in the current coordinate system) to the Origin .
Painter.translate (100,100);
Painter.setbrush (qt::red);
Painter.drawrect (0,0,50,50);
Sets the point (-100,-100) in the current coordinate systemto the origin point
Painter.translate ( -100,-100);
Painter.drawline (0,0,20,20);
2. Proportional Transformation
Scale with scale () function to achieve scaling effect .
Proportional Transformation Code:
qpainter painter (this);
Painter.setbrush (Qt::yellow);
Painter.drawrect (0,0,100,100);
Enlarges the coordinate system 2 times in x, y direction
Painter.scale (2,2);
Painter.setbrush (qt::red);
Painter.drawrect (50,50,50,50);
3. Twist Transform
use the shear () function to transform the line warp.
Warp Transform code:
qpainter painter (this);
Painter.setbrush (Qt::yellow);
Painter.drawrect (0,0,50,50);
Distort the y -axis of the current coordinate system
Painter.shear (0,1);
Painter.setbrush (qt::red);
Painter.drawrect (50,0,50,50);
4. Flip Transform
Flips the transform using the Rotate () function .
Flip Transform code:
qpainter painter (this);
centered at the origin, rotates clockwise by a degree .
Painter.rotate (30);
Painter.drawline (0,0,100,0);
centered at the origin, rotates clockwise and then in degrees .
Painter.rotate (30);
Painter.drawline (0,0,100,0);
5, the protection of the coordinate system State
the drawing process requiresFast,makecoordinate system toggle to draw different graphs, so you need to protect the state of the coordinate system, You can use the Save () function to save the current state of the coordinate system, then perform the transformation operation, and then use the Restore () function to restore the previous coordinate system State .Essenceis athe coordinate system isthe operation of the stack and the stack.
Coordinate System State Protection code:
qpainter painter (this);
Save current coordinate system State
Painter.save ();
translation coordinate system origin to ( +)
Painter.translate (100,100);
Painter.drawline (0,0,50,50);
Restores the coordinate system of the current origin at the point ( in.) to the saved coordinate system state
Painter.restore ();
Painter.drawline (0,0,50,50);
This article from "Life is endless, struggle not only" blog, declined reprint!
QT Development (15)--QT coordinate system