forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Draw.py
93 lines (75 loc) · 2.82 KB
/
Draw.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2022年12月12日
@site: https://pyqt.site , https://github.com/PyQt5
@description: QPainter画图
"""
import sys
try:
from PyQt5.QtWidgets import QApplication, QWidget, qApp
from PyQt5.QtGui import QPainter, QFont, QColor, QPixmap
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.Qt import QPoint, QPolygon
except ImportError:
from PySide2.QtWidgets import QApplication, QWidget, qApp
from PySide2.QtGui import QPainter, QFont, QColor, QPixmap
from PySide2.QtCore import Qt, pyqtSignal
from PySide2.Qt import QPoint, QPolygon
class draw(QWidget):
drawsig = pyqtSignal(bool)
def __init__(self):
super(draw, self).__init__()
self.setWindowTitle("QPainter画图")
self._painter = QPainter()
self.scale = 1.0
self.pixmap = QPixmap()
self.setMouseTracking(True)
self.setFocusPolicy(Qt.WheelFocus)
self.drawEnable = False
self.points = []
self.current_points = []
self.drawsig.connect(self.setDrawEnable)
def setDrawEnable(self, enable=False):
self.drawEnable = enable
self.update()
def mouseMoveEvent(self, ev):
if self.drawEnable:
def in_end_range(curr, first):
return first.x() - 5 <= curr.x() <= first.x() + 5 and first.y() - 5 <= curr.y() <= first.y() + 5
if len(self.current_points) > 0 and in_end_range(ev.pos(), self.current_points[0]):
self.current_points.append(self.current_points[0])
self.points.append(self.current_points)
self.current_points = []
else:
self.current_points.append(ev.pos())
elif len(self.current_points) > 0:
self.current_points.append(ev.pos())
self.points.append(self.current_points)
self.current_points = []
self.update()
def mousePressEvent(self, ev):
if Qt.LeftButton & ev.button():
self.drawsig.emit(True)
def mouseReleaseEvent(self, ev):
if Qt.LeftButton & ev.button():
self.drawsig.emit(False)
def paintEvent(self, ev):
if len(self.points) <= 0 and len(self.current_points) <= 0 : return
p = self._painter
p.begin(self)
p.setRenderHint(QPainter.Antialiasing)
p.setRenderHint(QPainter.HighQualityAntialiasing)
p.setRenderHint(QPainter.SmoothPixmapTransform)
p.scale(self.scale, self.scale)
p.setPen(QColor(0, 0, 0))
for pts in self.points:
p.drawPolyline(QPolygon(pts))
if len(self.current_points) > 0:
p.drawPolyline(QPolygon(self.current_points))
p.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = draw()
mainWin.show()
sys.exit(app.exec_())