forked from kakaZzzz/dpc_linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustombutton.cpp
97 lines (85 loc) · 2.15 KB
/
custombutton.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "custombutton.h"
#include <QPainter>
#include <QDebug>
tabButton::tabButton(QString image_path, QWidget *parent, QString text)
:QPushButton(parent)
{
this->setCursor(Qt::PointingHandCursor);
m_index = 0;
m_enter = false;
m_check = false;
m_text = text;
QPixmap pix(image_path);
for (int i = 0; i < 4; i++)
{
m_list_pixmap << pix.copy(i * pix.width()/4, 0, pix.width()/4, pix.height());
}
}
tabButton::~tabButton()
{
}
// set tab check
void tabButton::setTabCheck(bool check)
{
m_check = check;
update();
}
void tabButton::enterEvent(QEvent*)
{
m_index = 1;
m_enter = true;
update();
}
void tabButton::leaveEvent(QEvent*)
{
m_index = 0;
m_enter = false;
update();
}
void tabButton::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
if (m_check)
{
painter.drawPixmap((width()-m_list_pixmap.at(3).width())/2,
(height()-m_list_pixmap.at(m_index).height())/2,
m_list_pixmap.at(3).width(),
m_list_pixmap.at(3).height(),
m_list_pixmap.at(3));
}
else
{
painter.drawPixmap((width()-m_list_pixmap.at(m_index).width())/2,
(height()-m_list_pixmap.at(m_index).height())/2,
m_list_pixmap.at(m_index).width(),
m_list_pixmap.at(m_index).height(),
m_list_pixmap.at(m_index));
}
if (!m_text.isEmpty())
{
painter.setPen(QColor(51, 51, 51));
QFont font;
QFontMetrics metric(font);
int text_width = metric.width(m_text);
painter.drawText((width() - text_width)/2, 32, m_text);
//QPushButton::paintEvent(e);
}
}
void tabButton::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
{
m_index = 2;
update();
QPushButton::mousePressEvent(e);
}
}
void tabButton::mouseReleaseEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
{
m_index = 1;
update();
QPushButton::mouseReleaseEvent(e);
}
}