如果要移动鼠标左键,我想移动SimpleItem对象。如果按下对象,我已经成功获取了鼠标光标的位置。但是我不知道如何将物品移动到那个位置。你能帮我吗?
import sys
from PySide2 import QtGui, QtWidgets, QtCore
class SimpleItem(QtWidgets.QGraphicsItem):
def __init__(self):
QtWidgets.QGraphicsItem.__init__(self)
self.location = 1.0
def boundingRect(self):
penWidth = 1.0
return QtCore.QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
20 + penWidth, 20 + penWidth)
def paint(self, painter, option, widget):
rect = self.boundingRect()
painter.drawRect(rect)
def mousePressEvent(self, event):
print("hello")
def mouseMoveEvent(self, event):
print(event.pos())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
scene = QtWidgets.QGraphicsScene()
item = SimpleItem()
scene.addItem(item)
view = QtWidgets.QGraphicsView(scene)
view.show()
sys.exit(app.exec_())