Pregunta

Tengo un error extraño con QPropertyAnimation que no funciona para un QGraphicsObject.Aquí está el código, Pyqt v.4.8.6, Qt 4.6.Como puede ver, no se emite ninguna señal 'valueChanged'.¿Es un error o qué estoy haciendo mal?¡Gracias de antemano!

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)))
¿Fue útil?

Solución

no guardas ninguna referencia a tu anim objeto, por lo que se destruye inmediatamente antes de que pueda emitir algo.

También en Python puedes conectar las señales de forma mucho más elegante:

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

en lugar de:

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

solucionará tu problema.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top