QT实现弹跳按钮-自定义控件
立即下载
资源介绍:
Qt通过重新封装QPushButton类,实现自定义按钮,并且实现点击后的上下跳动特效。
#include "bouncebutton.h"
#include
#include
#include
#include
#include
BounceButton::BounceButton(QString imgPath,QWidget *parent) : QPushButton(parent)
{
setImage(imgPath);
connect(this,&BounceButton::clicked,[=](){this->zoom1();this->zoom2();});
}
void BounceButton::zoom1(){
//创建动画对象
QPropertyAnimation *animation1 = new QPropertyAnimation(this,"geometry");
//设置时间间隔,单位毫秒
animation1->setDuration(200);
//创建其实位置
animation1->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
animation1->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
//设置缓和曲线,QEasingCurve::OutBounce 为弹跳效果
animation1->setEasingCurve(QEasingCurve::OutBounce);
//开始执行动画
animation1->start();
}
void BounceButton::zoom2(){
//创建动画对象
QPropertyAnimation *animation1 = new QPropertyAnimation(this,"geometry");
//设置时间间隔,单位毫秒
animation1->setDuration(200);
//创建其实位置
animation1->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
animation1->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
//设置缓和曲线,QEasingCurve::OutBounce 为弹跳效果
animation1->setEasingCurve(QEasingCurve::OutBounce);
//开始执行动画
animation1->start();
}
void BounceButton::setImage(QString imgPath)
{
m_imgPath = imgPath;
QPixmap pixmap;
bool ret = pixmap.load(m_imgPath);
if(!ret)
return;
//设置图片固定尺寸
this->setFixedSize(pixmap.width(),pixmap.height());
//设置不规则图片的样式表
this->setStyleSheet("QPushButton{border:0px}");
//设置图标
this->setIcon(pixmap);
//设置图标大小
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
}