首先自定义封装一个类myPushButton,其构造函数如下
第一个参数表示按钮的图片
第二个参数表示按下按钮的图片
myPushButton::myPushButton(QString normalImg,QString pressImg)
{
//两个自己封装的参数,分别表示图片的路径
this->normalImgPath=normalImg;
this->pressImgPath=pressImg;
QPixmap pix;
bool ret = pix.load(normalImgPath);
if(!ret)
{
qDebug()<<"there is a worry when load the picture";
return;
}
//设置按钮大小
this->setFixedSize(pix.width(),pix.height());
//设置不规则样式
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pix);
this->setIconSize(QSize(pix.width(),pix.height()));
}
再自定义函数使得点击按钮,按钮抖动
void myPushButton::zoomdown()
{
//创建动态对象
QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");
//设置动画时间间隔
animation->setDuration(200);
//起始位置
animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
//结束位置
animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
//设置弹跳曲线
animation->setEasingCurve(QEasingCurve::OutBounce);
//执行动画
animation->start();
}
void myPushButton::zoomup()
{
QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");
animation->setDuration(200);
//起始位置
animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
//结束位置
animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
animation->setEasingCurve(QEasingCurve::OutBounce);
animation->start();
}
之后只需写个信号的槽调用即可