我正在尝试创建一个小部件来设置线条的动画,当窗口大小改变时,动画总是按大小播放
我知道画家的道路总是一样的,但我没有任何简单的想法去做
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class PathAnimation(QPropertyAnimation):
def __init__(self, path=QPainterPath(), object=None, property=None):
super().__init__(object, property)
self.path = path
def updateCurrentTime(self, currentTime):
easedProgress = self.easingCurve().valueForProgress(currentTime/self.duration())
pt = self.path.pointAtPercent(easedProgress)
self.updateCurrentValue(pt)
self.valueChanged.emit(pt)
class DemoB(QWidget):
def __init__(self):
super().__init__()
self.resize(400, 400)
self.button = QPushButton('test', self)
def mouseDoubleClickEvent(self, e):
self.ani = PathAnimation(self.path, self.button, b'pos')
self.ani.setDuration(2000)
self.ani.start()
def paintEvent(self, e):
painter = QPainter(self)
painter.begin(self)
painter.setWindow(0, 0, 400, 400)
self.path = QPainterPath()
self.path.cubicTo(QPointF(0, 400), QPointF(200, 0), QPointF(400, 400))
painter.drawPath( self.path )
painter.end()
app = QApplication([])
demo = DemoB()
demo.show()
app.exec()
对不起,你的问题有点困惑。如果我理解正确,您希望在每次调整窗口大小时更新路径。
问题在于,每当绘制窗口时,您都会创建一个新的
self.path
对象,这也会在第一次绘制窗口时发生,因此您为属性创建的QPainterPath对象实际上不会更新您应该仅在调整窗口大小时更新路径,这在
resizeEvent()
内然后请记住,您只能从Qt5.13(去年6月发布)更新现有路径,否则您必须创建一个新路径,并确保更新动画的
path
属性另一种可能是完全避免QPropertyImation子类,使用从
0.0
到1.0
的私有属性,为其创建QPropertyImation,然后将其valueChanged
信号连接到使用pointAtPercent计算位置的函数,然后移动按钮相关问题 更多 >
编程相关推荐