-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmypushbutton.cpp
66 lines (56 loc) · 1.71 KB
/
mypushbutton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "mypushbutton.h"
#include <QPainter>
#include <QPropertyAnimation>
MyPushButton::MyPushButton(QString normalImg,QString pressImg,QWidget *parent)
: QPushButton(parent)
,mNormalImg(normalImg)
,mPressImg(pressImg)
{
mStat= Normal;
}
void MyPushButton::moveUp()
{
QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry",this);
animation->setStartValue(this->geometry());
animation->setEndValue(QRect(this->x(),this->y()-10,this->width(),this->height()));
animation->setDuration(100);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
void MyPushButton::moveDown()
{
//位置大小属性发生变化
//给定开始位置
//给定结束位置
//给定速度,给定动画时长
QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry",this);
animation->setStartValue(this->geometry());
animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
animation->setDuration(100);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
void MyPushButton::paintEvent(QPaintEvent *ev)
{
QPainter painter(this);
QPixmap pix;
if(mStat==Normal){
pix.load(mNormalImg);
}
if(mStat==Pressed){
pix.load(mPressImg);
}
painter.drawPixmap(0,0,this->width(),this->height(),pix);
//绘制文字
painter.drawText(0,0,this->width(),this->height(),Qt::AlignHCenter|Qt::AlignVCenter,this->text());
}
void MyPushButton::mousePressEvent(QMouseEvent *e)
{
this->mStat= Pressed;
update();
QPushButton::mousePressEvent(e);
}
void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
this->mStat= Normal;
update();
QPushButton::mouseReleaseEvent(e);
}