-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmypushbutton.cpp
More file actions
70 lines (64 loc) · 2.19 KB
/
mypushbutton.cpp
File metadata and controls
70 lines (64 loc) · 2.19 KB
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
66
67
68
69
70
#include "mypushbutton.h"
MyPushButton::MyPushButton(QString normalImage,QString pressedImage,QWidget *parent)
: QPushButton{parent},
mNormalImage(normalImage),
mPressedImage(pressedImage)
{
buttonStat=Normal;//按钮默认状态为Normal
}
void MyPushButton::paintEvent(QPaintEvent *ev)
{
QPainter painter(this);
//加载图片前先判断按钮状态
//根据按钮状态加载对应的图片
QPixmap pix(mNormalImage);
if(buttonStat==Normal)
{
pix.load(mNormalImage);
}
if(buttonStat==Pressed)
{
pix.load(mPressedImage);
}
painter.drawPixmap(0,0,this->width(),this->height(),pix);
//绘制按钮文字
painter.drawText(0,0,this->width(),this->height(),Qt::AlignCenter,this->text());
}
void MyPushButton::mousePressEvent(QMouseEvent *e)
{
this->buttonStat=Pressed;//更新按钮status
update();//更新绘图事件
QPushButton::mousePressEvent(e);//调用父类函数 确保信号和槽的正确发射
}
void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
this->buttonStat=Normal;
update();
QPushButton::mouseReleaseEvent(e);
}
void MyPushButton::moveDown()
{
//1.位置大小属性发生变化
QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry",this);
//2.给定开始的位置大小属性
animation->setStartValue(this->geometry());
//3.给定结束的位置大小属性
animation->setEndValue(QRect(this->x(),this->y()+10,
this->width(),this->height()));
//4.给定速度 设置动画时长 100ms
animation->setDuration(100);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
void MyPushButton::moveUp()
{
//1.位置大小属性发生变化
QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry",this);
//2.给定开始的位置大小属性
animation->setStartValue(this->geometry());
//3.给定结束的位置大小属性
animation->setEndValue(QRect(this->x(),this->y(),
this->width(),this->height()));
//4.给定速度 设置动画时长 100ms
animation->setDuration(100);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}