-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgbutton.cpp
More file actions
68 lines (58 loc) · 1.42 KB
/
imgbutton.cpp
File metadata and controls
68 lines (58 loc) · 1.42 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
#include "imgbutton.h"
ImgButton::ImgButton(QWidget *parent) : QPushButton(parent)
{}
///
/// \brief ImgButton::set_pic 设置图片
/// \param normalImg 普通状态显示的图片
/// \param pressImg 按下状态时显示的图片
///
void ImgButton::set_pic(QString normalImg, QString pressImg)
{
this->normalImg = normalImg;
this->pressImg = pressImg;
if(!this->set_style(normalImg))
return ;
}
///
/// \brief ImgButton::set_style 设置样式
/// \param path 要显示的图片
/// \return 返回是否设置成功
///
bool ImgButton::set_style(QString path)
{
QPixmap pix;
bool flag = pix.load(path);
if(!flag)
return false;
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pix);
this->setIconSize(QSize(pix.width(),pix.height()));
return true;
}
///
/// \brief ImgButton::mousePressEvent 点击显示按下图片
/// \param e 鼠标点击事件
///
void ImgButton::mousePressEvent(QMouseEvent *e)
{
if( this->pressImg!="" )
{
if(!this->set_style(this->pressImg)){
return ;
}
}
QPushButton::mousePressEvent(e);
}
///
/// \brief ImgButton::mouseReleaseEvent 释放显示普通图片
/// \param e 鼠标点击事件
///
void ImgButton::mouseReleaseEvent(QMouseEvent *e)
{
if( this->pressImg!="" )
{
if(!this->set_style(this->normalImg))
return ;
}
QPushButton::mouseReleaseEvent(e);
}