|
| 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 | + |
0 commit comments