下面是一个示例,在该示例中,graphicsitem出现在动画的末尾,而不是像它应该出现的那样逐渐出现。
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt, QEasingCurve
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QGraphicsScene, QGraphicsView, QGraphicsPixmapItem, QGraphicsItem
class QStone(QGraphicsPixmapItem):
def __init__(self):
QGraphicsPixmapItem.__init__(self)
white = QPixmap("white2.png")
self.setPixmap(white.scaled(60, 60, Qt.KeepAspectRatio))
self.w = self.boundingRect().width()
self.h = self.boundingRect().height()
class QBoard(QGraphicsView):
def __init__(self,scene):
QGraphicsView.__init__(self)
self.scene=scene
self.setScene(scene)
def display_stone(self, x, y):
stone = QStone()
stone.setZValue(10)
stone.setOpacity(0)
stone.setPos(x - stone.w / 2, y - stone.h / 2)
self.scene.addItem(stone)
animation = QtCore.QVariantAnimation(self.scene)
animation.setDuration(3000)
animation.valueChanged.connect(stone.setOpacity)
# animation.setStartValue(0)
# animation.setEndValue(1)
animation.setParent(self.scene)
animation.setEasingCurve(QEasingCurve.BezierSpline)
animation.start()
class MainWindow(QMainWindow):
def __init__(self):
#all the usual stuff
QMainWindow.__init__(self)
centralWidget = QtWidgets.QWidget(self)
self.setCentralWidget(centralWidget)
mainLayout = QtWidgets.QGridLayout()
centralWidget.setLayout(mainLayout)
self.scene = QGraphicsScene()
self.view = QBoard(self.scene)
mainLayout.addWidget(self.view,0,0)
self.scene.setSceneRect(-200.0,-150.0,400.0,300.0)
self.view.display_stone(0,0)
app = QtWidgets.QApplication(sys.argv)
main_win = MainWindow()
main_win.show()
sys.exit(app.exec_())请不要放入任何图像文件,而要放入任何图像文件。
你知道为什么会这样吗?
总而言之,我也可以使用QPropertyAnimation,但对于同样的结果,它需要做更多的工作。
发布于 2019-08-03 05:37:12
QVariantAnimation使用通过在startValue和endValue中传递的值减去的数据类型生成动画,在您的情况下,不放置它意味着使用整数,或者放置相同的0和1会在插值中使用整数值。可以在0和1之间插值的整数值是什么?因为只有0和1,例如对于t = 0.5 * T,不透明度值应该是0.5,考虑到它是否是线性的,但是如何使用整数,则舍入将其设置为0,并且只有当T =T时它才可见。解决方案是将其作为0.0的startValue和1.0的endValue传递。
animation = QtCore.QVariantAnimation(self.scene)
animation.setDuration(3000)
animation.valueChanged.connect(stone.setOpacity)
animation.setStartValue(0.0) # <---
animation.setEndValue(1.0) # <---
animation.setParent(self.scene)
animation.setEasingCurve(QEasingCurve.BezierSpline)
animation.start()https://stackoverflow.com/questions/57333239
复制相似问题