Qt 坐標系統


繪圖設備的局部物理坐標——視口坐標(viewport)坐標 ( width(),height())

邏輯坐標——窗口(window)坐標

 

常用的坐標變換是平移、旋轉和縮放

1. 坐標平移  translate( qreal dx , qreal dy) 缺省的坐標體系中,缺省的單位是像素

2. 坐標旋轉  rotate(qreal angle) 按原點順時針旋轉angle度 正數 順時針  負數 逆時針

旋轉之后並不改變窗口矩形的實際大小,只是改變了坐標軸方向

3.縮放 scale(qreal sx, qreal sy) 橫向 縱向縮放比率 比例大於1 放大 小於1 縮小

4. 狀態保存和恢復

    QPainter內部實際上有一個坐標變換矩陣,save()壓棧,保存當前坐標狀態 ,restore()彈出,恢復上次保存的坐標狀態。配對使用

     resetTransform 復位所有坐標變換的操作。

  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     painter.setRenderHint(QPainter::TextAntialiasing);
 34 
 35     qreal R = 100 ;      //半徑
 36 
 37     const qreal Pi = 3.14159;
 38 
 39 #if 0  //畫五角星的點數據
 40     qreal deg = Pi * 72 /180 ;
 41     QPoint points[5] = {
 42         QPoint( R , 0 ),
 43         QPoint( R * std::cos(deg) , -R * 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(deg) , -R * std::sin(deg)),
 54         QPoint( R * std::cos(2*deg) , -R * std::sin(2*deg)),
 55         QPoint( R * std::cos(3*deg) , -R * std::sin(3*deg)),
 56         QPoint( R * std::cos(4*deg) , -R * std::sin(4*deg)),
 57         QPoint( R * std::cos(5*deg) , -R * std::sin(5*deg)),
 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(); //閉合路徑, 最后一個點和第一個點相連
 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     starPath.addText(points[4],font,"4");
 92 #endif
 93 
 94     QPainterPath starPath; // 設計繪制六芒星的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(); //閉合路徑, 最后一個點和第一個點相連
100     starPath.moveTo(points[1]);
101     starPath.lineTo(points[3]);
102     starPath.lineTo(points[5]);
103     starPath.closeSubpath(); //閉合路徑, 最后一個點和第一個點相連
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();   //保存坐標狀態
112     painter.translate(100,200);//平移
113     painter.drawPath(starPath); //畫星星
114     painter.drawText(0,0,"S1");
115     painter.restore();   // 恢復坐標狀態
116 
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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM