```python
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Demo.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import math
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QPoint, QPointF
from PyQt5.QtGui import QPainter, QPen, QPainterPath, QPolygonF, QColor, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QFrame, QSizePolicy, QColorDialog, QHBoxLayout, QPushButton, \
QComboBox, QSpinBox, QFormLayout, QLabel
class MyLabel(QLabel):
def __init__(self, parent=None):
super(MyLabel, self).__init__(parent)
self.flag = None
self.setLineWidth(1)
self.setMidLineWidth(0)
self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
self.x0 = 0
self.y0 = 0
self.x1 = 0
self.y1 = 0
self.penColor = (QColor(255, 0, 0, 50))
self.lastpenColor = (QColor(255, 0, 0, 50))
self.status = True
self.penWidth = 30
self.penStyle = Qt.SolidLine
self.capStyle = Qt.RoundCap
self.pathList = []
self.path = QPainterPath()
def setPenColor(self, color):
self.lastpenColor = color
self.penColor = color
def setDrawStatus(self, status):
if status == True:
self.penColor = self.lastpenColor
self.status = True
elif status == False:
self.penColor = (QColor(0, 0, 0, 0))
self.status = False
def widthChange(self, value):
self.penWidth = value
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing, True)
# 绘制边框线
painter.drawRect(self.rect())
# painter.setPen(QPen(self.penColor, self.penWidth, self.penStyle, self.capStyle))
# painter.drawLine(self.x0, self.y0, self.x1, self.y1)
for p, d in self.pathList:
painter.setPen(QPen(*d))
painter.drawPath(p)
def mousePressEvent(self, event):
self.path = QPainterPath()
self.pathList.append([self.path, (self.penColor, self.penWidth, self.penStyle, self.capStyle)])
self.path.moveTo(event.pos())
self.flag = True
self.update()
def mouseMoveEvent(self, event):
if self.flag:
self.path.lineTo(event.pos())
self.update()
def mouseReleaseEvent(self, event):
self.flag = False
class Widget(QWidget):
def __init__(self,parent=None):
super(Widget,self).__init__(parent)
self.resize(1000, 700)
self.initUi()
def initUi(self):
self.canvas = MyLabel()
layout = QHBoxLayout()
btnPen = QPushButton('画笔')
btnPen.clicked.connect(self.DrawOn)
penColor = QComboBox()
penColor.addItems(['红色 肿瘤','绿色 间质', '紫色 坏死', '蓝色 淋巴'])
penColor.currentTextChanged.connect(self.ColorChange)
btnEraser = QPushButton('橡皮擦')
btnEraser.clicked.connect(self.DrawOff)
penWidth = QSpinBox()
penWidth.resize(20,30)
penWidth.setRange(20,60)
penWidth.setValue(30)
penWidth.valueChanged.connect(self.canvas.widthChange)
fLayout = QFormLayout()
fLayout.setContentsMargins(0, 0, 0, 0)
fLayout.addRow(btnPen)
fLayout.addRow(penColor)
fLayout.addRow(btnEraser)
fLayout.addRow(penWidth)
wid_left = QWidget()
wid_left.setMaximumWidth(200)
wid_left.setLayout(fLayout)
layout.addWidget(wid_left)
layout.addWidget(self.canvas)
self.setLayout(layout)
def DrawOn(self):
self.canvas.setDrawStatus(True)
def ColorChange(self, color):
if color == '红色 肿瘤':
self.canvas.setPenColor(QColor(255, 0, 0, 50))
elif color == '绿色 间质':
self.canvas.setPenColor(QColor(0, 255, 0, 50))
elif color == '紫色 坏死':
self.canvas.setPenColor(QColor(153, 0, 255, 50))
elif color == '蓝色 淋巴':
self.canvas.setPenColor(QColor(0, 0, 255, 50))
def DrawOff(self):
self.canvas.setDrawStatus(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Widget()
window.show()
sys.exit(app.exec())
提供两个思路:
方法一:点击擦除按钮后,再鼠标移动槽函数中,记录鼠标移动的点,并判断点是否再已绘的path中,如果在就把原来的path分割成两个path,并把点从新path中删掉,然后update重绘。
方法二:点击擦除按钮后,在鼠标移动槽函数中,记录鼠标移动的点,并用画板的背景色绘制这条path(你原来的path怎么绘制的,这个擦除的path也怎么绘制,区别是,擦除的path采用的颜色是画板的背景色)
两种方法的比较:
方法一:逻辑会比较复杂,特别是在分割path的时候,频繁的重绘可能影响程序的稳定性,容易出错。
方法二:逻辑相对简单,但是需要记录每条path的绘制顺序及颜色,如果你之前绘制的时候没有记录这些数据,可能需要调整你的数据结构。
推荐采用第二种方法。
其实无非是2个办法
1.你的底板要用纯色,不要用一个图像
那么所谓擦除,无非就是用backcolor来绘制而已,相当于一个跟底色一样的画笔
2.你要记录下之前所有的绘制动作,在擦除后将之前的全部重新绘制一遍