QPropertyAnimation获取当前循环多少次
在Qt中,QPropertyAnimation类没有提供直接获取当前循环多少次的方法。不过,你可以通过继承QPropertyAnimation类并重写其相关方法来实现这个功能。
首先,你需要重写QPropertyAnimation的updateCurrentTime()方法。这个方法会在动画每次更新时被调用。你可以在这个方法中记录循环次数。
class MyPropertyAnimation : public QPropertyAnimation
{
public:
MyPropertyAnimation(QObject *target, const QByteArray &propertyName, QObject *parent = nullptr)
: QPropertyAnimation(target, propertyName, parent), m_loopCount(0) {}
protected:
void updateCurrentTime(int currentTime) override
{
// 每次更新动画时,检查是否完成了一次循环
if (currentTime >= duration()) {
m_loopCount++;
}
QPropertyAnimation::updateCurrentTime(currentTime);
}
private:
int m_loopCount;
};
然后,你可以使用这个自定义的QPropertyAnimation类来获取当前循环次数。
MyPropertyAnimation animation(targetObject, "propertyName");
animation.setDuration(duration);
animation.setLoopCount(loopCount);
animation.start();
// 获取当前循环次数
int currentLoopCount = animation.loopCount();
请注意,这种方法只能获取当前循环次数,而无法获取已经完成的总循环次数。如果你需要获取已经完成的总循环次数,你需要在自定义的QPropertyAnimation类中添加一个计数器,并在动画完成时,更新该计数器的值。
原文地址: http://www.cveoy.top/t/topic/jeO0 著作权归作者所有。请勿转载和采集!