Qt (11) QPainter and coordinates

1. Coordinate system and coordinate transformation

coordinate system
insert image description here

QPainter coordinate transformation related functions

grouping function prototype Function
Coordinate transformation void translate(qreal dx,qreal dy) A certain offset of the coordinate system, the coordinate origin is translated to the new point
void rotate(qreal angle) Coordinate system rotation clockwise - one angle
void scale(qreal sx,qreal sy) Coordinate system scaling
void shear(qrael sh,qreal sy) Coordinate system twist transformation
State save and restore void save() To save the current state of the painter is to push the current state onto the stack
void restore() To restore the last state is to pop the last state from the stack
void resetTransform() reset all coordinate transformations

Example of drawing a five-pointed star

Ship widget window, only overloaded paintEventfunctions

#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");
}

insert image description here

2. Scalable graphics

Viewport : The physical coordinates of any rectangular area of ​​the drawing device, you can only select a rectangular area of ​​physical coordinates
for drawing
. The viewport is by default equal to the entire rectangle of the drawing device.
Window : The rectangular area corresponding to the viewport is just a coordinate system defined by logical coordinates, and the center of the window coordinates is at the center of the rectangle
.
Draw using window coordinates, regardless of the actual physical size

#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);
    }
}

insert image description here

Guess you like

Origin blog.csdn.net/wsp_1138886114/article/details/123494938