使用qt的QPainter可以绘制出任何你想要的图形,同时也须要一定的功底;
这里是个人使用的一些实践例子,以作学习和备忘。本次介绍主要有画弦、
矩形、圆、椭圆、QPainterPath一次性画多个、画贝塞尔曲线、画扇形、
画弧、裁剪、掩码等。
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setAutoFillBackground(true);
QPalette pal = palette();
pal.setBrush(QPalette::Background,Qt::black);
setPalette(pal); //设置背景色
resize(600,480); //调整大小
new QPushButton("hello",this); //重写paintEvent不会影响该对象的子控件
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
//设置反锯齿
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);
painter.setPen(Qt::yellow); //设置画笔颜色
//画弦,该弦在QRect(10.0, 30.0, 80.0, 60.0)的内切椭圆上,并且起始角度为30(3点钟为0度,逆时针为正),跨度为120
painter.drawChord(10.0, 30.0, 80.0, 60.0,30 * 16, 120 * 16);
}
painter.drawEllipse(QPoint(width()/2,height()/2),width()/2 - 10,height()/2 - 10) //有多种画法,这里是指定圆心和x、y轴的半径;当x、y相等时就是画圆了
painter.drawRect(width()/2,height()/2,100,100); //以矩形左上角为顶点开画
painter.drawPie(width()/2 - 50,height()/2 - 50,100,100,30 * 16,120 * 16); //在矩形内切椭圆上画,3点钟为0度,逆时针为正,角度要乘以16(参考文档)
QPainter painter(this);
//设置反锯齿
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);
painter.setPen(Qt::yellow); //设置画笔颜色
painter.drawPie(width()/2 - 50,height()/2 - 50,100,100,30 * 16,120 * 16); //在矩形内切椭圆上画,3点钟为0度,逆时针为正,角度要乘以16(参考文档)
painter.save(); //保存painter当前状态,后续紧跟一个restore()方法还原到这个状态
painter.setPen(Qt::white);
painter.drawRect(50,50,80,100); //画一个白色的矩形
painter.restore(); //还原到前一个保存状态
painter.drawRect(150,150,50,50); //用前一个保存的painter状态,画一个矩形
painter.setPen(Qt::yellow); //设置画笔颜色
painter.translate(width()/2,height()/2); //将坐标第原点移动到中心
painter.drawRect(0,0,100,100);
painter.setPen(Qt::yellow); //设置画笔颜色
painter.translate(width()/2 - 50 ,height()/2 - 50);
painter.resetMatrix();
painter.drawEllipse(0,0,100,100);
painter.setPen(Qt::yellow); //设置画笔颜色
painter.drawRoundRect(QRect(50,50,100,100),50,50);
QRegion maskRegion(-7,-30,width() + 14,height() + 67);
setMask(maskRegion.subtracted(QRegion(width()/2 - 70,height()/2 - 30,140,60,QRegion::Ellipse)));
painter.setPen(Qt::yellow); //设置画笔颜色
painter.setClipRegion(QRegion(width()/2 - 100,height()/2 - 100,200,200)); //设置一个裁剪区域
painter.setPen(Qt::white);
painter.drawRect(painter.clipRegion().boundingRect());
painter.setPen(Qt::yellow);
painter.setBrush(QBrush(Qt::red));
painter.drawEllipse(width()/2 - 80,height()/2 - 80,200,200); //超出裁剪区域的不画
painter.setPen(Qt::yellow); //设置画笔颜色
QRegion r1(width()/2 - 50,height()/2 - 50,100,100,QRegion::Ellipse);
QRegion r2(width()/2 - 100,height()/2 - 100,200,200,QRegion::Ellipse);
painter.setClipRegion(r2 - r1);
painter.fillRect(r2.boundingRect(),QBrush(Qt::red));
painter.setPen(Qt::yellow); //设置画笔颜色
QPainterPath path;
path.addText(10,10,painter.font(),"Qt");
path.moveTo(10,50);
path.lineTo(120,50);
path.addRoundedRect(10,80,80,80,5,5);
painter.drawPath(path);
painter.setPen(Qt::yellow); //设置画笔颜色
QPainterPath path;
path.arcMoveTo(width()/2 - 100,height()/2 - 100, 200,200,30); //将path的起点移到一个椭圆(以3点钟为0度,逆时针为正)距离角度0点为30度的椭圆上,该椭圆是rect(100,280,120,120)的内切椭圆
path.arcTo(width()/2 - 100,height()/2 - 100, 200,200,30,120);
painter.drawPath(path);
painter.setPen(Qt::yellow); //设置画笔颜色
QPainterPath path;
path.moveTo(width()/2,height()/2);
path.arcTo(width()/2 - 100,height()/2 - 100, 200,200,30,120);
painter.drawPath(path);
painter.setPen(Qt::yellow); //设置画笔颜色
QPainterPath path;
path.moveTo(width()/2,height()/2);
path.arcTo(width()/2 - 100,height()/2 - 100, 200,200,30,120);
path.closeSubpath();
painter.drawPath(path);
painter.setPen(Qt::yellow); //设置画笔颜色
QPainterPath cubPath;
cubPath.cubicTo(170,50,90,200,width(),height());//传入点1(170,50),点2(90,200),endPoint(width(),heigt())
cubPath.addEllipse(170 - 3,50 - 3,6,6); //以点1为圆心画圆
cubPath.addText(180,60,painter.font(),QStringLiteral("Point1")); //标记点1
cubPath.addEllipse(90 - 3,200 - 3,6,6);//以点2为圆心画圆
cubPath.addText(100,210,painter.font(),QStringLiteral("Point2"));//标记点2
painter.drawPath(cubPath);
painter.setPen(Qt::yellow); //设置画笔颜色
QPainterPath cubPath;
cubPath.cubicTo(170,50,90,200,width(),height());//传入点1(170,50),点2(90,200),endPoint(width(),heigt())
cubPath.addEllipse(170 - 3,50 - 3,6,6); //以点1为圆心画圆
cubPath.addText(180,60,painter.font(),QStringLiteral("Point1")); //标记点1
cubPath.addEllipse(90 - 3,200 - 3,6,6);//以点2为圆心画圆
cubPath.addText(100,210,painter.font(),QStringLiteral("Point2"));//标记点2
painter.setBrush(QBrush(Qt::lightGray));
painter.drawPath(cubPath);