文章目录
1 QPainter 介绍
-
QPainter: 绘图操作的类
-
QPaintDevice: 抽象二维界面
-
QPaintEngine: 提供接口,用在不同设备上的绘制
QPainter 可以在任何继承QPaintDevice类上进行绘制操作
QPainter 在部件的绘图时间 paintEvent() 中绘制。使用 repaint() 和 update() 后,调用paintEvent()。
2 QPainter 主要配置
void QPainter::setPen(const QPen & pen) // 设置pen,用于画线配置
void QPainter::setBrush(const QBrush & brush) // 设置笔刷,用于填充配置
void QPainter::setRenderHint(RenderHint hint, bool on = true) // 设置渲染,如抗锯齿等
void QPainter::setFont(const QFont & font) // 设置字体,用于写字
2.1 QPen
相当于绘画路径的配置
void QPen::setWidth(int width) // 配置画线像素宽度
void QPen::setColor(const QColor & color) // 配置画线颜色
void QPen::setStyle(Qt::PenStyle style) // 配置线的类型,实现虚线等
void QPen::setCapStyle(Qt::PenCapStyle style) // 配置线段 端点的样式
2.2 QBrush
填充的配置
void QBrush::setColor(Qt::GlobalColor color)
void QBrush::setStyle(Qt::BrushStyle style)
2.3 QFont
void QFont::setPixelSize(int pixelSize) // 配置字体的像素大小
3 绘画
QPainter 类中,以draw开头的函数。
3.1 矩形
QRect::QRect(int x, int y, int width, int height) // 指定矩形的位置及大小
void QPainter::drawRect(const QRectF & rectangle) // 画出矩形
Example:
{
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
QPainter painter(this);
painter.drawRect(rectangle);
}
3.2 椭圆形
指定椭圆形所在的位置及高宽,然后画出来
void QPainter::drawEllipse(int x, int y, int width, int height)
Example:
{
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
QPainter painter(this);
painter.drawEllipse(rectangle);
}
3.3 弧形
角度值解释:一圈为16*360,正值为逆时针方向,负值为顺时针。
QRectF: 指定整弧形所在的矩形位置及大小,参考椭圆
startAngle:起始角度
spanAngle: 画线划过的角度
void QPainter::drawArc(const QRectF & rectangle, int startAngle, int spanAngle)
Example:
{
QRectF rectangle(10.0, 20.0, 60.0, 60.0);
int startAngle = 50 * 16;
int spanAngle = 120 * 16;
QPainter painter(this);
painter.drawArc(rectangle, startAngle, spanAngle);
}
3.4 多边形
void QPainter::drawPolygon(const QPointF * points, int pointCount, Qt::FillRule fillRule = Qt::OddEvenFill)
Example:
{
static const QPointF points[4] = {
QPointF(10.0, 80.0),
QPointF(20.0, 10.0),
QPointF(80.0, 30.0),
QPointF(90.0, 70.0)
};
QPainter painter(this);
painter.drawPolygon(points, 4);
}
3.5 多线线段
void QPainter::drawPolyline(const QPoint * points, int pointCount)
Example:
{
static const QPointF points[3] = {
QPointF(10.0, 80.0),
QPointF(20.0, 10.0),
QPointF(80.0, 30.0),
};
QPainter painter(this);
painter.drawPolyline(points, 3);
}
4 实例
widget 的图形绘制
.h:
void paintEvent(QPaintEvent * event) Q_DECL_OVERRIDE;
.c:
void Widget::paintEvent(QPaintEvent *event)
{
int w = this->width();
int h = this->height();
QRect rect(w/4, h/4, w/2, h/2);
QPen pen;
pen.setWidth(20);
pen.setColor(Qt::red);
pen.setStyle(Qt::SolidLine);
pen.setJoinStyle(Qt::RoundJoin);
QBrush brush;
brush.setColor(Qt::yellow);
brush.setStyle(Qt::Dense7Pattern);
QPainter painter(this);
painter.setPen(pen);
painter.setBrush(brush);
painter.setRenderHint(QPainter::TextAntialiasing,true);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawRect(rect);
pen.setColor(Qt::black);
painter.setPen(pen);
QFont font;
font.setPixelSize(30);
painter.setFont(font);
painter.drawText(rect, "Hello");
QPoint points[] = {
QPoint(w/3, h/3),
QPoint(w/2, h/2),
QPoint(w/2, 2*h/3)
};
painter.drawPolyline(points, 3);
QRectF rectangle(10.0, 20.0, 60.0, 60.0);
int startAngle = 50 * 16;
int spanAngle = 120 * 16;
painter.drawArc(rectangle, startAngle, spanAngle);
}