-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoverbutton.cpp
68 lines (60 loc) · 2 KB
/
hoverbutton.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
66
67
68
#include "hoverbutton.h"
#include <QDebug>
HoverButton::HoverButton(QWidget *parent) : QPushButton(parent)
{
setStyleSheet("QPushButton{border:0px;}");
setSound();
}
void HoverButton::setImage(QString pathNormal, QString pathHover, int width, int height){
if(iconNormal)
delete iconNormal;
if(iconHover)
delete iconHover;
w = width; h =height;
iconNormal = new QIcon(pathNormal);
iconHover = (pathHover != QString("") ? new QIcon(pathHover) : new QIcon(pathNormal));
this->setIcon(*iconNormal);
this->setIconSize(QSize(width, height));
}
void HoverButton::setSound(QString pathHover, QString pathLeave, QString pathPress, QString pathRelease){
soundHover = (pathHover != QString("") ? new QSound(pathHover, this) : nullptr);
soundPress = (pathPress != QString("") ? new QSound(pathPress, this) : nullptr);
soundRelease = (pathRelease != QString("") ? new QSound(pathRelease, this) : nullptr);
soundLeave = (pathLeave != QString("") ? new QSound(pathLeave, this) : nullptr);
}
void HoverButton::setLabel(QString text){
label = new QLabel(text, this);
label->setAttribute(Qt::WA_TransparentForMouseEvents);
label->setGeometry(0,0, w, h);
label->setAlignment(Qt::AlignCenter);
label->setFont(QFont("Microsoft YaHei", 8, 600));
label->setStyleSheet("QLabel{color:white;}");
label->setVisible(true);
}
bool HoverButton::event(QEvent *e) {
if(!isEnabled())
return QPushButton::event(e);
switch(e->type()){
case QEvent::Enter:
setIcon(*iconHover);
if(soundHover)
soundHover->play();
break;
case QEvent::Leave:
setIcon(*iconNormal);
if(soundLeave)
soundLeave->play();
break;
case QEvent::MouseButtonPress:
if(soundPress)
soundPress->play();
break;
case QEvent::MouseButtonRelease:
if(soundRelease)
soundRelease->play();
break;
default:
break;
}
return QPushButton::event(e);
}