刚刚开始学习Qt不久,才开始渐渐地熟悉基础内容,学习过程中的一些知识的总结和感悟希望通过博客记录下来,与大家分享学习的同时,也是对自己坚持下去的鞭策,废话不多说了,开始第一次的小总结吧。
Qt提供了强大的2D绘图系统,主要基于QPainter、QPaintDevice、QPaintEngine这三个类。
其中QPainter用于执行绘图操作,QPaintDevice提供绘图设备,是一个二维空间的抽象,QPaintEngine提供一些接口,可以用于QPainter在不同的设备上进行会绘制。
基本的绘制与填充
-
- 绘制图形QPainter类的使用,其有两种构造函数:
1 //构造函数一 2 QPainter painter(this); 3 ... 4 //构造函数二 5 QPainter painter; 6 painter.begin(this); 7 ... 8 painter.end();
this表明绘图设备,构造函数一中begin()自动调用,创建对象时立即开始绘图,在析构函数中调用end()结束绘图。注意:无论使用哪种方式都必须指定绘图设备,否则无法绘制。
- 使用画笔
1 QPen() 2 QPen(Qt::PenStyle style) 3 QPen(const QColor & color) 4 QPen(const QBrush & brush, qreal width, Qt::PenStyle style = Qt::SolidLine, Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJoin) 5 QPen(const QPen & pen) 6 QPen(QPen && pen)
在第4行的构造函数中,QBrush brush表示画笔使用的画刷,qreal width表示线宽,Qt::PenStyle style表示画笔风格,Qt::PenCapStyle cap表示画笔端点风格,Qt::PenJoinStyle join表示画笔连接风格。这几个参数也可以在创建对象后使用函数setBrush()、setWidth()、setStyle()、setCapStyle()、setJoinStyle()来设置。
- 使用画刷
1 QBrush() 2 QBrush(Qt::BrushStyle style) 3 QBrush(const QColor & color, Qt::BrushStyle style = Qt::SolidPattern) 4 QBrush(Qt::GlobalColor color, Qt::BrushStyle style = Qt::SolidPattern) 5 QBrush(const QColor & color, const QPixmap & pixmap) 6 QBrush(Qt::GlobalColor color, const QPixmap & pixmap) 7 QBrush(const QPixmap & pixmap) 8 QBrush(const QImage & image) 9 QBrush(const QBrush & other) 10 QBrush(const QGradient & gradient)
- 渐变填充Qt支持三种渐变:
- 绘制图形QPainter类的使用,其有两种构造函数:
-
-
- 线性渐变 QLinearGradient
- 辐射渐变 QRadialGradient
- 锥形渐变 QConicalGradient
-