I need to move a QGraphicsPixmapItem through a circle that it is at the top left corner of the image. That is, when I grab with the mouse the circle, I need the top left corner of the image to follow the circle. I subclassed a QGraphicsEllipseItem and reimplemented the itemChange method but when I set the position of the image to that value, the image is not being positioned correctly. What should I modify in my code?

Qt Code:
  1. import sys
  2. from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsView
  3. from PyQt5 import QtGui, QtWidgets
  4.  
  5. class MainWindow(QMainWindow):
  6. def __init__(self, parent=None):
  7. super(MainWindow, self).__init__(parent)
  8.  
  9. self.scene = Scene()
  10. self.view = QGraphicsView(self)
  11. self.setGeometry(10, 30, 850, 600)
  12. self.view.setGeometry(20, 22, 800, 550)
  13. self.view.setScene(self.scene)
  14.  
  15. class Scene(QtWidgets.QGraphicsScene):
  16. def __init__(self, parent=None):
  17. super(Scene, self).__init__(parent)
  18. # other stuff here
  19. self.set_image()
  20.  
  21. def set_image(self):
  22. image = Image()
  23. self.addItem(image)
  24. image.set_pixmap()
  25.  
  26. class Image(QtWidgets.QGraphicsPixmapItem):
  27. def __init__(self, parent=None):
  28. super(Image, self).__init__(parent)
  29.  
  30. self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
  31.  
  32. def set_pixmap(self):
  33. pixmap = QtGui.QPixmap("image.jpg")
  34. self.setPixmap(pixmap)
  35. self.pixmap_controller = PixmapController(self)
  36. self.pixmap_controller.set_pixmap_controller()
  37. self.pixmap_controller.setPos(self.boundingRect().topLeft())
  38. self.pixmap_controller.setFlag(QtWidgets.QGraphicsItem.ItemSendsScenePositionChanges, True)
  39.  
  40. def change_image_position(self, position):
  41. self.setPos(position)
  42.  
  43. class PixmapController(QtWidgets.QGraphicsEllipseItem):
  44. def __init__(self, pixmap):
  45. super(PixmapController, self).__init__(parent=pixmap)
  46. self.pixmap = pixmap
  47.  
  48. self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
  49. color = QtGui.QColor(0, 0, 0)
  50. brush = QtGui.QBrush(color)
  51. self.setBrush(brush)
  52.  
  53. def set_pixmap_controller(self):
  54. self.setRect(-5, -5, 10, 10)
  55.  
  56. def itemChange(self, change, value):
  57. if change == QtWidgets.QGraphicsItem.ItemPositionChange:
  58. self.pixmap.change_image_position(value)
  59. return super(PixmapController, self).itemChange(change, value)
  60.  
  61. if __name__ == "__main__":
  62. app = QApplication(sys.argv)
  63. window = MainWindow()
  64. window.show()
  65. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode