重载绘图事件:
protected:void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;void Widget::paintEvent(QPaintEvent *event)
{
进行绘图操作
}
绘图前操作:
//基本绘图QPainter painter(this);//创建QPainter对象painter.setRenderHint(QPainter::Antialiasing);//painter.setRenderHint(QPainter::TextAntialiasing);//int W=this->width(); //绘图区宽度int H=this->height(); //绘图区高度QRect rect(W/4,H/4,W/2,H/2); //中间区域矩形框//设置画笔QPen pen;pen.setWidth(3); //线宽pen.setColor(Qt::red); //划线颜色//Qt::NoPen,Qt::SolidLine, Qt::DashLine, Qt::DotLine,Qt::DashDotLine,Qt::DashDotDotLine,Qt::CustomDashLinepen.setStyle(Qt::SolidLine);//线的类型,实线、虚线等//Qt::FlatCap, Qt::SquareCap,Qt::RoundCappen.setCapStyle(Qt::FlatCap);//线端点样式//Qt::MiterJoin,Qt::BevelJoin,Qt::RoundJoin,Qt::SvgMiterJoinpen.setJoinStyle(Qt::BevelJoin);//线的连接点样式painter.setPen(pen);//设置画刷
// QPixmap texturePixmap(":images/images/texture.jpg");QBrush brush;brush.setColor(Qt::yellow); //画刷颜色brush.setStyle(Qt::SolidPattern); //画刷填充样式
// brush.setStyle(Qt::TexturePattern); //画刷填充样式
// brush.setTexture(texturePixmap); //设置材质图片painter.setBrush(brush);
绘图:
painter.drawLine(rect.left(),rect.top(),rect.right(),rect.bottom());painter.drawRect(rect); //只填充定义的渐变区域painter.drawRect(this->rect()); //填充更大区域,会有延展效果painter.fillRect(rect,Qt::red);painter.drawRoundRect(rect);painter.drawRoundedRect(rect,40,20);painter.drawEllipse(rect);painter.drawPolyline(points,9);painter.drawPolygon(points,9);painter.drawPoints(points,9);painter.translate(100,300);painter.drawPath(starPath);painter.translate(200,0);painter.drawPath(starPath);painter.translate(200,0);painter.drawPath(starPath);painter.drawArc(rect,30*16,80*16);painter.drawChord(rect,90*16,120*16);painter.drawPie(rect,90*16,120*16);painter.drawText(rect,"Hello, QT");
通用函数:
void Widget::myDrawTextureRect()
{QPainter painter(this);//创建QPainter对象painter.setRenderHint(QPainter::Antialiasing);//painter.setRenderHint(QPainter::TextAntialiasing);//int W=this->width(); //绘图区宽度int H=this->height(); //绘图区高度QRect rect(W/4,H/4,W/2,H/2); //中间区域矩形框//设置画笔QPen pen;pen.setWidth(3); //线宽pen.setColor(Qt::red); //划线颜色//Qt::NoPen,Qt::SolidLine, Qt::DashLine, Qt::DotLine,Qt::DashDotLine,Qt::DashDotDotLine,Qt::CustomDashLinepen.setStyle(Qt::SolidLine);//线的类型,实线、虚线等//Qt::FlatCap, Qt::SquareCap,Qt::RoundCappen.setCapStyle(Qt::FlatCap);//线端点样式//Qt::MiterJoin,Qt::BevelJoin,Qt::RoundJoin,Qt::SvgMiterJoinpen.setJoinStyle(Qt::BevelJoin);//线的连接点样式painter.setPen(pen);//设置画刷QPixmap texturePixmap(":images/images/texture.jpg");QBrush brush;
// brush.setColor(Qt::yellow); //画刷颜色
// brush.setStyle(Qt::SolidPattern); //画刷填充样式brush.setStyle(Qt::TexturePattern); //画刷填充样式brush.setTexture(texturePixmap); //设置材质图片painter.setBrush(brush);//绘图painter.drawRect(rect); //只填充定义的渐变区域
}
绘制渐变色:
void Widget::myDrawGradient()
{QPainter painter(this);//创建QPainter对象painter.setRenderHint(QPainter::Antialiasing);//painter.setRenderHint(QPainter::TextAntialiasing);//int W=this->width(); //绘图区宽度int H=this->height(); //绘图区高度QRect rect(W/4,H/4,W/2,H/2); //中间区域矩形框//设置画笔
// QPen pen;
// pen.setStyle(Qt::NoPen);//线的类型,实线、虚线等
// painter.setPen(pen);//线性渐变
// QLinearGradient linearGrad(rect.left(),rect.top(),rect.right(),rect.bottom()); //对角线QLinearGradient linearGrad(rect.left(),rect.top(),rect.right(),rect.top());//从左到右linearGrad.setColorAt(0,Qt::blue);//起点颜色linearGrad.setColorAt(0.5,Qt::green);//起点颜色linearGrad.setColorAt(1,Qt::red);//终点颜色linearGrad.setSpread(QGradient::ReflectSpread); //展布模式
//QGradient::PadSpread ,QGradient::RepeatSpread, QGradient::ReflectSpreadpainter.setBrush(linearGrad);//径向渐变
// QRadialGradient radialGrad(W/2,H/2,qMax(W/8,H/8),W/2,H/2);radialGrad.setColorAt(0,Qt::white);
// radialGrad.setColorAt(0,Qt::green);
// radialGrad.setColorAt(1,Qt::blue);
// radialGrad.setSpread(QGradient::ReflectSpread);
// //QGradient::PadSpread ,QGradient::RepeatSpread, QGradient::ReflectSpread
// painter.setBrush(radialGrad);//圆锥型渐变
// QConicalGradient coniGrad(W/2,H/2,45);
// coniGrad.setColorAt(0,Qt::yellow);
// coniGrad.setColorAt(0.5,Qt::blue);
// coniGrad.setColorAt(1,Qt::green);coniGrad.setSpread(QGradient::PadSpread); //对于锥形渐变不起作用
// painter.setBrush(coniGrad);//绘图
// painter.drawRect(rect); //只填充定义的渐变区域painter.drawRect(this->rect()); //填充更大区域,会有延展效果
}
绘制各种图形:
void Widget::myDrawShape()
{QPainter painter(this);//创建QPainter对象painter.setRenderHint(QPainter::Antialiasing);painter.setRenderHint(QPainter::TextAntialiasing);int W=this->width(); //绘图区宽度int H=this->height(); //绘图区高度//设置画笔QPen pen;pen.setWidth(3); //线宽pen.setColor(Qt::black); //划线颜色//Qt::NoPen,Qt::SolidLine, Qt::DashLine, Qt::DotLine,Qt::DashDotLine,Qt::DashDotDotLine,Qt::CustomDashLinepen.setStyle(Qt::SolidLine);//线的类型,实线、虚线等//Qt::FlatCap, Qt::SquareCap,Qt::RoundCappen.setCapStyle(Qt::FlatCap);//线端点样式//Qt::MiterJoin,Qt::BevelJoin,Qt::RoundJoin,Qt::SvgMiterJoinpen.setJoinStyle(Qt::BevelJoin);//线的连接点样式painter.setPen(pen);//1. drawArc()
// QRect rect(W/4,H/4,W/2,H/2);
// int startAngle = 90 * 16; //起始90°
// int spanAngle = 90 * 16; //旋转90°
// painter.drawArc(rect, startAngle, spanAngle);//2. drawChord()
// QRect rect(W/4,H/4,W/2,H/2);
// int startAngle = 90 * 16; //起始90°
// int spanAngle = 90 * 16; //旋转90°
// painter.drawChord(rect, startAngle, spanAngle);//3.drawConvexPolygon()
// QPoint points[4]={
// QPoint(5*W/12,H/4),QPoint(7*W/12,H/4),
// QPoint(3*W/4,5*H/12),QPoint(3*W/4,7*H/12),QPoint(7*W/12,3*H/4),
// QPoint(5*W/12,3*H/4),QPoint(W/4,7*H/12),
// QPoint(W/4,5*H/12),QPoint(5*W/12,H/4)
// };
// painter.drawConvexPolygon(points, 4);//4. drawEllipse
// QRect rect(W/4,H/4,W/2,H/2);
// painter.drawEllipse(rect);//5. drawImage
// QRect rect(W/4,H/4,W/2,H/2);
// QImage image(":images/images/qt.jpg");
// painter.drawImage(rect, image);//6. drawLine
// QLine Line(W/4,H/4,W/2,H/2);
// painter.drawLine(Line);//7. drawLines
// QRect rect(W/4,H/4,W/2,H/2);// QVector<QLine> Lines;
// Lines.append(QLine(rect.topLeft(),rect.bottomRight()));
// Lines.append(QLine(rect.topRight(),rect.bottomLeft()));
// Lines.append(QLine(rect.topLeft(),rect.bottomLeft()));
// Lines.append(QLine(rect.topRight(),rect.bottomRight()));// painter.drawLines(Lines);// 8.QPainterPath
// QRect rect(W/4,H/4,W/2,H/2);
// QPainterPath path;
// path.addEllipse(rect);
// path.addRect(rect);
// painter.drawPath(path);//9.drawPie
// QRect rect(W/4,H/4,W/2,H/2);
// int startAngle = 40 * 16;//起始40°
// int spanAngle = 120 * 16;//旋转120°
// painter.drawPie(rect, startAngle, spanAngle);//10. drawPixmap
// QRect rect(W/4,H/4,W/2,H/2);
// QPixmap pixmap(":images/images/qt.jpg");
// painter.drawPixmap(rect, pixmap);//11. drawPolygon
// QPoint points[]={
// QPoint(5*W/12,H/4),
// QPoint(3*W/4,5*H/12),
// QPoint(5*W/12,3*H/4),
// QPoint(2*W/4,5*H/12),
// };
// painter.drawPolygon(points, 4);//12.drawPolyline
// QPoint points[]={
// QPoint(5*W/12,H/4),
// QPoint(3*W/4,5*H/12),
// QPoint(5*W/12,3*H/4),
// QPoint(2*W/4,5*H/12)
// };
// painter.drawPolyline(points, 4);//13.drawRect
// QRect rect(W/4,H/4,W/2,H/2);
// painter.drawRect(rect);//14.drawRect
// QRect rect(W/4,H/4,W/2,H/2);
// painter.drawRoundedRect(rect,20,20);//14.drawRect
// QRect rect(W/4,H/4,W/2,H/2);
// QFont font;
// font.setPointSize(30);
// font.setBold(true);
// painter.setFont(font);
// painter.drawText (rect,"Hello,Qt");//. eraseRect
// QRect rect(W/4,H/4,W/2,H/2);
// painter.eraseRect(rect);//15. fillRectQRect rect(W/4,H/4,W/2,H/2);painter.fillRect (rect,Qt::green);//16. fillPath
// QRect rect(W/4,H/4,W/2,H/2);
// QPainterPath path;
// path.addEllipse(rect);
// path.addRect(rect);
// painter.fillPath(path,Qt::red);//17.drawPoint
// painter.drawPoint(QPoint(W/2,H/2));//18. drawPoints
// QPoint points[]={
// QPoint(5*W/12,H/4),
// QPoint(3*W/4,5*H/12),
// QPoint(2*W/4,5*H/12)
// };
// painter.drawPoints(points, 3);
}
绘制图片:
// QPixmap pixmap;
// pixmap.load(":/images/images/qt.jpg");
// painter.drawPixmap(rect.left(),rect.top(),pixmap);