If you want an item not to change its position with respect to another item when the last item rotates or moves then it is only enough that the first item is the child of the second item:
import random
import sys
from PyQt4 import QtCore, QtGui
def create_pixmap(size):
pixmap = QtGui.QPixmap(size)
pixmap.fill(QtGui.QColor(*random.sample(range(255), 3)))
return pixmap
class VariantAnimation(QtCore.QVariantAnimation):
def updateCurrentValue(self, value):
pass
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = QtGui.QMainWindow()
scene = QtGui.QGraphicsScene(w)
view = QtGui.QGraphicsView(scene)
parent_item = scene.addPixmap(create_pixmap(QtCore.QSize(150, 150)))
parent_item.setTransformOriginPoint(parent_item.boundingRect().center())
parent_item.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
child_item = QtGui.QGraphicsPixmapItem(
create_pixmap(QtCore.QSize(70, 70)), parent_item
)
# or
# child_item = QtGui.QGraphicsPixmapItem(create_pixmap(QtCore.QSize(70, 70)))
# child_item.setParentItem(parent_item)
animation = VariantAnimation(
startValue=0, endValue=360, duration=1000, loopCount=-1
)
animation.valueChanged.connect(parent_item.setRotation)
animation.start()
w.setCentralWidget(view)
w.resize(640, 480)
w.show()
sys.exit(app.exec_())