如何在pyqt5中为小部件创建一个清晰的板功能

时间:2017-10-18 20:08:12

标签: python pyqt pyqt5

大家好我正在PyQt5上制作一个gui,我在主窗口上集成了一个Widget用于绘图,但是我想用一个按钮来清除绘制的Widget我正在使用这个代码但是我没有'得到它的错误;它似乎没有工作,谢谢

class Drawer(QWidget):
    newPoint = pyqtSignal(QPoint)

    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setAttribute(QtCore.Qt.WA_StaticContents)
        self.modified = False
        self.scribbling = False
        imageSize = QtCore.QSize(9500, 9500)
        h=400
        w=400
        self.image = QtGui.QImage()
        self.image=QtGui.QImage(w,h,QtGui.QImage.Format_RGB32)
        self.path = QPainterPath()   


    def setPenColor(self, newColor):
        self.myPenColor = newColor

    def setPenWidth(self, newWidth):
        self.myPenWidth = newWidth

    def clearImage(self):

        self.image.fill(QtGui.qRgb(255, 255, 255))  ## switch it to else 
        self.modified = True
        self.update()


    def paintEvent(self, event):
        painter = QPainter(self)

        painter.setPen(QColor(0, 0, 0))

        painter.setFont(QFont('Decorative', 10))
        painter.drawImage(event.rect(), self.image)
        painter.drawPath(self.path)

    def mousePressEvent(self, event):
        self.path.moveTo(event.pos())
        self.update()

    def mouseMoveEvent(self, event):
        self.path.lineTo(event.pos())
        self.newPoint.emit(event.pos())
        self.update()

    def sizeHint(self):
        return QSize(200, 200)

1 个答案:

答案 0 :(得分:1)

清除图像时需要清除路径,否则绘图功能只会重新绘制相同的内容。

def clearImage(self):
    self.path = QPainterPath()   
    self.image.fill(QtGui.qRgb(255, 255, 255))  ## switch it to else 
    self.modified = True
    self.update()