2012-05-18 31 views
0

我有一個奇怪的錯誤,對QGraphicsObject的QPropertyAnimation非工作。這裏是代碼,Pyqt v.4.8.6,Qt 4.6。正如你所看到的,沒有發生'valueChanged'信號。 這是一個錯誤還是我做錯了什麼?提前致謝!QGraphicsObject與QPropertyAnimation在PyQt

import sys 

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

from functools import partial 

class GObject(QGraphicsObject): 

    def boundingRect(self): 
     return QRectF(0, 0, 10, 10) 

    def paint(self, p, *args): 
     p.drawRect(self.boundingRect()) 

    def animatePos(self, start, end): 
     print 'Animating..' 
     anim = QPropertyAnimation(self, 'pos') 
     anim.setDuration(400) 
     anim.setStartValue(start) 
     anim.setEndValue(end) 
     self.connect(anim, SIGNAL('valueChanged(const QVariant&)'), self.go) 
     #self.connect(anim, SIGNAL('stateChanged (QAbstractAnimation::State, QAbstractAnimation::State)'),self.ttt) 
     anim.start() #QAbstractAnimation.DeleteWhenStopped) 

    def go(self, t): 
     print t 

class Scene_Chooser(QWidget): 
    def __init__(self, parent = None): 
     super(Scene_Chooser, self).__init__(parent) 

     vbox = QVBoxLayout(self) 
     vbox.setContentsMargins(0, 0, 0, 0) 
     view = QGraphicsView(self) 
     self.scene = QGraphicsScene(self) 
     obj = GObject() 
     self.scene.addItem(obj) 
     view.setScene(self.scene) 

     btn = QPushButton('animate', self) 
     vbox.addWidget(view) 
     vbox.addWidget(btn) 
     btn.clicked.connect(partial(obj.animatePos, QPointF(0,0), QPointF(10, 5))) 

回答

1

你不保留任何引用您的anim對象,因此它被立即銷燬,纔可以發出任何東西。

也蟒可以更elegantely連接信號:代替

anim.valueChanged.connect(self.go) 
self.anim = anim 

self.connect(anim, SIGNAL('valueChanged(const QVariant&)'), self.go) 

將解決您的問題。

+0

非常感謝你,瑪塔!而已! –