Qt 中提供了强大的 2D 绘图系统,可以使用相同的 API 在屏幕上和绘图 · 设备上进行绘制,主要基于 QPainter、QPainterDevice 和 QPainterEngine 这 3 个类。QPainter 执行绘图操作,QPainterDevice 提供绘图设备,是一个二维空间的抽象,QPainterEngine 提供一些接口。QPainter 可以绘制一切简单的图形,从简单的一条直线到任何复杂的图形。QPainter 类可以在一切继承 QPainterDevice 的子类上进行绘制操作。

在 Qt Assistant 查 paintEvent() 函数,有如下的说明:

void QWidget::paintEvent ( QPaintEvent * event ) [virtual protected]

This event handler can be reimplemented in a subclass to receive paint events passed in event.

基础部件类 Qwidget 提供的 paintEvent 函数,是纯虚函数;继承它的子类想用它,必须重新实现它。下列三种情况会发生重绘事件:

a)当窗口部件第一次显示时,系统会自动产生一个绘图事件;
b)repaint()与 update()函数被调用时;
c)当窗口部件被其他部件遮挡,然后又再次显示出来时,就会对隐藏的区域产生一个重绘事件。

d) 重新调整窗口大小时。

(1)QBrush

画刷与画笔是 Qt 绘图时,重要属性;前者用 QBrush 描述,用来填充,后者用 QPen 描述,来绘制轮廓线。

QBrush 的属性我们可以从 QBrush 类的构造函数中看出,在 QtAssistant 中输入 QBrush;

QBrush ()
QBrush (Qt::BrushStyle style)
QBrush ( const QColor & color, Qt::BrushStyle style = Qt::SolidPattern )
QBrush ( Qt::GlobalColor color, Qt::BrushStyle style = Qt::SolidPattern )
QBrush ( const QColor & color, const QPixmap & pixmap )
QBrush ( Qt::GlobalColor color, const QPixmap & pixmap )
QBrush ( const QPixmap & pixmap )
QBrush ( const QImage & image )
QBrush ( const QBrush & other )
QBrush ( const QGradient & gradient )

可以看出 QBrush 定义了 QPainter 的填充模式(style),具有样式、颜色(QColor)、渐变(QGradient)以及纹理(QPximap)等属性。

style 定义了填充模式,通过枚举类型 Qt::BrushStyle 来实现,默认值是 Qt::NoBrush,不进行任何填充;填充模式包括基本填充模式,渐变填充,和纹理填充模式,下图是不同的填充模式区别.

画刷的 gradient() 定义了渐变填充。这个属性只有在样式是 Qt::LinearGradientPattern、Qt::RadialGradientPattern 或者 Qt::ConicalGradientPattern 之一时才有效。渐变可以由 QGradient 对象表示。Qt 提供了三种渐变:QLinearGradient、QConicalGradient 和 QRadialGradient,它们都是 QGradient 的子类。

当画刷样式是 Qt::TexturePattern 时,texture() 定义了用于填充的纹理。注意,即使你没有设置样式为 Qt::TexturePattern,当你调用 setTexture() 函数时,QBrush 会自动将 style() 设置为 Qt::TexturePattern。

画刷的 color 定义了填充的颜色,这个颜色可以使用 Qt 预定义的颜色常量(Qt::GlobalColor),也可以使用 QColor 对象。

(2)QPen

画笔用 QPen 类来实现,先看一下 QPen 类的构造函数,

QPen ()
QPen (Qt::PenStyle style)
QPen ( const QColor & color )
QPen ( const QBrush & brush, qreal width, Qt::PenStyle style = Qt::SolidLine, Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJoin )
QPen ( const QPen & pen )

包含了画笔实用的画刷,线宽,画笔风格,画笔端点风格,画笔连接风格;也可以使用对应的函数进行设置,

void setBrush (const QBrush & brush)
void setCapStyle ( Qt::PenCapStyle style )
void setColor ( const QColor & color )
void setJoinStyle ( Qt::PenJoinStyle style )
void setWidth ( int width )

  1. QPainter painter(this);
  2. QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
  3. painter.setPen(pen);

等价于

  1. QPainter painter(this);

  2. QPen pen;

  3. pen.setStyle(Qt::DashDotLine);

  4. pen.setWidth(3);

  5. pen.setBrush(Qt::green);

  6. pen.setCapStyle(Qt::RoundCap);

  7. pen.setJoinStyle(Qt::RoundJoin);

  8. painter.setPen(pen);

使用构造函数的优点是代码较短,但是参数含义不明确;使用 set 函数则正好反过来。 Pen Style: 用枚举类型 Qt::PenStyle 来定义

Cap Style:

The cap style defines how the end points of lines are drawn using QPainter. The cap style only apply to wide lines, i.e. when the width is 1 or greater. 当线宽比较大时,才能看书画笔端点风格的差别。

Join Style:

The join style defines how joins between two connected lines can be drawn using QPainter. The join style only apply to wide lines, i.e. when the width is 1 or greater. 当两个宽度大于 1 的线,端点连接时的风格;

Qt学习之2D绘图(画刷和画笔)_u012803067的博客-CSDN博客_qt的brush画笔填充 - 图1

代码:

使用画笔:

  1. void MyWidget::paintEvent(QPaintEvent *)

  2. {

  3. QPainter painter(this);

  4. painter.drawLine(0,0,100,100);

  5. QPen pen(Qt::green,5,Qt::DashLine,Qt::FlatCap,Qt::RoundJoin);

  6. painter.setPen(pen);

  7. QRectF rectangle(70.0, 40.0, 80.0, 60.0);

  8. int startAngle = 30 * 16;

  9. int spanAngle = 120 * 16;

  10. painter.drawArc(rectangle, startAngle, spanAngle);

  11. pen.setWidth(2);

  12. pen.setStyle(Qt::SolidLine);

  13. painter.setPen(pen);

  14. painter.drawRect(50,50,20,100);

  15. }

使用画刷:

  1. void MyWidget::paintEvent(QPaintEvent *)

  2. {

  3. QPainter painter(this);

  4. QBrush brush(QColor(0, 0, 255), Qt::Dense4Pattern);

  5. painter.setBrush(brush);

  6. painter.drawEllipse(220, 20, 50, 50);

  7. brush.setTexture(QPixmap(“../mydrawing/eryuelan.JPG”));

  8. painter.setBrush(brush);

  9. staticconst QPointF points[4] = {

  10. QPointF(270.0, 80.0),

  11. QPointF(290.0, 10.0),

  12. QPointF(350.0, 30.0),

  13. QPointF(390.0, 70.0)

  14. };

  15. painter.drawPolygon(points, 4);

  16. }

(1)线性渐变

Qt学习之2D绘图(画刷和画笔)_u012803067的博客-CSDN博客_qt的brush画笔填充 - 图2

(2)辐射渐变

QRadialGradient::QRadialGradient (const QPointF & center, qreal radius, const QPointF & focalPoint)
Constructs a radial gradient with the given center, radius and focalPoint.
需要指定圆心 center 和半径 radius,这样就可以确定一个圆,然后再指定一个焦点 focalPoint;焦点的位置为 0, 圆环的位置为 1,然后在焦点和圆环之间插入颜色。同样可以使用 setspread() 指定扩散方式。

  1. QRadialGradient radialGradient(QPointF(200, 190), 50, QPointF(275, 200));
  2. radialGradient.setColorAt(0, QColor(255, 255, 100, 150));
  3. radialGradient.setColorAt(1, QColor(0, 0, 0, 50));
  4. painter.setBrush(radialGradient);
  5. painter.drawEllipse(QPointF(200, 190), 50, 50);

(3)锥形渐变

QConicalGradient::QConicalGradient (const QPointF & center, qreal angle)
Constructs a conical gradient with the given center, starting the interpolation at the given angle. The angle must be specified in degrees between 0 and 360.
需要指定一个中心点和一个角度,角度在 0 到 360 度之间,沿逆时针从给定的角度开始环绕中心点插入颜色。

  1. QConicalGradient conicalGradient(QPointF(350, 190), 60);
  2. conicalGradient.setColorAt(0.2, Qt::cyan);
  3. conicalGradient.setColorAt(0.9, Qt::black);
  4. painter.setBrush(conicalGradient);
  5. painter.drawEllipse(QPointF(350, 190), 50, 50);
    https://blog.csdn.net/u012803067/article/details/74066949