zoukankan      html  css  js  c++  java
  • Qt 2D绘图 分类: QT学习实践 2015-05-03 09:58 27人阅读 评论(0) 收藏

    原文出处: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 )
    
    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;  // creates a default pen  
    3.    
    4. pen.setStyle(Qt::DashDotLine);  
    5. pen.setWidth(3);  
    6. pen.setBrush(Qt::green);  
    7. pen.setCapStyle(Qt::RoundCap);  
    8. pen.setJoinStyle(Qt::RoundJoin);  
    9.    
    10. 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的线,端点连接时的风格;
    


    代码:

    使用画笔:

    1. void MyWidget::paintEvent(QPaintEvent *)  
    2. {  
    3.     QPainter painter(this);  
    4.     painter.drawLine(0,0,100,100);    //画直线  
    5.     //定义画笔  
    6.     QPen pen(Qt::green,5,Qt::DashLine,Qt::FlatCap,Qt::RoundJoin);  
    7.     painter.setPen(pen); //使用画笔  
    8.     QRectF rectangle(70.0, 40.0, 80.0, 60.0);  
    9.     int startAngle = 30 * 16;  
    10.     int spanAngle = 120 * 16;  
    11.     //绘制圆弧  
    12.     painter.drawArc(rectangle, startAngle, spanAngle);  
    13.   
    14.     /**********重新定义画笔***************/  
    15.     pen.setWidth(2);  
    16.     pen.setStyle(Qt::SolidLine);  
    17.   
    18.     painter.setPen(pen); //使用画笔  
    19.     painter.drawRect(50,50,20,100);  
    20. }  

    使用画刷:
    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.     //设置纹理  
    8.     brush.setTexture(QPixmap("../mydrawing/eryuelan.JPG"));  
    9.     //重新使用画刷  
    10.     painter.setBrush(brush);  
    11.     //定义四个点  
    12.     static const QPointF points[4] = {  
    13.         QPointF(270.0, 80.0),  
    14.         QPointF(290.0, 10.0),  
    15.         QPointF(350.0, 30.0),  
    16.         QPointF(390.0, 70.0)  
    17.     };  
    18.     //使用四个点绘制多边形  
    19.     painter.drawPolygon(points, 4);  
    20. }  

    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()指定扩散方式。
    1. //辐射渐变  
    2.    QRadialGradient radialGradient(QPointF(200, 190), 50, QPointF(275, 200));  
    3.    radialGradient.setColorAt(0, QColor(255, 255, 100, 150));  
    4.    radialGradient.setColorAt(1, QColor(0, 0, 0, 50));  
    5.    painter.setBrush(radialGradient);  
    6.    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);  
  • 相关阅读:
    元类,单例模式
    面向对象高阶
    类的三大特性---封装以及Property特性
    c# 中的string(神奇的string)
    c#中的equal和getHashCode
    linq中的Distinct的使用(附带IComparable和IComparer的复习和使用)
    flex布局完整示例
    flex布局中flex-basis的理解
    CSS两端对齐的效果;
    理解c#中扩展性代码
  • 原文地址:https://www.cnblogs.com/zclzqbx/p/4687143.html
Copyright © 2011-2022 走看看