Detais:
我有QGraphicScene,当我停止移动项目时,我想在Pixmap项目中使用dragLeaveEvent。但是,当我设置“拖放”时,项目会根据其坐标移动,但是我需要针对场景进行移动。
项目按dx,dy移动。
from PyQt5.QtCore import (QEasingCurve, QFileInfo, QLineF, QMimeData,
QParallelAnimationGroup, QPoint, QPointF, QPropertyAnimation, qrand,
QRectF, qsrand, Qt, QTime)
from PyQt5.QtGui import (QBrush, QColor, QDrag, QImage, QPainter, QPen,
QPixmap, QTransform)
from PyQt5.QtWidgets import (QApplication, QGraphicsItem, QGraphicsObject, QGraphicsPixmapItem,
QGraphicsScene, QGraphicsView)
class PixmapItem(QGraphicsPixmapItem):
n = 0
def __init__(self):
super().__init__()
self.setAcceptDrops(True)
self.setPixmap(QPixmap('hold.png'))
self.setCursor(Qt.OpenHandCursor)
def mousePressEvent(self, event):
self.setCursor(Qt.ClosedHandCursor)
def mouseMoveEvent(self, event):
drag = QDrag(event.widget())
mime = QMimeData()
drag.setMimeData(mime)
pos = event.pos()
self.moveBy(pos.x(), pos.y())
drag.exec_()
self.setCursor(Qt.OpenHandCursor)
def mouseReleaseEvent(self, event):
self.setCursor(Qt.OpenHandCursor)
def dragEnterEvent(self, e):
e.accept()
print('enter')
def dragLeaveEvent(self, e):
print('leave')
import sys
app = QApplication(sys.argv)
scene = QGraphicsScene()
view = QGraphicsView(scene)
pixmapItem = PixmapItem()
scene.addItem(pixmapItem)
view.setScene(scene)
view.show()
sys.exit(app.exec_())