PDA

View Full Version : [SOLVED] Unable to move QGraphicsItem (delta is always 0) when calculating using...



enjoysmath
8th August 2015, 19:10
Here's my mouseMoveEvent (PyQt5):

def mouseMoveEvent(self, event):
parentPos = self.mapToParent(event.pos())
# parentPos = event.pos()

if self.lastParentPos == None:
self.lastParentPos = parentPos

delta = parentPos - self.lastParentPos
print(delta.x(), ", ", delta.y())
self.setPos(self.pos() + delta)

if self.onPositionChange:
self.onPositionChange(self, delta)

self.lastParentPos = parentPos

I've also tried doing this with scene coords and various combinations, with / without mapTo/From. What am I doing wrong? Thank you!

Added after 18 minutes:

This code hacks around it: :)


def mouseMoveEvent(self, event):
pos = event.scenePos()

if self.lastPos == None:
self.lastPos = pos

delta = pos - self.lastPos
self.setPos(self.pos() + delta)

if self.onPositionChange:
self.onPositionChange(self, delta)

self.lastPos = pos

def mouseReleaseEvent(self, event):
self.lastPos = None