Repository navigation
Expand file tree
/
Copy pathQelButtonGroup.cpp
More file actions
139 lines (121 loc) · 3.55 KB
/
Copy pathQelButtonGroup.cpp
File metadata and controls
139 lines (121 loc) · 3.55 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "QelButtonGroup.h"
#include <QPainter>
#include <QPainterPath>
namespace qel {
QelButtonGroup::QelButtonGroup(QWidget *parent)
: QWidget(parent)
, m_size(QelButton::Medium)
, m_type(QelButton::Default)
{
initUI();
updateStyle();
}
void QelButtonGroup::initUI()
{
m_layout = new QHBoxLayout(this);
m_layout->setSpacing(0);
m_layout->setContentsMargins(0, 0, 0, 0);
setLayout(m_layout);
}
void QelButtonGroup::addButton(QelButton* button)
{
if (!button) return;
m_buttons.append(button);
m_layout->addWidget(button);
// 更新按钮样式
button->setSize(m_size);
button->setType(m_type);
// 特殊处理第一个和最后一个按钮的圆角
updateStyle();
}
void QelButtonGroup::setSize(QelButton::ButtonSize size)
{
m_size = size;
for (QelButton* btn : m_buttons) {
btn->setSize(size);
}
}
void QelButtonGroup::setType(QelButton::ButtonType type)
{
m_type = type;
for (QelButton* btn : m_buttons) {
btn->setType(type);
}
}
void QelButtonGroup::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
}
void QelButtonGroup::updateStyle()
{
if (m_buttons.isEmpty()) return;
QString baseStyle = R"(
QelButtonGroup {
border: none;
background: transparent;
}
)";
// 设置按钮组的样式
for (int i = 0; i < m_buttons.size(); ++i) {
QelButton* btn = m_buttons[i];
QString btnStyle;
if (i == 0) {
// 第一个按钮:左边有圆角,右边没有
btnStyle = QString(R"(
QPushButton {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
border-right: none;
margin: 0;
}
QPushButton:hover {
position: relative;
z-index: 1;
border-right: 1px solid %1;
}
)").arg(btn->property("type") == QelButton::Primary ? "#66b1ff" : "#c0c4cc");
} else if (i == m_buttons.size() - 1) {
// 最后一个按钮:右边有圆角,左边没有
btnStyle = QString(R"(
QPushButton {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
margin: 0;
margin-left: -1px;
}
QPushButton:hover {
position: relative;
z-index: 1;
}
)");
} else {
// 中间的按钮:两边都没有圆角
btnStyle = QString(R"(
QPushButton {
border-radius: 0;
border-right: none;
margin: 0;
margin-left: -1px;
}
QPushButton:hover {
position: relative;
z-index: 1;
border-right: 1px solid %1;
}
)").arg(btn->property("type") == QelButton::Primary ? "#66b1ff" : "#c0c4cc");
}
// 合并现有样式和新样式
QString currentStyle = btn->styleSheet();
if (!currentStyle.isEmpty()) {
currentStyle += "\n";
}
btn->setStyleSheet(currentStyle + btnStyle);
}
setStyleSheet(baseStyle);
// 更新布局
m_layout->setSpacing(0);
m_layout->setContentsMargins(0, 0, 0, 0);
}
} // namespace qel