1.QPainter简介
PyQt5中QPainter提供了绘制图形的基本功能,用于绘制各种图形。QPainter通常在重写的 paintEvent 方法中使用。
2.QPainter使用过程
要使用 QPainter 进行绘图,需要遵循以下步骤:
- 开始绘图:
- 创建一个 QPainter 对象,并开始在绘图设备上绘图。
- 使用 begin 方法开启绘图过程。
- 绘图操作:
- 使用 QPainter 的绘图方法(如 drawLine, drawRect, drawText 等)进行绘图。
- 结束绘图:
- 结束绘图后,需要调用 end 方法来完成绘图过程。
3.QPainter案例
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QPixmap, QBrush, QPen, QImage
from PyQt5.QtCore import Qt, QPoint, QRect
class QPainterDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QPainter演示')
self.resize(600, 600)
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
self.draw_shapes(painter)
painter.end()
def draw_shapes(self, painter):
# 设置画笔颜色
pen = QPen(Qt.blue, 2, Qt.SolidLine)
painter.setPen(pen)
# 设置填充颜色
brush = QBrush(Qt.red, Qt.Dense4Pattern)
painter.setBrush(brush)
# 绘制矩形
rect = QRect(10, 15, 90, 60)
painter.drawRect(rect)
# 绘制椭圆
painter.drawEllipse(130, 15, 90, 60)
# 绘制带弦的图形
painter.drawChord(0, 110, 100, 100, 0, 180 * 16)
# 绘制扇形
painter.drawPie(0, 220, 100, 100, 0, 90 * 16)
# 绘制弧
painter.drawArc(150, 100, 100, 100, 0, 45 * 16)
# 绘制直线
painter.drawLine(300, 100, 500, 200)
# 绘制文本
painter.drawText(200, 220, "Hello, PyQt!")
# 绘制多边形
points = [
(20, 280), (120, 280), (60, 380),
(200, 280), (300, 280), (240, 380)
]
polygon = [QPoint(x, y) for x, y in points]
painter.drawPolygon(polygon)
# 绘制图像
img = QImage('../icon-1.png')
# img.save('xxx') # 保存图片
# 定义绘图区域,缩小到1/4
rect = QRect(10, 400, int(img.width() / 4), int(img.height() / 4))
painter.drawImage(rect, img)
# 绘制位图
pixmap = QPixmap('../icon-1.png')
rect = QRect(300, 400, 300, 300)
painter.drawPixmap(rect, pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QPainterDemo()
w.show()
sys.exit(app.exec_())
4.运行结果
5.常用方
方法 | 描述 |
setPen(QPen pen) | 设置绘图时使用的画笔样式 |
QPen(brush: QColor, width: int, style: Qt.PenStyle, cap: Qt.PenCapStyle, join: Qt.PenJoinStyle) | 内置类,用于定义线条的颜色、宽度和样式。参考【QPen可用样式】 |
setBrush(QBrush brush) | 设置绘图时使用的填充样式 |
QBrush() | 类用于定义填充的颜色和样式 |
setFont(QFont font) | 设置字体 |
font() | 返回当前使用的字体 |
begin(QWidget *device) | 开始在一个设备上绘图 |
end() | 结束绘图 |
drawRect(QtCore.QRect) drawRect(x: int, y: int, w: int, h: int) | 绘制矩形 |
drawLine(x1: int, y1: int, x2: int, y2: int) drawLine(p1: QPoint, p2: QPoint) | 绘制直线 |
drawEllipse(x: int, y: int, w: int, h: int) drawRect(QtCore.QRect) | 绘制椭圆 |
drawArc(x: int, y: int, w: int, h: int, a: int, alen: int) drawArc(r: QRect, a: int, alen: int) | 绘制弧线,参数a:起始位置,alen:结束位置 解释:单位alen,1个alen等于1/16度, 绘制45度弧,为45 * 16 |
drawPie(rect: QRect, a: int, alen: int) drawPie(x: int, y: int, w: int, h: int, a: int, alen: int) | 绘制扇形,参数a:起始位置,alen:结束位置 解释:单位alen,1个alen等于1/16度, 绘制45度弧,为45 * 16 |
drawPolygon(points: list[QPoint]) | 绘制多边形 |
drawChord(rect: QRect, a: int, alen: int) drawChord(x: int, y: int, w: int, h: int, a: int, alen: int) | 绘制带弦的图形,参数a:起始位置,alen:结束位置 解释:单位alen,1个alen等于1/16度, 绘制45度弧,为45 * 16 |
drawImage(r: QRect, image: QImage) | 绘制图像 |
drawText(x: int, y: int, s: Optional[str]) | 绘制文本 |
drawPixmap(r: QRect, pm: QPixmap) drawPixmap(x: int, y: int, w: int, h: int, pm: QPixmap) | 绘制位图 |
- QPen方法可用样式
- 样式 (Qt.PenStyle):
- Qt.NoPen: 不绘制任何线条。
- Qt.SolidLine: 实线(默认)。
- Qt.DashLine: 虚线。
- Qt.DotLine: 点线。
- Qt.DashDotLine: 间隔虚线。
- Qt.DashDotDotLine: 两点间隔虚线。
- Qt.CustomDashLine: 自定义虚线模式,需要通过 setDashPattern() 和 setDashOffset() 方法来设置。
- 笔帽样式 (Qt.PenCapStyle):
- Qt.FlatCap: 平头。
- Qt.SquareCap: 方头。
- Qt.RoundCap: 圆头(默认)。
- 连接样式 (Qt.PenJoinStyle):
- Qt.MiterJoin: 尖锐连接(默认)。
- Qt.BevelJoin: 斜接连接。
- Qt.RoundJoin: 圆滑连接。