如何在pyqt5中为小部件制作一个清晰的板子功能 [英] How to Make a Clear Board Function for a Widget in pyqt5

查看:59
本文介绍了如何在pyqt5中为小部件制作一个清晰的板子功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在使用 PyQt5 制作 gui 并且我集成了一个用于在主窗口上绘图的小部件,但我想使用一个按钮来清除该小部件进行绘图我正在使用此代码,但我没有得到它有什么问题;它似乎不起作用,谢谢

Hi every one I am working on PyQt5 to make a gui and I integrated a Widget for drawing on the Main Window but I want to use a button to clear that Widget for drawing I am using this code but I don't get what is wrong with it ;it doesn't seem to work thanks

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)

推荐答案

清除图片时需要清除路径,否则paint函数只会重新绘制相同的东西.

You need to clear the path when you clear the image, otherwise the paint function will just redraw the same thing again.

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

这篇关于如何在pyqt5中为小部件制作一个清晰的板子功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆