首頁 > 軟體

Qt中QPainter與座標的使用

2022-04-07 19:01:21

一、座標系統與座標變換

座標系統

QPainter座標變換相關函數

分組函數原型功能
座標變換void translate(qreal dx,qreal dy)座標系統一定的偏移量,座標原點平移到新的點
void rotate(qreal angle)座標系統順時針旋轉-一個角度
void scale(qreal sx,qreal sy)座標系統縮放
void shear(qrael sh,qreal sy)座標系統做扭轉變換
狀態儲存與恢復void save()儲存painter當前的狀態,就是將當前狀態壓入棧
void restore()恢復上一次狀態,就是從堆疊中彈出上次的狀態
void resetTransform()復位所有的座標變換

範例

繪製五角星

船艦widget視窗,只過載paintEvent函數

#include "widget.h"
#include "ui_widget.h"
#include <QPalette>
#include <QPainter>
#include <cmath>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setPalette(QPalette(Qt::white));
    setAutoFillBackground(true);
    resize(600,300);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    QPen pen;                                      //筆
    pen.setStyle(Qt::SolidLine);
    painter.setPen(pen);
    painter.setRenderHint(QPainter::Antialiasing); //抗鋸齒

    QBrush brush;                                  //刷子
    brush.setColor(Qt::yellow);
    brush.setStyle(Qt::SolidPattern);
    painter.setBrush(brush);

    //qreal deg = 3.141592*2/5;
    qreal deg = (360/5)*3.141592/180;
    qreal R=100;
    QPoint points[5] = {
        QPoint(R,0),
        QPoint(R*std::cos(deg),-R*std::sin(deg)),
        QPoint(R*std::cos(2*deg),-R*std::sin(2*deg)),
        QPoint(R*std::cos(3*deg),-R*std::sin(3*deg)),
        QPoint(R*std::cos(4*deg),-R*std::sin(4*deg)),
    };
    QPainterPath starPath;

    starPath.moveTo(points[3]);
    starPath.lineTo(points[1]);
    starPath.lineTo(points[4]);
    starPath.lineTo(points[2]);
    starPath.lineTo(points[0]);
    starPath.closeSubpath();

    QFont font;
    font.setPointSize(12);
    starPath.addText(points[0], font, "0");
    starPath.addText(points[1], font, "1");
    starPath.addText(points[2], font, "2");
    starPath.addText(points[3], font, "3");
    starPath.addText(points[4], font, "4");

    painter.setFont(font);
    painter.save();                  //儲存
    painter.translate(100,120);
    painter.drawPath(starPath);
    painter.drawText(0,0,"S1");

    painter.restore();              //恢復
    painter.translate(300,120);
    painter.rotate(90);
    painter.scale(0.7,0.7);
    painter.drawPath(starPath);
    painter.drawText(0,0,"S2");


    painter.resetTransform();
    painter.translate(500,120);
    painter.rotate(-90);
    painter.scale(1.05,1.05);
    painter.drawPath(starPath);
    painter.drawText(0,0,"S3");
}

二、可縮放圖形

視口: 繪圖裝置的任意一個矩形區域的物理座標,可以只選取物理座標的一個矩形區域用
於繪圖
。視口預設情況下等於繪圖裝置的整個矩形區。
視窗: 對應於視口的矩形區域,只不過是用邏輯座標定義的座標系,視窗座標的中心在矩
形中心。
使用視窗座標繪製,不用管實際的物理大小

#include "widget.h"
#include "ui_widget.h"
#include <QPalette>
#include <QPainter>
#include <QLinearGradient>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setPalette(QPalette(Qt::white));
    setAutoFillBackground(true);
    resize(300,300);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    int W = QWidget::width();
    int H = QWidget::height();

    int side = qMin(W,H);
    QRect rect((W-side)/2,(H-side)/2, side,side);  //視口大小
    painter.drawRect(rect);
    painter.setViewport(rect);
    painter.setWindow(-100,-100,200,200);

    QLinearGradient linerGradient(0,0,100,0);     // 漸變
    linerGradient.setColorAt(0,Qt::yellow);
    linerGradient.setColorAt(1,Qt::green);
    linerGradient.setSpread(QGradient::PadSpread); //發散

    painter.setCompositionMode(QPainter::RasterOp_NotSourceXorDestination);
    painter.setBrush(linerGradient);
    for (size_t i=0; i<36; i++) {
        painter.drawEllipse(QPoint(50,0),50,50);
        painter.rotate(10);
    }
}

 到此這篇關於Qt中QPainter與座標的使用的文章就介紹到這了,更多相關Qt中QPainter與座標內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com