Simple PySide/PyQt QPropertyAnimation

  • Hi,

    I'm struggling to get an animation working. It seems that animation outside of my class work fine, however as soon as I run them from withing the class, they refuse to start. Can anyone tell me what's wrong with my code:

    @import sys

    from PySide import QtCore, QtGui

    class Pixmap(QtCore.QObject):
    def init(self, pix):
    super(Pixmap, self).init()

        if type(pix) == type(""):
            pix = QtGui.QPixmap(pix)
        self.pixmap_item = QtGui.QGraphicsPixmapItem(pix)
    def set_pos(self, pos):
    def get_pos(self):
        return self.pixmap_item.pos()
    def get_opacity(self):
        return self.pixmap_item.opacity()
    def set_opacity(self, opacity):
    pos = QtCore.Property(QtCore.QPointF, get_pos, set_pos)
    opacity = QtCore.Property(float, get_opacity, set_opacity)

    Sets up the subclassed QGraphicsView
    class ImageWidget(QtGui.QGraphicsView):
    LastPanPoint = None
    CurrentCenterPoint = None

    def __init__(self, parent=None):
        super(ImageWidget, self).__init__(parent)
        self.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform)
        # set-up the scene
        self.scene = QtGui.QGraphicsScene()
    def loadImage(self, f):
        # Load image
         pixmap = Pixmap(f)
        item = pixmap.pixmap_item
        anim = QtCore.QPropertyAnimation(pixmap, "opacity")
        QtCore.QTimer.singleShot(1000, anim, QtCore.SLOT("start()"))
        QtCore.QObject.connect(anim, QtCore.SIGNAL("finished()"), anim, QtCore.SLOT("deleteLater()"))

    if name == "main":
    a = QtGui.QApplication(sys.argv)
    view = ImageWidget()
    view.setGeometry(100,100, 900, 600)

  • Hi

    This answer may be a little help but I hope it helps someone nevertheless.

    In ImageWidget.loadImage, you create anim as a local variable. That means it's going to be garbage collected at the end of th call : as soon as the call is over, the animation is deleted, and won't have any effect.

    There is two ways to solve the problem :

    • Define anim as an object attribute (@self.anim = …@)
    • Set anim as a child of the current object, so that it keeps a reference to anim and prevent the garbage collection (@anim = QtCore.QPropertyAnimation(pixmap, "opacity", self)@)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.