1 回答

TA貢獻(xiàn)1812條經(jīng)驗 獲得超5個贊
如果您希望一個項目在最后一個項目旋轉(zhuǎn)或移動時不改變其相對于另一個項目的位置,那么第一個項目是第二個項目的子項目就足夠了:
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_())
添加回答
舉報