实例:绘制圆矩形
//绘制圆矩形------------------
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.setPen(QPen(Qt::red,2,Qt::SolidLine,Qt::RoundCap));
painter.setBrush(QBrush(Qt::gray));
//跟画矩形类似,前两个参数是左上角的坐标,中间两个参数是右下角的坐标。最后两个参数为组合起来有点类似border-radius
painter.drawRoundRect(20,20,200,100,50,30);
效果:
实例:绘制折线
//绘制折线--------------------
//定义5个点
QPoint point[5] = {
QPoint(10,10),
QPoint(100,100),
QPoint(400,50),
QPoint(50,200),
QPoint(40,100)
};
//构建绘制工具
QPainter painter(this);
//设置边线圆滑
painter.setRenderHint(QPainter::Antialiasing,true);
//设置画笔属性
painter.setPen(QPen(Qt::red,3,Qt::SolidLine,Qt::RoundCap));
//绘制折线
painter.drawPolyline(point,5);
效果:
实例:绘制多边形
//绘制多边形-----------------------------
//定义5个点
QPoint point[5] = {
QPoint(210,10),
QPoint(10,10),
QPoint(210,100),
QPoint(10,100),
QPoint(300,200)
};
//构建绘制工具
QPainter painter(this);
//设置画笔(边框)
painter.setPen(QPen(Qt::red,2,Qt::SolidLine));
//设置填充
painter.setBrush(QBrush(Qt::green));
//设置圆滑边框
painter.setRenderHint(QPainter::Antialiasing,true);
//绘制多边形
painter.drawPolygon(point,5);
效果:
实例:绘制弧线
//绘制弧线------------------
//构建绘制工具
QPainter painter(this);
//设置画笔
painter.setPen(QPen(Qt::red,2,Qt::SolidLine));
//设置圆滑边框
painter.setRenderHint(QPainter::Antialiasing,true);
//绘制弧线(相当于在矩形范围中绘制弧线),
//第三个参数是从3点钟开始的弧度 * 16,如果是整数,
//第四个参数表示弧线的弧度 * 16
//整数表示逆时针画弧线
//负数表示顺时针画弧线
//从3点钟开始逆时针画一个弧度为180的弧线
painter.drawArc(QRect(0,0,200,200), 0 * 16 , 180 * 16);
painter.setPen(QPen(Qt::blue,2,Qt::SolidLine));
//画实线
painter.drawLine(0,100,200,100);
//从3点钟开始顺时针画一个弧度为180的弧线
painter.drawArc(QRect(0,0,200,200), 0 * 16 , -180 * 16);
效果:
实例:绘制弦(与绘制弧线类似,只是方法不同)
//绘制弦------------------
//构建绘制工具
QPainter painter(this);
//设置画笔
painter.setPen(QPen(Qt::red,2,Qt::SolidLine));
//设置填充
painter.setBrush(QBrush(Qt::gray));
//设置圆滑边框
painter.setRenderHint(QPainter::Antialiasing,true);
painter.drawChord(QRect(0,0,200,200), -45 * 16 , 135 * 16);
效果:
实例:绘制扇形
//绘制扇形--------------------------------
//构建绘制对象
QPainter painter(this);
//设置边框圆滑
painter.setRenderHint(QPainter::Antialiasing, true);
//设置画笔
painter.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
//设置填充
painter.setBrush(QBrush(Qt::green, Qt::Dense3Pattern));
//设置扇形(跟弧线类似)
painter.drawPie(10, 10, 200, 200, -75 * 16, 75 * 16);
效果: