-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmypushbutton.cpp
More file actions
82 lines (78 loc) · 2.78 KB
/
Copy pathmypushbutton.cpp
File metadata and controls
82 lines (78 loc) · 2.78 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
71
72
73
74
75
76
77
78
79
80
81
82
#include "mypushbutton.h"
#include <QDebug>
#include <QPropertyAnimation>
//MyPushButton::MyPushButton(QWidget *parent) : QWidget(parent)
//{
//
//}
MyPushButton::MyPushButton(QString normalImagePath, QString pressImagePath)
: m_normalImagePath{normalImagePath}, m_pressImagePath{pressImagePath}
{
QPixmap pix;
bool ret{pix.load(this->getNormalImagePath())};
if (!ret)
{
qDebug() << QString{"wrong path : %1"}.arg(this->getNormalImagePath());
return;
}
else
{
this->setFixedSize(QSize{pix.width(), pix.height()});
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(QIcon{pix});
this->setIconSize(QSize{pix.width(), pix.height()});
}
}
void MyPushButton::moveUp()
{
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(QAbstractAnimation::DeleteWhenStopped);
}
void MyPushButton::moveDown()
{
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(QAbstractAnimation::DeleteWhenStopped);
}
void MyPushButton::mousePressEvent(QMouseEvent *e)
{
if (getPressImagePath() != "") //选中路径不为空,显示选中图片
{
QPixmap pixmap;
bool ret = pixmap.load(getPressImagePath());
if (!ret)
{
qDebug() << getPressImagePath() << "加载图片失败!";
}
this->setFixedSize(pixmap.width(), pixmap.height());
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(), pixmap.height()));
}
//交给父类执行按下事件
return QPushButton::mousePressEvent(e);
}
void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
if (getNormalImagePath() != "") //选中路径不为空,显示选中图片
{
QPixmap pixmap;
bool ret = pixmap.load(getNormalImagePath());
if (!ret)
{
qDebug() << getNormalImagePath() << "加载图片失败!";
}
this->setFixedSize(pixmap.width(), pixmap.height());
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(), pixmap.height()));
}
return QPushButton::mouseReleaseEvent(e);
}