PyQt: 使用setPosAt的QGraphicsItemAnimation

0 投票
2 回答
716 浏览
提问于 2025-04-16 20:08

我遇到了一点问题,因为我无法通过像下面这样重写 setPos 方法来创建一个简单的 setPos 动画:

def setPos(self, pos):                                                                                                                                                                                          
    print('from %s to %s' % (self.scenePos(), pos))                                                                                                                                                             
    timer = QTimeLine(5000)                                                                                                                                                                                     
    timer.setFrameRange(0, 100)                                                                                                                                                                                 
    animation = QGraphicsItemAnimation()                                                                                                                                                                        
    animation.setItem(self)                                                                                                                                                                                     
    animation.setTimeLine(timer)                                                                                                                                                                                
    x_step = (pos - self.scenePos()).x() / 200                                                                                                                                                                  
    y_step = (pos - self.scenePos()).y() / 200                                                                                                                                                                  
    for i in range(200):                                                                                                                                                                                        
        animation.setPosAt(i/200, self.scenePos() + QPointF(i * x_step, i * y_step))                                                                                                                            
    timer.start()    

提前谢谢你,b52

2 个回答

0

为什么不使用 QPropertyAnimation 呢?这个类可以让你为对象的任何属性创建动画。

 QPropertyAnimation * animationPos = new QPropertyAnimation(Object, propertyName);

  animationPos->setDuration(miliseconds);
  animationPos->setStarValue(startValueForProperty);
  animationsPos->setEndValue(endValueForProperty);

你可以试试看,这样可以避免很多冲突。

1

@robe: QPropertyAnimation 这个东西需要声明的类是 QObject,而我认为 QGraphicsItem 不是这个类。

撰写回答