简体   繁体   English

如何擦除 PyQt5 QPainter 中的点?

[英]How to erase point in PyQt5 QPainter?

I want to display the x and y coordinates from an accelerometer on an image.我想在图像上显示加速度计的 x 和 y 坐标。 I used QPainter to set up the image and the drawPoint function to draw the coordinate at the given point.我使用 QPainter 设置图像和 drawPoint function 在给定点绘制坐标。 When the new coordinate is drawn, I need to erase the old coordinate so that there is only one at a time.绘制新坐标时,我需要擦除旧坐标,以便一次只有一个。 In the code below, I called drawCoordinate twice, what happens is the target image ends up with 2 coordinate points at 0,0 and 0.5,0.5 but ideally I would be left with the last drawn coordinate.在下面的代码中,我调用了 drawCoordinate 两次,发生的情况是目标图像在 0,0 和 0.5,0.5 处有 2 个坐标点,但理想情况下我会留下最后一个绘制的坐标。

This is my first question on here so I hope my format is correct.这是我在这里的第一个问题,所以我希望我的格式是正确的。 Let me know if I need to fix anything to help with clarity.让我知道是否需要解决任何问题以帮助清晰。

具有 2 个坐标的目标图像


class Target(QWidget):

    def __init__(self):
        super().__init__()
        self.drawing = False
        self.image = QPixmap(r"Pictures\target_png_300.png")
        self.setGeometry(0, 0, 300, 300)
        self.resize(self.image.width(), self.image.height())
        self.show()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(self.rect(), self.image)
    
    def paintCoordinate(self, x, y):
        painter = QPainter(self.image)
        r = QRect(-1, -1, 2, 2)
        painter.setWindow(r)
        pen = QPen(Qt.black, 0.06, Qt.DotLine, Qt.RoundCap)
        painter.setPen(pen)
        painter.drawPoint(QPointF(x, y))

if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Target()
    ex.paintCoordinate(0, 0)
    ex.paintCoordinate(0.5, 0.5)
    sys.exit(app.exec_())

If you want only one point then don't modify the QPixmap but just do the painting in the paintEvent method:如果您只想要一个点,则不要修改 QPixmap,而只需在 paintEvent 方法中进行绘制:

class Target(QWidget):
    def __init__(self):
        super().__init__()
        self._pixmap = QPixmap()
        self._coordinate = QPointF()

    @property
    def pixmap(self):
        return self._pixmap

    @pixmap.setter
    def pixmap(self, pixmap):
        self._pixmap = pixmap.copy()
        self.update()
        size = self.pixmap.size()
        if size.isValid():
            self.resize(size)
        else:
            self.resize(300, 300)

    @property
    def coordinate(self):
        return self._coordinate

    @coordinate.setter
    def coordinate(self, point):
        self._coordinate = point
        self.update()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(self.rect(), self.pixmap)
        r = QRect(-1, -1, 2, 2)
        painter.setWindow(r)
        pen = QPen(Qt.black, 0.06, Qt.DotLine, Qt.RoundCap)
        painter.setPen(pen)
        painter.drawPoint(self.coordinate)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    ex = Target()
    ex.pixmap = QPixmap(r"Pictures\target_png_300.png")
    ex.coordinate = QPointF(0, 0)
    ex.coordinate = QPointF(0.5, 0.5)
    ex.show()
    sys.exit(app.exec_())

Drawing on a raster image means overriding its contents, there's no "erase" nor "undo": it's like using a brush on a painting: if you try to "erase" it's like using bleach on what you're trying to "unpaint".在光栅图像上绘图意味着覆盖其内容,没有“擦除”或“撤消”:这就像在绘画上使用画笔:如果您尝试“擦除”它就像在您尝试“取消绘制”的东西上使用漂白剂.

Keep track of what you're manually painting (the coordinates) and then implement the paintEvent accordingly.跟踪您手动绘制的内容(坐标),然后相应地实现paintEvent

class Target(QWidget):

    def __init__(self):
        super().__init__()
        self.drawing = False
        self.image = QPixmap(r"Pictures\target_png_300.png")
        self.setGeometry(0, 0, 300, 300)
        self.resize(self.image.width(), self.image.height())
        self.points = []
        self.show()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(self.rect(), self.image)
        r = QRect(-1, -1, 2, 2)
        painter.setWindow(r) painter.setPen(QPen(Qt.black, 0.06, Qt.DotLine, Qt.RoundCap)) for point in self.points: painter.drawPoint(point)
    
    def paintCoordinate(self, x, y):
        self.points.append(QPointF(x, y)) self.update()

    def deleteLastPoint(self):
        self.points.pop()
        self.update()


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    ex = Target()
    ex.paintCoordinate(0, 0)
    ex.paintCoordinate(0.5, 0.5)
    QTimer.singleShot(1000, lambda: ex.deleteLastPoint())
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM