QPainter::scale() | 缩放坐标系统 |
QPainter::rotate() | 顺时针旋转 |
QPainter::translate() | 平移 |
QPainter::shear() | 围绕原点来扭曲坐标系统 |
QTransfrom T | 用该对象来保存设置过程 |
setTransfrom() | 设置Transfrom |
QPainter::scale(qreal sx,qreal sy)
- sx 为 x轴的倍数
- sy 为 y轴的倍数
放大:
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- QPen pen(Qt::black);
- painter.setPen(pen);//设置笔
- painter.drawRect(100,100,100,100);
- painter.scale(2,2);//x,y翻2倍
- painter.drawRect(100,100,100,100);//相当于drawRect(200,200,200,200)
- }
缩小:
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- QPen pen(Qt::black);
- painter.setPen(pen);//设置笔
- painter.drawRect(100,100,100,100);
- //painter.scale(2,2);//x,y放大2倍
- //painter.drawRect(100,100,100,100);//相当于drawRect(200,200,200,200)
- painter.scale(0.5,0.5);//x,y缩小2倍
- painter.drawRect(100,100,100,100);//相当于drawRect(50,50,50,50)
- }
注意:当使用多次scale时,它们是结合一起使用。
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- QPen pen(Qt::black);
- painter.setPen(pen);//设置笔
- painter.scale(2,2);//x,y放大2倍
- painter.scale(0.5,0.5);//x,y缩小2倍
- //相当于放大2倍 缩小2倍 相当于没变换
- painter.drawRect(100,100,100,100);//相当于drawRect(100,100,100,100)
- }
QPainter::rotate(qreal angle)
- angle为角度
- 以原点坐标
初始状态:
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- QPen pen(Qt::black);
- painter.setPen(pen);//设置笔
- painter.drawRect(100,0,100,100);
-
- }
旋转30°
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- QPen pen(Qt::black);
- painter.setPen(pen);//设置笔
- painter.rotate(30);
- painter.drawRect(100,0,100,100);
-
- }
移动坐标再旋转:
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPen pen(Qt::cyan);
- pen.setBrush(Qt::blue);
- pen.setWidth(2);
- QPainter painter(this);
- painter.translate(100,100);//移动坐标
- painter.drawLine(0,0,0,-100);//绘制线段
- painter.rotate(200);//旋转270°
- painter.drawLine(0,0,0,-100);//绘制线段
- }
QPainter::translate(constQPointF&offset)
- offset x轴的增量和y轴的增量
初始状态:
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- QPen pen(Qt::black);
- painter.setPen(pen);//设置笔
- painter.drawRect(100,0,100,100);
-
- }
平移:
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- QPen pen(Qt::black);
- painter.setPen(pen);//设置笔
- painter.translate(100,100);//x坐标加100 y坐标加100
- painter.drawRect(100,0,100,100);
- }
QPainter::shear(qrealsh,qrealsv)
- 按 (sh,sv) 切变坐标系
- sh横向扭曲
- sv纵向扭曲
初始状态:
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- QPen pen(Qt::black);
- painter.setPen(pen);//设置笔
- painter.drawRect(100,0,100,100);
-
- }
扭曲坐标:
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- QPen pen(Qt::black);
- painter.setPen(pen);//设置笔
- painter.shear(0,1);//纵向扭曲
- painter.drawRect(100,0,100,100);
- }
在头文件中添加:
- int angle=0;//弧度
- void paintEvent(QPaintEvent *event);//绘图事件
构造函数中添加:
- QTimer *time=new QTimer(this);
- connect(time,&QTimer::timeout,this,[=](){
- update();//更新数据
- });
- time->start(1000);//开启定时器
绘图事件的实现:
- void Widget::paintEvent(QPaintEvent *event)
- {
- angle+=10;//每次弧度加10
- if(angle==360) angle=0;//当弧度为360时,重置
- int side=qMin(width(),height());
- QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing);//开启锯齿
- QTransform transform;
- transform.translate(width()/2,height()/2);//移动坐标
- transform.scale(side/300.0,side/300.0);//缩放
- transform.rotate(angle);//旋转
- painter.setTransform(transform);//设置过程
- painter.drawEllipse(-120,-120,240,240);//画一个圆
- painter.drawLine(0,0,100,0);//绘制线段
- }