Qt coordinate system

Local physical coordinate of the drawing device - viewport coordinates (the viewport) coordinates (width (), height ())

Logical coordinates - the window (window) coordinates

 

Common coordinate transformation is a translation, rotation and scaling

1. coordinate translation translate (qreal dx, qreal dy) coordinate system default, default in pixels

2. Coordinate Rotation rotate (qreal angle) angle of clockwise rotation around the origin positive negative counter clockwise

After the rotation does not change the actual size of the rectangular window, but to change the direction of the axis

3. Scaling scale (qreal sx, qreal sy) horizontal and vertical scaling ratio is greater than 1 less than 1 zoom reduction

4. Saving and restoring state

    Internal QPainter actually a coordinate transformation matrix, save () push to save the current coordinate state, restore () pops up, restore the last saved state coordinates. Paired with

     resetTransform reset all coordinate transformation.

  1 #include "pointstar.h"
  2 #include "ui_pointstar.h"
  3 #include <QPainter>
  4 #include <stdlib.h>
  5 #include <stdio.h>
  6 #include <iostream>
  7 #include <cmath>
  8 #include <complex>
  9 #include <QPen>
 10 #include <QBrush>
 11 #include <QPainterPath>
 12 
 13 PointStar::PointStar(QWidget *parent) :
 14     QWidget(parent),
 15     ui(new Ui::PointStar)
 16 {
 17     ui->setupUi(this);
 18 
 19     setPalette(QPalette(Qt::white));
 20     setAutoFillBackground(true);
 21     resize(600,300);
 22 }
 23 
 24 PointStar::~PointStar()
 25 {
 26     delete ui;
 27 }
 28 
 29 void PointStar::paintEvent(QPaintEvent *event)
 30 {
 31     QPainter painter(this);
32      painter.setRenderHint (QPainter :: Antialiasing);
 33 is      painter.setRenderHint (QPainter :: TextAntialiasing);
 34 is  
35      QREAL R & lt = 100 ;       // a radius of 
36  
37 [      const QREAL Pi = 3.14159 ;
 38 is  
39  #if 0   // Videos pentagram dot data 
40      QREAL deg = Pi * 72 / 180 [ ;
 41 is      QPoint points [ . 5 ] = {
 42 is          QPoint (R & lt, 0 ),
 43 is          QPoint (R & lt STD :: * COS (deg), -R & lt * std::sin(deg)),
 44         QPoint( R * std::cos(2*deg) , -R * std::sin(2*deg)),
 45         QPoint( R * std::cos(3*deg) , -R * std::sin(3*deg)),
 46         QPoint( R * std::cos(4*deg) , -R * std::sin(4*deg)),
 47     };
 48 #endif
 49 // 画六芒星的点数据
 50     qreal deg = Pi * 60 /180 ;
 51     QPoint points[6] = {
 52         QPoint (R, 0 ),
 53          QPoint (R * std :: cos (you), -R * std :: sin (on)),
 54          QPoint (R * std :: cos ( 2 * you) -R * std :: sin ( 2 * you)),
 55          QPoint (R * std :: cos ( 3 * you) -R * std :: sin ( 3 * you)),
 56          QPoint (R * std :: cos ( 4 * you) -R * std :: sin ( 4 * you)),
 57          QPoint (R * std :: cos ( 5 * you) -R * std :: sin ( 5 * you))
 58  
59      };
60 
 61     QFont font;
 62     font.setPointSize(12);
 63     font.setBold(true);
 64     painter.setFont(font);
 65 
 66     QPen penLine;
 67     penLine.setWidth(2);
 68     penLine.setColor(Qt::red);
 69     penLine.setStyle(Qt::SolidLine);  //线的类型
 70     penLine.setCapStyle(Qt::FlatCap);
 71     penLine.setJoinStyle(Qt::BevelJoin);
 72     painter.setPen(penLine);
 73 
 74 
 75     QBrush brush;
 76     brush.setColor(Qt::yellow);
 77     brush.setStyle(Qt::SolidPattern);
 78     painter.setBrush(brush);
 79 #if 0
 80     QPainterPath starPath; // 设计绘制五角星的paintPath 以便重复使用
 81     starPath.moveTo(points[0]);
 82     starPath.lineTo(points[2]);
 83     starPath.lineTo(points[4]);
 84     starPath.lineTo(points[1]);
 85     starPath.lineTo(points[3]);
 86     starPath.closeSubpath(); //Closed path, the last point to the first point of attachment 
87      starPath.addText (Points [ 0 ], font, " 0 " );
 88      starPath.addText (Points [ . 1 ], font, " . 1 " );
 89      starPath.addText (Points [ 2 ], font, " 2 " );
 90      starPath.addText (Points [ . 3 ], font, " . 3 " );
 91 is      starPath.addText (Points [ . 4 ], font, " . 4 " );
 92  #endif 
93  
94     StarPath QPainterPath; // design drawing for reuse hexagram paintPath 
95      starPath.addEllipse (QPointF ( 0 , 0 ), 100 , 100 );
 96      starPath.moveTo (Points [ 0 ]);
 97      starPath.lineTo (Points [ 2 ] );
 98      starPath.lineTo (points [ . 4 ]);
 99      starPath.closeSubpath (); // a closed path, the first and the last point is connected to a point 
100      starPath.moveTo (points [ . 1 ]);
 101      starPath.lineTo ( Points [ . 3 ]);
 102     starPath.lineTo (Points [ . 5 ]);
 103      starPath.closeSubpath (); // a closed path, the last point to the first point of attachment 
104      starPath.addText (Points [ 0 ], font, " 0 " );
 105      starPath .addText (Points [ . 1 ], font, " . 1 " );
 106      starPath.addText (Points [ 2 ], font, " 2 " );
 107      starPath.addText (Points [ . 3 ], font, " . 3 " );
 108      starPath.addText (Points [ . 4], font, " . 4 " );
 109      starPath.addText (Points [ . 4 ], font, " . 5 " );
 110  
111      painter.save ();    // save the state coordinates 
112      painter.translate ( 100 , 200 is ); / / translation 
113      painter.drawPath (starPath); // Videos stars 
114      painter.drawText ( 0 , 0 , " Sl " );
 115      painter.restore ();    // restore the state coordinates 
1 16  
117     painter.translate(300,120);
118     painter.scale(0.8,0.8);
119     painter.rotate(90);
120     painter.drawPath(starPath);
121     painter.drawText(0,0,"S2");
122 
123     painter.resetTransform();
124     painter.translate(500,120);
125     painter.rotate(-160);           //逆时针旋转160度
126     painter.drawPath(starPath);
127     painter.drawText(0,0,"S3");
128 }

 

Guess you like

Origin www.cnblogs.com/AmyBKLP/p/11704650.html