I have a function that when you click a point it draws a path between that clicked point and start point. I do have a checkable button that activates and disables the tool, so I am trying to intergrate that. However, all of this has a major issue: I cannot draw multiple paths without it connecting from the last point of the original one, meaning I get this one infinetly drawable line. No matter me disabling or re-enabling the tool, it still starts from the original first drawn path.
elif event.button() == Qt.MouseButton.RightButton:
self.points.append(self.mapToScene(event.pos()))
if len(self.points) > 1:
path = QPainterPath()
path.moveTo(self.points[0])
for point in self.points[1:]:
path.lineTo(point) # Add line segment to existing path
# Create and configure CustomPathItem only once (outside loop)
if not hasattr(self, "path_item"):
self.path_item = CustomPathItem(path)
self.path_item.setPen(self.pen)
if self.button3.isChecked():
self.path_item.setBrush(QBrush(QColor(self.stroke_fill_color)))
self.path_item.setZValue(0)
self.path_item.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)
self.path_item.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsMovable)
self.canvas.addItem(self.path_item)
# Update path of existing item within the loop
self.path_item.setPath(path)
super().mousePressEvent(event)
I don’t know if I explained my issue, so if clarification is needed, please ask. Any help is appreciated.