I am try to create a widget to animate a line, the when the windows size is changed, the animation always play the size.
I know the PainterPath is always same, but i have no any simple idea to do?
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()
Sorry but your question is a bit confused. If I understand it correctly, you want to update the path everytime the window is resized.
The problem is that you create a new
self.path
object whenever the window is painted, which will also happen as soon as the window is painted the first time, so the QPainterPath object you created for the property is not actually updated.You should update the path only when the window is resized, which within
resizeEvent()
.Then keep in mind that you can update an existing path only from Qt 5.13 (which was released last June), otherwise you'll have to create a new one and ensure to update the
path
property of your animation too.Another possibility is to avoid a QPropertyAnimation subclass at all, use a private property that will go from
0.0
to1.0
, create a QPropertyAnimation for that, then connect itsvalueChanged
signal to a function that computes the position using pointAtPercent and then move the button.