1、QPainter
常用方法
方法 | 描述 |
---|---|
begin() | 开始在目标设备上绘制 |
drawArc() | 在起始角度和最终角度之间画弧 |
drawEliipse() | 在一个矩形内画一个椭圆 |
drawLine(int x1,int y1,int x2,int y2) | 绘制一条指定了端点坐标的直线,并且指定当前画笔位置为(x2,y2) |
drawPixmap() | 从图像文件中提取 Pixmap 并将其显示在指定位置 |
drawPolygon() | 使用坐标数组绘制多边形 |
drawRect(int x,int y,int w,int h) | 以给定的宽度和高度从左上角坐标(x,y)开始绘制一个矩形 |
drawText() | 显示给定坐标处的文字 |
fillRext() | 使用 QColor 参数填充矩形 |
setBrush() | 设置画笔风格 |
setPen() | 设置用于绘制的笔的颜色、大小和样式 |
setpen可选参数
参数 | 描述 |
---|---|
Qt.NoPen | 没有线 |
Qt.SolidLine | 一条简单的线 |
Qt.DashLine | 由一些像素分隔的短线 |
Qt.DotLine | 由一些像素分隔得点 |
Qt.DashDotLine | 轮流交替的点和短线 |
Qt.DashDotDotLine | 一条短线、两个点 |
Qt.MPenStyle | 画笔风格的掩码 |
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Author: yudengwu # @Date : 2020/8/20 import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * class Drawing(QWidget): def __init__(self, parent=None): super(Drawing, self).__init__(parent) self.setWindowTitle("在窗口中绘制文字") self.setWindowIcon(QIcon("head.png")) self.resize(300, 200) self.text = "你好呀,总裁余" def paintEvent(self, event): painter = QPainter() painter.begin(self) # 自定义绘制方法 self.draw_text(event, painter) painter.end() def draw_text(self, event, qp): # 设置画笔颜色 qp.setPen(QColor(168, 34, 3)) # 设置字体 qp.setFont(QFont('SimSun', 20)) # 绘制文字 qp.drawText(event.rect(), Qt.AlignCenter, self.text) if __name__ == "__main__": app = QApplication(sys.argv) demo = Drawing() demo.show() sys.exit(app.exec_())
解释:
首先定义文字
self.text = “你好呀,总裁余”
定义绘制事件
def paintEvent(self, event):
自定义绘制方法
def draw_text(self, event, qp):

绘制点线
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Author: yudengwu # @Date : 2020/8/20 import sys, math from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import Qt class Drawing(QWidget): def __init__(self, parent=None): super(Drawing, self).__init__(parent) self.resize(300, 200) self.setWindowTitle("在窗口中画点") self.setWindowIcon(QIcon("limi.jpg")) def paintEvent(self, event): # 初始化绘图工具 qp = QPainter() qp.begin(self) # 自定义画点方法 self.drawPoints(qp) qp.end() def drawPoints(self, qp): qp.setPen(Qt.red) size = self.size()#判断当前窗口大小 for i in range(1000): # 绘制正弦图形,周期 [-1000,1000] x = 100 * (- 1 + 2.0 * i / 1000) + size.width() / 2.0 y = -50 * math.sin((x - size.width() / 2.0) * math.pi / 50) + size.height() / 2.0 qp.drawPoint(x, y) if __name__ == "__main__": app = QApplication(sys.argv) win = Drawing() win.show() sys.exit(app.exec_())

2、QPen
QPen(钢笔)是基本图形对象,用于绘制直线、曲线或者给轮廓画出矩形、椭圆形、多边形以及其他形状等
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import Qt class Drawing(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 280, 270) self.setWindowTitle('钢笔样式例子') def paintEvent(self, e): qp = QPainter() qp.begin(self) self.drawLines(qp) qp.end() def drawLines(self, qp): pen = QPen(Qt.black, 2, Qt.SolidLine) qp.setPen(pen) qp.drawLine(20, 40, 250, 40) pen.setStyle(Qt.DashLine) qp.setPen(pen) qp.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) qp.setPen(pen) qp.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawLine(20, 160, 250, 160) pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) qp.drawLine(20, 200, 250, 200) pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 240, 250, 240) if __name__ == '__main__': app = QApplication(sys.argv) demo = Drawing() demo.show() sys.exit(app.exec_())

3、QBrush
QBrush(画刷),用于填充如矩形、椭圆形或多边形等形状。
QBrush 有三种类型:预定义、过渡和纹理图案。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77# -*- coding: utf-8 -*- """ 【简介】 绘图中QBrush 的例子 ,绘制九个不同样式的矩形。 """ import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import Qt class Drawing(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 365, 280) self.setWindowTitle('画刷例子') self.show() def paintEvent(self, e): qp = QPainter() qp.begin(self) self.drawLines(qp) qp.end() def drawLines(self, qp): brush = QBrush(Qt.SolidPattern) qp.setBrush(brush) qp.drawRect(10, 15, 90, 60) brush = QBrush(Qt.Dense1Pattern) qp.setBrush(brush) qp.drawRect(130, 15, 90, 60) brush = QBrush(Qt.Dense2Pattern) qp.setBrush(brush) qp.drawRect(250, 15, 90, 60) brush = QBrush(Qt.Dense3Pattern) qp.setBrush(brush) qp.drawRect(10, 105, 90, 60) brush = QBrush(Qt.DiagCrossPattern) qp.setBrush(brush) qp.drawRect(10, 105, 90, 60) brush = QBrush(Qt.Dense5Pattern) qp.setBrush(brush) qp.drawRect(130, 105, 90, 60) brush = QBrush(Qt.Dense6Pattern) qp.setBrush(brush) qp.drawRect(250, 105, 90, 60) brush = QBrush(Qt.HorPattern) qp.setBrush(brush) qp.drawRect(10, 195, 90, 60) brush = QBrush(Qt.VerPattern) qp.setBrush(brush) qp.drawRect(130, 195, 90, 60) brush = QBrush(Qt.BDiagPattern) qp.setBrush(brush) qp.drawRect(250, 195, 90, 60) if __name__ == '__main__': app = QApplication(sys.argv) demo = Drawing() demo.show() sys.exit(app.exec_())

4、QPixmap
QPixmap 类用于绘图设备的图像显示,可作为一个 QPaintDevice 对象,也可以加载到一个控件中。
QPixmap 可以读取的图像文件类型有:BMP、GIF、JPG、JPEG、PNG、PBM、PGM、PPM、XBM、XPM 等。
QPixmap 类的常用方法如下:
方法 | 描述 |
---|---|
copy() | 从 QRect 对象复制到 QPixmap 对象 |
fromIamge() | 将 QImage 对象转换为 QPixmap 对象 |
grabWidget() | 从给定的窗口小控件创建一个像素图 |
grabWindow() | 在窗口中创建数据的像素图 |
load() | 加载图像文件作为 QPixmap 对象 |
save() | 将 QPixmap 对象保存为文件 |
toImage() | 将 QPixmap 对象转换为 QImage 对象 |
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27# -*- coding: utf-8 -*- ''' 【简介】 PyQt5中 QPixmap 例子 ''' import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * if __name__ == '__main__': app = QApplication(sys.argv) win = QWidget() lab1 = QLabel() lab1.setPixmap(QPixmap("D:机器学习学习草稿duoren.jpg")) vbox=QVBoxLayout() vbox.addWidget(lab1) win.setLayout(vbox) win.setWindowTitle("QPixmap 例子") win.show() sys.exit(app.exec_())

这篇博文内容来源于书籍《pyqt5快速开发与实战》
我只是记录下有用的信息,方便以后复习查找。
不然学的东西又忘啦。
电气专业的计算机萌新,写博文不容易。如果你觉得本文对你有用,请点个赞支持下,谢谢。
最后
以上就是心灵美机器猫最近收集整理的关于pyqt5讲解5:窗口绘图类控件QPainter,QPen,QBrush,QPixmap的全部内容,更多相关pyqt5讲解5:窗口绘图类控件QPainter内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复