Skip to content

Commit 9e5ea33

Browse files
committed
Add Qpainter
1 parent 2e0d014 commit 9e5ea33

File tree

6 files changed

+407
-1
lines changed

6 files changed

+407
-1
lines changed

QPainter/Data/qt-logo.png

2.63 KB
Loading

QPainter/Draw.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2022年12月12日
6+
@site: https://pyqt.site , https://github.com/PyQt5
7+
@description: QPainter画图
8+
"""
9+
import sys
10+
try:
11+
from PyQt5.QtWidgets import QApplication, QWidget, qApp
12+
from PyQt5.QtGui import QPainter, QFont, QColor, QPixmap
13+
from PyQt5.QtCore import Qt, pyqtSignal
14+
from PyQt5.Qt import QPoint, QPolygon
15+
except ImportError:
16+
from PySide2.QtWidgets import QApplication, QWidget, qApp
17+
from PySide2.QtGui import QPainter, QFont, QColor, QPixmap
18+
from PySide2.QtCore import Qt, pyqtSignal
19+
from PySide2.Qt import QPoint, QPolygon
20+
21+
22+
23+
class draw(QWidget):
24+
25+
26+
drawsig = pyqtSignal(bool)
27+
28+
def __init__(self):
29+
super(draw, self).__init__()
30+
self.setWindowTitle("QPainter画图")
31+
self._painter = QPainter()
32+
self.scale = 1.0
33+
self.pixmap = QPixmap()
34+
self.setMouseTracking(True)
35+
self.setFocusPolicy(Qt.WheelFocus)
36+
self.drawEnable = False
37+
self.points = []
38+
self.current_points = []
39+
self.drawsig.connect(self.setDrawEnable)
40+
41+
42+
def setDrawEnable(self, enable=False):
43+
self.drawEnable = enable
44+
self.update()
45+
46+
def mouseMoveEvent(self, ev):
47+
if self.drawEnable:
48+
def in_end_range(curr, first):
49+
return first.x() - 5 <= curr.x() <= first.x() + 5 and first.y() - 5 <= curr.y() <= first.y() + 5
50+
51+
if len(self.current_points) > 0 and in_end_range(ev.pos(), self.current_points[0]):
52+
self.current_points.append(self.current_points[0])
53+
self.points.append(self.current_points)
54+
self.current_points = []
55+
else:
56+
self.current_points.append(ev.pos())
57+
elif len(self.current_points) > 0:
58+
self.current_points.append(ev.pos())
59+
self.points.append(self.current_points)
60+
self.current_points = []
61+
62+
self.update()
63+
64+
def mousePressEvent(self, ev):
65+
if Qt.LeftButton & ev.button():
66+
self.drawsig.emit(True)
67+
def mouseReleaseEvent(self, ev):
68+
if Qt.LeftButton & ev.button():
69+
self.drawsig.emit(False)
70+
71+
def paintEvent(self, ev):
72+
if len(self.points) <= 0 and len(self.current_points) <= 0 : return
73+
p = self._painter
74+
p.begin(self)
75+
p.setRenderHint(QPainter.Antialiasing)
76+
p.setRenderHint(QPainter.HighQualityAntialiasing)
77+
p.setRenderHint(QPainter.SmoothPixmapTransform)
78+
p.scale(self.scale, self.scale)
79+
p.setPen(QColor(0, 0, 0))
80+
for pts in self.points:
81+
p.drawPolyline(QPolygon(pts))
82+
if len(self.current_points) > 0:
83+
p.drawPolyline(QPolygon(self.current_points))
84+
p.end()
85+
86+
87+
88+
if __name__ == '__main__':
89+
app = QApplication(sys.argv)
90+
mainWin = draw()
91+
mainWin.show()
92+
sys.exit(app.exec_())
93+

QPainter/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
# QPainter
1+
# QPainter
2+
3+
4+
- 目录
5+
- [利用QPainter绘制各种图形](#QPainter绘制各种图形)
6+
- [简易画板](#简易画板)
7+
8+
## 1、QPainter绘制各种图形
9+
[运行 StockDialog.py](StockDialog.py)
10+
11+
![CountDownClose](ScreenShot/StockDialog.gif)
12+
13+
## 2、简易画板
14+
[运行 Draw.py](Draw.py)
15+
16+
![CustomColorIcon](ScreenShot/Draw.gif)

QPainter/ScreenShot/Draw.gif

2.14 MB
Loading

QPainter/ScreenShot/StockDialog.gif

5.39 MB
Loading

0 commit comments

Comments
 (0)