目录
1 坐标变换函数
1.1 坐标平移
1.2 坐标旋转
1.3 缩放
1.4 状态保存与恢复
2 坐标变换绘图实例
2.1 绘制3个五角星的程序
2.2 绘制五角星的PainterPath的定义
3 视口和窗口
3.1 视口和窗口的定义与原理
3.2 视口和窗口的使用实例
4 绘图叠加的效果
void translate(qreal dx,qreal dy)
void rotate(qreal angle)
void scale(qreal sx,qreal sy)
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setPalette(QPalette(Qt::white)); //设置窗口背景色
setAutoFillBackground(true);
resize(600,300); //固定初始化窗口大小
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);//创建QPainter对象
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);
//生成五角星的5个顶点的,假设原点在五角星中心
qreal R=100; //半径
const qreal Pi=3.14159;
qreal deg=Pi*72/180;
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)),
};
//设置字体
QFont font;
font.setPointSize(12);
font.setBold(true);
painter.setFont(font);
//设置画笔
QPen penLine;
penLine.setWidth(2); //线宽
penLine.setColor(Qt::blue); //划线颜色
penLine.setStyle(Qt::SolidLine);//线的类型,实线、虚线等
penLine.setCapStyle(Qt::FlatCap);//线端点样式
penLine.setJoinStyle(Qt::BevelJoin);//线的连接点样式
painter.setPen(penLine);
//设置画刷
QBrush brush;
brush.setColor(Qt::yellow); //画刷颜色
brush.setStyle(Qt::SolidPattern); //画刷填充样式
painter.setBrush(brush);
//设计绘制五角星的PainterPath,以便重复使用
QPainterPath starPath;
starPath.moveTo(points[0]);
starPath.lineTo(points[2]);
starPath.lineTo(points[4]);
starPath.lineTo(points[1]);
starPath.lineTo(points[3]);
starPath.closeSubpath(); //闭合路径,最后一个点与第一个点相连
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.save(); //保存坐标状态
painter.translate(100,120);
painter.drawPath(starPath); //画星星
painter.drawText(0,0,"S1");
painter.restore(); //恢复坐标状态
painter.translate(300,120); //平移
painter.scale(0.8,0.8); //缩放
painter.rotate(90); //顺时针旋转
painter.drawPath(starPath);//画星星
painter.drawText(0,0,"S2");
painter.resetTransform(); //复位所有坐标变换
painter.translate(500,120); //平移
painter.rotate(-145); //逆时针旋转
painter.drawPath(starPath);//画星星
painter.drawText(0,0,"S3");
}
painter.save(); //保存坐标状态painter.translate(100,120);painter.drawPath(starPath); //画星星painter.drawText(0,0,"S1");painter.restore(); //恢复坐标状态
painter.translate(300,120); //平移painter.scale(0.8,0.8); //缩放painter.rotate(90); //顺时针旋转painter.drawPath(starPath);//画星星painter.drawText(0,0,"S2");
painter.resetTransform();//复位所有坐标变换
void QPainter::setViewport(int x,int y,int width,int height)
painter.setViewport(50,0,200,200);
void QPainter::setWindow(int x,int y,int width,int height)
painter.setWindow(-50,-50,100,100);
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
int W=width();
int H=height();
int side=qMin(W,H);//取长和宽的小值
QRect rect((W-side)/2, (H-side)/2,side,side); //viewport矩形区
painter.drawRect(rect); //Viewport大小
// painter.setViewport((W-side)/2, (H-side)/2,side,side);//设置Viewport,物理坐标范围
painter.setViewport(rect);//设置Viewport
painter.setWindow(-100,-100,200,200); // 设置窗口大小,逻辑坐标
painter.setRenderHint(QPainter::Antialiasing);
// p.setRenderHint(QPainter::TextAntialiasing);
//设置画笔
QPen pen;
pen.setWidth(1); //线宽
pen.setColor(Qt::red); //划线颜色
pen.setStyle(Qt::SolidLine);//线的类型,实线、虚线等
painter.setPen(pen);
for(int i=0; i<36;i++)
{
painter.drawEllipse(QPoint(50,0),50,50);
painter.rotate(10);
}
}
for(int i=0; i<36;i++){painter.drawEllipse(QPoint(50,0),50,50);painter.rotate(10);}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
int W=width();
int H=height();
int side=qMin(W,H);//取长和宽的小值
QRect rect((W-side)/2, (H-side)/2,side,side); //viewport矩形区
painter.drawRect(rect); //Viewport大小
// painter.setViewport((W-side)/2, (H-side)/2,side,side);//设置Viewport,物理坐标范围
painter.setViewport(rect);//设置Viewport
painter.setWindow(-100,-100,200,200); // 设置窗口大小,逻辑坐标
painter.setRenderHint(QPainter::Antialiasing);
// p.setRenderHint(QPainter::TextAntialiasing);
//设置画笔
QPen pen;
pen.setWidth(1); //线宽
pen.setColor(Qt::red); //划线颜色
pen.setStyle(Qt::SolidLine);//线的类型,实线、虚线等
painter.setPen(pen);
//线性渐变
QLinearGradient linearGrad(0,0,100,0);//从左到右,
linearGrad.setColorAt(0,Qt::yellow);//起点颜色
linearGrad.setColorAt(1,Qt::green);//终点颜色
linearGrad.setSpread(QGradient::PadSpread); //展布模式
painter.setBrush(linearGrad);
//设置复合模式
painter.setCompositionMode(QPainter::RasterOp_NotSourceXorDestination);
// painter.setCompositionMode(QPainter::CompositionMode_Difference);
// painter.setCompositionMode(QPainter::CompositionMode_Exclusion);
for(int i=0; i<36;i++)
{
painter.drawEllipse(QPoint(50,0),50,50);
painter.rotate(10);
}
}
在上面的程序中,对单个圆使用了线性渐变填充,单个圆从左到右,由黄色渐变为绿色。使用QPainter::setCompositionMode()函数设置组合模式,即后面绘制的图与前面绘制的图的叠加模式。函数参数是一个QPainter::CompositionMode枚举类型值,可以查看Qt帮助,这个枚举
类型有近40种取值,表示了后绘制图形与前面图形的不同叠加运算方式。
图8-15是其中两种叠加模式下的绘图效果,可以发现采用不同的叠加模式,可以得到不同的绘图效果,甚至是意想不到的绚丽效果。用户可以自己修改程序,设置不同渐变颜色、渐变填充模式、不同叠加模式,也许能绘制出更炫的图形呢。
图8-15 渐变填充和叠加效果,(左)CompositionMode_Difference模式叠加,(右)RasterOp_NotSourceXorDestination模式叠加