Skip to content

Commit 061b3fd

Browse files
author
wangqiang
committed
refactor(qel-button): 增强样式和构造函数,添加测试界面
重构QelButton类,简化构造函数,添加设置方法,并引入新的NativeButtonType枚举。增强样式自定义,现在支持通过设置方法调整按钮类型、大小、样式。新增QelButtonTester类,用于展示不同配置下的按钮样式,便于界面测试。 feat(qel-button): 添加图标支持和测试界面集成 新增图标支持,修改构造函数以适应图标和文字的组合。主函数调整,现在展示测试界面,方便开发者直观查看和测试不同类型的按钮。 refactor(qel-icon): 优先从系统加载FontAwesome字体 优化FontAwesome字体加载逻辑,优先从系统加载,若系统未安装,则从资源文件加载。这提高了字体加载的效率和可靠性,减少了从资源文件加载字体的依赖性。
1 parent 18e8649 commit 061b3fd

File tree

11 files changed

+10871
-222
lines changed

11 files changed

+10871
-222
lines changed

QelButton/QelButton.cpp

Lines changed: 255 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,110 +3,290 @@
33
namespace qel
44
{
55

6-
QelButton::QelButton(QWidget *parent,
7-
QString text,
8-
ButtonSize btnSize,
9-
ButtonType btnType,
6+
//QelButton::QelButton(QWidget *parent,
7+
// QString text,
8+
// ButtonSize btnSize,
9+
// ButtonType btnType,
10+
// bool isPlain,
11+
// bool isRound,
12+
// bool isCircle,
13+
// bool isDisabled,
14+
// bool isLoading)
15+
// : QPushButton(parent),
16+
// btn_text_(text),
17+
// btn_size_(btnSize),
18+
// btn_type_(btnType),
19+
// is_plain_(isPlain),
20+
// is_round_(isRound),
21+
// is_circle_(isCircle),
22+
// is_loading_(isLoading),
23+
// is_disabled_(isDisabled)
24+
//{
25+
// this->updateStyle();
26+
//}
27+
28+
//QelButton::~QelButton()
29+
//{
30+
31+
//}
32+
33+
//ButtonSize QelButton::getSize()
34+
//{
35+
36+
// return ButtonSize::Medium;
37+
//}
38+
39+
//void QelButton::updateStyle()
40+
//{
41+
// switch (btn_size_) {
42+
// case ButtonSize::Medium:
43+
// this->setFixedSize(98, 36);
44+
// break;
45+
// case ButtonSize::Small:
46+
// this->setFixedSize(80, 32);
47+
// break;
48+
// case ButtonSize::Mini:
49+
// this->setFixedSize(80, 28);
50+
// break;
51+
// default:
52+
// break;
53+
// }
54+
55+
// QString type_qss = "";
56+
// QString hover_qss = "";
57+
// QString pressed_qss = "";
58+
// QString type_hover_qss = "";
59+
60+
// switch (btn_type_)
61+
// {
62+
// case qel::ButtonType::Default:
63+
// type_qss = QString("color:#606266;background-color:%1; border: 1px solid #dcdfe6;").arg(MACRO_STR(DEFAULT_COLOR));
64+
// type_hover_qss = QString("color:#606266;background-color:%1;").arg(MACRO_STR(DEFAULT_HOVER_COLOR));
65+
// break;
66+
// break;
67+
// case qel::ButtonType::Warning:
68+
// break;
69+
// case qel::ButtonType::Danger:
70+
// break;
71+
// case qel::ButtonType::Info:
72+
// break;
73+
// case qel::ButtonType::Text:
74+
// break;
75+
// case ButtonType::Primary:
76+
// type_qss = QString("color:white;background-color:#%1;").arg(MACRO_STR(PRIMARY_COLOR));
77+
// type_hover_qss = QString("color:white;background-color:#%1;").arg(MACRO_STR(PRIMARY_HOVER_COLOR));
78+
// break;
79+
// case ButtonType::Success:
80+
// type_qss = QString("color:white;background-color:#%1;").arg(MACRO_STR(SUCCESS_COLOR));
81+
// type_hover_qss = QString("color:white;background-color:#%1;").arg(MACRO_STR(SUCCESS_HOVER_COLOR));
82+
// break;
83+
// default:
84+
// break;
85+
// }
86+
87+
88+
// QString round_qss;
89+
// if (is_round_)
90+
// round_qss = QString("border-radius:%1px;").arg(QString::number(this->height()/2));
91+
// else
92+
// round_qss = QString("border-radius:4px;");
93+
94+
// if (is_circle_)
95+
// {
96+
// round_qss = QString("border-radius:%1px;").arg(QString::number(this->height()/2));
97+
// this->setFixedWidth(this->height());
98+
// }
99+
100+
// this->setStyleSheet(QString("QPushButton{")
101+
// + type_qss
102+
// + round_qss
103+
// + "}"
104+
// + "QPushButton:hover{"
105+
// + type_hover_qss
106+
// + round_qss
107+
// + "}");
108+
109+
// this->setText(btn_text_);
110+
//}
111+
112+
QelButton::QelButton(ButtonType type,
113+
ButtonSize size,
10114
bool isPlain,
11115
bool isRound,
12116
bool isCircle,
13-
bool isDisabled,
14-
bool isLoading)
15-
: QPushButton(parent),
16-
btn_text_(text),
17-
btn_size_(btnSize),
18-
btn_type_(btnType),
19-
is_plain_(isPlain),
20-
is_round_(isRound),
21-
is_circle_(isCircle),
22-
is_loading_(isLoading),
23-
is_disabled_(isDisabled)
117+
bool isLoading,
118+
NativeButtonType nativeType,
119+
const QIcon &icon,
120+
const QString &text,
121+
QWidget *parent)
122+
: QPushButton(text, parent),
123+
type_(type),
124+
size_(size),
125+
isPlain_(isPlain),
126+
isRound_(isRound),
127+
isCircle_(isCircle),
128+
isLoading_(isLoading),
129+
nativeType_(nativeType)
24130
{
25-
this->updateStyle();
131+
setIcon(icon);
132+
updateButtonStyle();
26133
}
27134

28-
QelButton::~QelButton()
29-
{
135+
void QelButton::setType(ButtonType type) {
136+
type_ = type;
137+
updateButtonStyle();
138+
}
30139

140+
void QelButton::setSize(ButtonSize size) {
141+
size_ = size;
142+
updateButtonStyle();
31143
}
32144

33-
ButtonSize QelButton::getSize()
34-
{
145+
void QelButton::setPlain(bool isPlain) {
146+
isPlain_ = isPlain;
147+
updateButtonStyle();
148+
}
35149

36-
return ButtonSize::Medium;
150+
void QelButton::setRound(bool isRound) {
151+
isRound_ = isRound;
152+
updateButtonStyle();
37153
}
38154

39-
void QelButton::updateStyle()
40-
{
41-
switch (btn_size_) {
42-
case ButtonSize::Medium:
43-
this->setFixedSize(98, 36);
44-
break;
45-
case ButtonSize::Small:
46-
this->setFixedSize(80, 32);
47-
break;
48-
case ButtonSize::Mini:
49-
this->setFixedSize(80, 28);
50-
break;
51-
default:
52-
break;
155+
void QelButton::setCircle(bool isCircle) {
156+
isCircle_ = isCircle;
157+
updateButtonStyle();
158+
}
159+
160+
void QelButton::setLoading(bool isLoading) {
161+
isLoading_ = isLoading;
162+
// 加载状态的图标或样式变更
163+
updateButtonStyle();
164+
}
165+
166+
void QelButton::setDisabled(bool isDisabled) {
167+
setEnabled(!isDisabled);
168+
}
169+
170+
void QelButton::setIcon(const QIcon &icon) {
171+
QPushButton::setIcon(icon);
172+
}
173+
174+
void QelButton::setAutofocus(bool autofocus) {
175+
setFocusPolicy(autofocus ? Qt::StrongFocus : Qt::NoFocus);
176+
}
177+
178+
void QelButton::setNativeType(NativeButtonType nativeType) {
179+
nativeType_ = nativeType;
180+
// 设置为表单按钮类型(submit, reset, button)
181+
if (nativeType == Submit) {
182+
setProperty("type", "submit");
183+
} else if (nativeType == Reset) {
184+
setProperty("type", "reset");
185+
} else {
186+
setProperty("type", "button");
53187
}
188+
}
54189

55-
QString type_qss = "";
56-
QString hover_qss = "";
57-
QString pressed_qss = "";
58-
QString type_hover_qss = "";
190+
void QelButton::updateButtonStyle() {
191+
QString style;
59192

60-
switch (btn_type_)
61-
{
62-
case qel::ButtonType::Default:
63-
type_qss = QString("color:#606266;background-color:%1; border: 1px solid #dcdfe6;").arg(MACRO_STR(DEFAULT_COLOR));
64-
type_hover_qss = QString("color:#606266;background-color:%1;").arg(MACRO_STR(DEFAULT_HOVER_COLOR));
65-
break;
66-
break;
67-
case qel::ButtonType::Warning:
68-
break;
69-
case qel::ButtonType::Danger:
193+
// 根据类型设置样式
194+
QString backgroundColor;
195+
QString hoverBackgroundColor;
196+
QString textColor = "white";
197+
QString hoverTextColor = textColor;
198+
QString borderColor;
199+
QString hoverBorderColor;
200+
201+
switch (type_) {
202+
case Primary:
203+
backgroundColor = "#409EFF";
204+
hoverBackgroundColor = "#66B1FF";
205+
borderColor = "#409EFF";
206+
hoverBorderColor = "#66B1FF";
70207
break;
71-
case qel::ButtonType::Info:
208+
case Success:
209+
backgroundColor = "#67C23A";
210+
hoverBackgroundColor = "#85CE61";
211+
borderColor = "#67C23A";
212+
hoverBorderColor = "#85CE61";
72213
break;
73-
case qel::ButtonType::Text:
214+
case Warning:
215+
backgroundColor = "#E6A23C";
216+
hoverBackgroundColor = "#EBB563";
217+
borderColor = "#E6A23C";
218+
hoverBorderColor = "#EBB563";
74219
break;
75-
case ButtonType::Primary:
76-
type_qss = QString("color:white;background-color:%1;").arg(MACRO_STR(PRIMARY_COLOR));
77-
type_hover_qss = QString("color:white;background-color:%1;").arg(MACRO_STR(PRIMARY_HOVER_COLOR));
220+
case Danger:
221+
backgroundColor = "#F56C6C";
222+
hoverBackgroundColor = "#F78989";
223+
borderColor = "#F56C6C";
224+
hoverBorderColor = "#F78989";
78225
break;
79-
case ButtonType::Success:
80-
type_qss = QString("color:white;background-color:%1;").arg(MACRO_STR(SUCCESS_COLOR));
81-
type_hover_qss = QString("color:white;background-color:%1;").arg(MACRO_STR(SUCCESS_HOVER_COLOR));
226+
case Info:
227+
backgroundColor = "#909399";
228+
hoverBackgroundColor = "#A6A9AD";
229+
borderColor = "#909399";
230+
hoverBorderColor = "#A6A9AD";
82231
break;
83232
default:
233+
backgroundColor = "#ffffff";
234+
hoverBackgroundColor = "#ECF5FF";
235+
textColor = "black";
236+
hoverTextColor = "#000000";
237+
borderColor = "#dcdfe6";
238+
hoverBorderColor = "#ECF5FF";
84239
break;
85240
}
86241

242+
if (isPlain_) {
243+
textColor = backgroundColor;
244+
hoverTextColor = hoverBackgroundColor;
245+
backgroundColor = "transparent";
246+
hoverBackgroundColor = "transparent";
247+
}
248+
249+
// 设置按钮默认样式
250+
style += QString("QPushButton { background-color: %1; color: %2; border: 1px solid %3;")
251+
.arg(backgroundColor)
252+
.arg(textColor)
253+
.arg(borderColor);
87254

88-
QString round_qss;
89-
if (is_round_)
90-
round_qss = QString("border-radius:%1px;").arg(QString::number(this->height()/2));
91-
else
92-
round_qss = QString("border-radius:4px;");
255+
QString hoverStyle = QString("QPushButton:hover { background-color: %1; color: %2; border: 1px solid %3; }")
256+
.arg(hoverBackgroundColor)
257+
.arg(hoverTextColor)
258+
.arg(hoverBorderColor);
93259

94-
if (is_circle_)
95-
{
96-
round_qss = QString("border-radius:%1px;").arg(QString::number(this->height()/2));
97-
this->setFixedWidth(this->height());
260+
// 设置尺寸
261+
switch (size_) {
262+
case Large: style += " font-size: 16px; padding: 10px 20px;"; break;
263+
case Medium: style += " font-size: 14px; padding: 8px 16px;"; break;
264+
case Small: style += " font-size: 12px; padding: 6px 12px;"; break;
265+
case Mini: style += " font-size: 10px; padding: 4px 8px;"; break;
98266
}
99267

100-
this->setStyleSheet(QString("QPushButton{")
101-
+ type_qss
102-
+ round_qss
103-
+ "}"
104-
+ "QPushButton:hover{"
105-
+ type_hover_qss
106-
+ round_qss
107-
+ "}");
268+
QString roundQSS;
269+
270+
// 圆角按钮
271+
if (isRound_) {
272+
roundQSS = QString(" border-radius: %1px;").arg(this->height()/2);
273+
} else {
274+
roundQSS = " border-radius: 4px;";
275+
}
108276

109-
this->setText(btn_text_);
277+
// 圆形按钮
278+
if (isCircle_) {
279+
roundQSS = QString(" border-radius: %1px;").arg(this->height()/2);
280+
setFixedWidth(this->height());
281+
}
282+
283+
style += roundQSS + " }"; // 闭合 QPushButton 样式
284+
285+
style += hoverStyle; // 添加 hover 样式
286+
287+
// 设置样式表
288+
this->setStyleSheet(style);
110289
}
111290

291+
112292
} // namespace qel

0 commit comments

Comments
 (0)