Qt实现非规则图形的按钮及按钮抖动

首先自定义封装一个类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();


}

之后只需写个信号的槽调用即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值