QGraphicsPixmapItem is not being positioned correctly
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?
Code:
import sys
from PyQt5 import QtGui, QtWidgets
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.scene = Scene()
self.setGeometry(10, 30, 850, 600)
self.view.setGeometry(20, 22, 800, 550)
self.view.setScene(self.scene)
def __init__(self, parent=None):
super(Scene, self).__init__(parent)
# other stuff here
self.set_image()
def set_image(self):
image = Image()
self.addItem(image)
image.set_pixmap()
def __init__(self, parent=None):
super(Image, self).__init__(parent)
def set_pixmap(self):
pixmap
= QtGui.
QPixmap("image.jpg") self.setPixmap(pixmap)
self.pixmap_controller = PixmapController(self)
self.pixmap_controller.set_pixmap_controller()
self.pixmap_controller.setPos(self.boundingRect().topLeft())
self.
pixmap_controller.
setFlag(QtWidgets.
QGraphicsItem.
ItemSendsScenePositionChanges,
True)
def change_image_position(self, position):
self.setPos(position)
def __init__(self, pixmap):
super(PixmapController, self).__init__(parent=pixmap)
self.pixmap = pixmap
self.setBrush(brush)
def set_pixmap_controller(self):
self.setRect(-5, -5, 10, 10)
def itemChange(self, change, value):
self.pixmap.change_image_position(value)
return super(PixmapController, self).itemChange(change, value)
if __name__ == "__main__":
window = MainWindow()
window.show()
sys.exit(app.exec_())