在QPainter可以使用以下函数变换坐标:
QPainter::scale() | 缩放坐标系统 |
QPainter::rotate() | 顺时针旋转 |
QPainter::translate() | 平移 |
QPainter::shear() | 围绕原点来扭曲坐标系统 |
缩放 (QPainter::scale)
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)
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);
}
平移(QPainter::translate)
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)
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);
}