1 Star 0 Fork 309

Qt(开源集合) / PyQt

forked from PyQt5 / PyQt 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Draw.py 2.82 KB
一键复制 编辑 原始数据 按行查看 历史
zkep 提交于 2022-12-12 15:28 . Add Qpainter
#!/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_())
Python
1
https://gitee.com/qt-open-source-collection/PyQt.git
git@gitee.com:qt-open-source-collection/PyQt.git
qt-open-source-collection
PyQt
PyQt
master

搜索帮助