您的位置:首页 > 编程语言 > Qt开发

Qt学习之2D绘图(画刷和画笔)

2017-07-28 16:22 786 查看
转载自:http://blog.csdn.net/lpp0900320123/article/details/25246873

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

1.重绘事件的处理函数 :paintEvent()

在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) 重新调整窗口大小时。

2.画刷与画笔

(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 )


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


等价于

QPainter painter(this);
QPen pen;  // creates a default pen

pen.setStyle(Qt::DashDotLine);
pen.setWidth(3);
pen.setBrush(Qt::green);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);

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的线,端点连接时的风格;



代码:

使用画笔:

void MyWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawLine(0,0,100,100);    //画直线
//定义画笔
QPen pen(Qt::green,5,Qt::DashLine,Qt::FlatCap,Qt::RoundJoin);
painter.setPen(pen); //使用画笔
QRectF rectangle(70.0, 40.0, 80.0, 60.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
//绘制圆弧
painter.drawArc(rectangle, startAngle, spanAngle);

/**********重新定义画笔***************/
pen.setWidth(2);
pen.setStyle(Qt::SolidLine);

painter.setPen(pen); //使用画笔
painter.drawRect(50,50,20,100);
}


使用画刷:

void MyWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QBrush brush(QColor(0, 0, 255), Qt::Dense4Pattern);//创建画刷
painter.setBrush(brush);                          //使用画刷
painter.drawEllipse(220, 20, 50, 50);            //绘制椭圆
//设置纹理
brush.setTexture(QPixmap("../mydrawing/eryuelan.JPG"));
//重新使用画刷
painter.setBrush(brush);
//定义四个点
static const QPointF points[4] = {
QPointF(270.0, 80.0),
QPointF(290.0, 10.0),
QPointF(350.0, 30.0),
QPointF(390.0, 70.0)
};
//使用四个点绘制多边形
painter.drawPolygon(points, 4);
}


3.渐变填充

(1)线性渐变



(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()指定扩散方式。

//辐射渐变
QRadialGradient radialGradient(QPointF(200, 190), 50, QPointF(275, 200));
radialGradient.setColorAt(0, QColor(255, 255, 100, 150));
radialGradient.setColorAt(1, QColor(0, 0, 0, 50));
painter.setBrush(radialGradient);
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度之间,沿逆时针从给定的角度开始环绕中心点插入颜色。

QConicalGradient conicalGradient(QPointF(350, 190), 60);
conicalGradient.setColorAt(0.2, Qt::cyan);
conicalGradient.setColorAt(0.9, Qt::black);
painter.setBrush(conicalGradient);
painter.drawEllipse(QPointF(350, 190), 50, 50);


ba3c
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  qt 2d