如果您希望一个项目在最后一个项目旋转或移动时不改变其相对于另一个项目的位置,那么第一个项目是第二个项目的子项目就足够了:
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_())