【问题标题】:QGraphicsObject with QPropertyAnimation in PyQtPyQt 中带有 QPropertyAnimation 的 QGraphicsObject
【发布时间】:2012-05-18 23:19:19
【问题描述】:

我有一个奇怪的错误,QGraphicsObject 的 QPropertyAnimation 无法正常工作。这是代码,Pyqt v.4.8.6,Qt 4.6。如您所见,没有发出“valueChanged”信号。 这是一个错误还是我做错了什么?提前致谢!

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)))

【问题讨论】:

    标签: pyqt qgraphicsitem


    【解决方案1】:

    您没有保留对您的 anim 对象的任何引用,因此它在发出任何内容之前立即被销毁。

    在 python 中你也可以更优雅地连接信号:

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

    代替:

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

    会解决你的问题。

    【讨论】:

      猜你喜欢
      • 2019-03-22
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2017-04-21
      • 2015-11-01
      • 2022-01-06
      • 2013-08-31
      相关资源
      最近更新 更多