效果图
基于QPropertyAnimation实现
代码部分
//设置滑动条动画
m_scrollAnimation=new QPropertyAnimation;
m_scrollAnimation->setTargetObject(this);
m_scrollAnimation->setPropertyName("value");
m_scrollAnimation->setEasingCurve(QEasingCurve::OutQuint);
m_scrollAnimation->setDuration(1000);
m_targetValue =value();
void SmoothScrollBar::setValue(int value)
{
m_scrollAnimation->stop();
m_scrollAnimation->setStartValue(this->value());
m_scrollAnimation->setEndValue(value);
m_scrollAnimation->start();
}
void SmoothScrollBar::scroll(int value)
{
m_targetValue -= value;
setValue(m_targetValue);
}
void SmoothScrollBar::mousePressEvent(QMouseEvent *e)
{
m_scrollAnimation->stop();
QScrollBar::mousePressEvent(e);
m_targetValue = value();
}
void SmoothScrollBar::mouseReleaseEvent(QMouseEvent *e)
{
m_scrollAnimation->stop();
QScrollBar::mouseReleaseEvent(e);
m_targetValue = value();
}
void SmoothScrollBar::mouseMoveEvent(QMouseEvent *e)
{
m_scrollAnimation->stop();
QScrollBar::mouseMoveEvent(e);
m_targetValue = value();
}