from PyQt5.QtCore import Qt, QPointF from PyQt5.QtGui import QPen from PyQt5.QtWidgets import QGraphicsLineItem line = QGraphicsLineItem(QPointF(0, 0), QPointF(100, 100)) line.setPen(QPen(Qt.red, 2))
class MyScene(QGraphicsScene): def __init__(self): super().__init__() self.startPos = None def mousePressEvent(self, event): self.startPos = event.scenePos() def mouseMoveEvent(self, event): if self.startPos: endPos = event.scenePos() line = QGraphicsLineItem(self.startPos, endPos) self.addItem(line) def mouseReleaseEvent(self, event): self.startPos = NoneIn this example, a custom QGraphicsScene class is created that responds to mouse events. When the mouse is pressed, the start position of the line is stored. When the mouse is moved, a new line is created from the start position to the current position of the mouse and added to the scene. When the mouse is released, the start position is reset to None. This allows the user to create multiple lines by clicking and dragging the mouse across the scene.