帮我重写graphicsview类,并重写鼠标事件,实现调整鼠标拖动目标并能拉伸大小
时间: 2024-05-05 07:15:59 浏览: 76
以下是重写的GraphicsView类及鼠标事件:
```
class GraphicsView(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(GraphicsView, self).__init__(parent)
self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
self.setRenderHint(QtGui.QPainter.Antialiasing, True)
self.setRenderHint(QtGui.QPainter.SmoothPixmapTransform, True)
self.setResizeAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
self.current_item = None
self.resize_direction = None
self.resize_threshold = 10
def mousePressEvent(self, event: QtGui.QMouseEvent) -> None:
super(GraphicsView, self).mousePressEvent(event)
self.current_item = None
self.resize_direction = None
items = self.items(event.pos())
for item in items:
if isinstance(item, QtWidgets.QGraphicsRectItem):
self.current_item = item
rect = item.rect()
if rect.top() + self.resize_threshold >= event.pos().y():
if rect.left() + self.resize_threshold >= event.pos().x():
self.resize_direction = "top_left"
elif rect.right() - self.resize_threshold <= event.pos().x():
self.resize_direction = "top_right"
else:
self.resize_direction = "top"
elif rect.bottom() - self.resize_threshold <= event.pos().y():
if rect.left() + self.resize_threshold >= event.pos().x():
self.resize_direction = "bottom_left"
elif rect.right() - self.resize_threshold <= event.pos().x():
self.resize_direction = "bottom_right"
else:
self.resize_direction = "bottom"
elif rect.left() + self.resize_threshold >= event.pos().x():
self.resize_direction = "left"
elif rect.right() - self.resize_threshold <= event.pos().x():
self.resize_direction = "right"
def mouseMoveEvent(self, event: QtGui.QMouseEvent) -> None:
super(GraphicsView, self).mouseMoveEvent(event)
if self.current_item:
rect = self.current_item.rect()
dx = event.pos().x() - event.lastPos().x()
dy = event.pos().y() - event.lastPos().y()
if self.resize_direction == "top_left":
rect.setTop(rect.top() + dy)
rect.setLeft(rect.left() + dx)
elif self.resize_direction == "top_right":
rect.setTop(rect.top() + dy)
rect.setRight(rect.right() + dx)
elif self.resize_direction == "top":
rect.setTop(rect.top() + dy)
elif self.resize_direction == "bottom_left":
rect.setBottom(rect.bottom() + dy)
rect.setLeft(rect.left() + dx)
elif self.resize_direction == "bottom_right":
rect.setBottom(rect.bottom() + dy)
rect.setRight(rect.right() + dx)
elif self.resize_direction == "bottom":
rect.setBottom(rect.bottom() + dy)
elif self.resize_direction == "left":
rect.setLeft(rect.left() + dx)
elif self.resize_direction == "right":
rect.setRight(rect.right() + dx)
self.current_item.setRect(rect)
def mouseReleaseEvent(self, event: QtGui.QMouseEvent) -> None:
super(GraphicsView, self).mouseReleaseEvent(event)
self.current_item = None
self.resize_direction = None
```
这个类继承自QGraphicsView,并在mousePressEvent、mouseMoveEvent和mouseReleaseEvent方法中重新实现了鼠标事件,以实现调整鼠标拖动目标并能拉伸大小的功能。
在mousePressEvent方法中,我们检查鼠标是否按下了一个矩形项。如果是,我们将该项设置为当前项,并检查鼠标指针是否在项的边界上。如果是,我们将resize_direction设置为相应的方向。
在mouseMoveEvent方法中,如果我们有一个当前项和resize_direction,则我们使用鼠标移动的距离来调整项的大小和位置。
在mouseReleaseEvent方法中,我们清除当前项和resize_direction,以便下一次鼠标事件可以重新开始。
阅读全文