@@ -8,116 +8,128 @@ MainWindow::MainWindow(QWidget *parent)
88{
99 // 创建电池控件
1010 auto *battery = new BatteryWidget (this );
11- battery->setValue (75 ); // 设置初始电量
1211
13- // 创建滑动条控制电量
14- auto *slider = new QSlider (Qt::Vertical, this );
15- slider->setRange (0 , 100 );
16- slider->setValue (75 );
12+ // 创建数值控制
13+ auto *valueLabel = new QLabel (tr (" Current value:" ), this );
1714
18- // 创建充电控制复选框
19- auto *chargingCheckbox = new QCheckBox (tr (" Charging state" ), this );
20- chargingCheckbox->setChecked (false );
15+ auto *valueSlider = new QSlider (Qt::Horizontal, this );
16+ valueSlider->setRange (0 , 100 );
17+ valueSlider->setValue (75 );
18+
19+ auto *valueSpinBox = new QSpinBox (this );
20+ valueSpinBox->setRange (0 , 100 );
21+ valueSpinBox->setValue (75 );
22+ valueSpinBox->setSuffix (" %" );
2123
22- // 创建报警阈值设置
24+ // 创建报警阈值控制
25+ auto *alarmLabel = new QLabel (tr (" Alarm threshold:" ), this );
2326 auto *alarmSlider = new QSlider (Qt::Horizontal, this );
2427 alarmSlider->setRange (0 , 50 );
2528 alarmSlider->setValue (20 );
26- auto *alarmLabel = new QLabel (tr (" Alarm threshold: 20%" ), this );
29+ auto *alarmSpinBox = new QSpinBox (this );
30+ alarmSpinBox->setRange (0 , 50 );
31+ alarmSpinBox->setValue (20 );
32+ alarmSpinBox->setSuffix (" %" );
33+
34+ // 创建充电状态控制
35+ auto *chargingCheckbox = new QCheckBox (tr (" Charging state" ), this );
36+ chargingCheckbox->setChecked (false );
2737
28- // 创建颜色选择控件 - 使用颜色预览按钮
38+ // 创建颜色选择控件
2939 auto *powerColorButton = new QPushButton (this );
3040 auto *alarmColorButton = new QPushButton (this );
3141 auto *borderColorButton = new QPushButton (this );
3242
3343 // 创建动画控制
34- auto *animationCheckbox = new QCheckBox (tr (" Enable animation" ), this );
35- animationCheckbox->setChecked (true );
3644 auto *animationDurationSlider = new QSlider (Qt::Horizontal, this );
3745 animationDurationSlider->setRange (100 , 2000 );
3846 animationDurationSlider->setValue (500 );
3947 auto *durationLabel = new QLabel (tr (" Animation duration: 500ms" ), this );
4048
41- // 创建快速操作按钮
42- auto *increaseButton = new QPushButton (" +10% " , this );
43- auto *decreaseButton = new QPushButton (" -10% " , this );
49+ // 创建操作按钮
50+ auto *increaseButton = new QPushButton (tr ( " +10" ) , this );
51+ auto *decreaseButton = new QPushButton (tr ( " -10" ) , this );
4452 auto *resetButton = new QPushButton (tr (" Reset" ), this );
53+ auto *animateToButton = new QPushButton (tr (" Animate to 50" ), this );
4554
4655 // 创建状态显示标签
4756 auto *statusLabel = new QLabel (tr (" Status: Normal" ), this );
4857 statusLabel->setAlignment (Qt::AlignCenter);
4958 statusLabel->setFrameStyle (QFrame::Box);
59+ statusLabel->setMinimumHeight (30 );
5060
51- // 布局设置
61+ // 布局设置 - 针对横向电池优化
5262 auto *mainWidget = new QWidget (this );
5363 auto *mainLayout = new QVBoxLayout (mainWidget);
5464
55- // 电池和滑动条布局
56- auto *batteryLayout = new QHBoxLayout ();
65+ // 顶部:电池显示区域
66+ auto *batteryContainer = new QWidget (this );
67+ auto *batteryLayout = new QHBoxLayout (batteryContainer);
68+ batteryLayout->addStretch ();
5769 batteryLayout->addWidget (battery);
58- batteryLayout->addWidget (slider);
59-
60- // 充电和动画控制布局
61- auto *gridLayout = new QGridLayout;
62- gridLayout->addWidget (chargingCheckbox, 0 , 0 );
63- gridLayout->addWidget (animationCheckbox, 0 , 1 );
64- gridLayout->addWidget (new QLabel (tr (" Battery color:" ), this ), 1 , 0 );
65- gridLayout->addWidget (powerColorButton, 1 , 1 );
66- gridLayout->addWidget (new QLabel (tr (" Alarm color:" ), this ), 2 , 0 );
67- gridLayout->addWidget (alarmColorButton, 2 , 1 );
68- gridLayout->addWidget (new QLabel (tr (" Border color:" ), this ), 3 , 0 );
69- gridLayout->addWidget (borderColorButton, 3 , 1 );
70- gridLayout->addWidget (alarmLabel, 4 , 0 );
71- gridLayout->addWidget (alarmSlider, 4 , 1 );
72- gridLayout->addWidget (durationLabel, 5 , 0 );
73- gridLayout->addWidget (animationDurationSlider, 5 , 1 );
74-
75- // 快速操作布局
76- auto *quickActionsLayout = new QHBoxLayout ();
77- quickActionsLayout->addWidget (increaseButton);
78- quickActionsLayout->addWidget (decreaseButton);
79- quickActionsLayout->addWidget (resetButton);
70+ batteryLayout->addStretch ();
71+
72+ // 控制面板区域
73+ auto *controlPanel = new QWidget (this );
74+ auto *controlLayout = new QGridLayout (controlPanel);
75+
76+ // 第一行:数值控制
77+ int row = 0 ;
78+ controlLayout->addWidget (valueLabel, row, 0 );
79+ controlLayout->addWidget (valueSlider, row, 1 );
80+ controlLayout->addWidget (valueSpinBox, row, 2 );
81+
82+ // 第二行:报警阈值
83+ row++;
84+ controlLayout->addWidget (alarmLabel, row, 0 );
85+ controlLayout->addWidget (alarmSlider, row, 1 );
86+ controlLayout->addWidget (alarmSpinBox, row, 2 );
87+
88+ // 第三行:颜色控制
89+ row++;
90+ controlLayout->addWidget (new QLabel (tr (" Power color:" ), this ), row, 0 );
91+ controlLayout->addWidget (powerColorButton, row, 1 );
92+
93+ row++;
94+ controlLayout->addWidget (new QLabel (tr (" Alarm color:" ), this ), row, 0 );
95+ controlLayout->addWidget (alarmColorButton, row, 1 );
96+
97+ row++;
98+ controlLayout->addWidget (new QLabel (tr (" Border color:" ), this ), row, 0 );
99+ controlLayout->addWidget (borderColorButton, row, 1 );
100+
101+ // 第四行:动画控制
102+ row++;
103+ controlLayout->addWidget (durationLabel, row, 0 );
104+ controlLayout->addWidget (animationDurationSlider, row, 1 );
105+
106+ // 第五行:操作按钮
107+ row++;
108+ controlLayout->addWidget (new QLabel (tr (" Actions:" ), this ), row, 0 );
109+ controlLayout->addWidget (increaseButton, row, 1 );
110+ controlLayout->addWidget (animateToButton, row, 2 );
111+
112+ row++;
113+ controlLayout->addWidget (decreaseButton, row, 1 );
114+ controlLayout->addWidget (resetButton, row, 2 );
115+
116+ // 第六行:充电控制
117+ row++;
118+ controlLayout->addWidget (new QLabel (tr (" Charging:" ), this ), row, 0 );
119+ controlLayout->addWidget (chargingCheckbox, row, 1 );
80120
81121 // 主布局组装
82- mainLayout->addLayout (batteryLayout);
83- mainLayout->addLayout (gridLayout);
84- mainLayout->addLayout (quickActionsLayout);
122+ mainLayout->addWidget (batteryContainer);
123+ mainLayout->addWidget (controlPanel);
85124 mainLayout->addWidget (statusLabel);
86125
87126 setCentralWidget (mainWidget);
88- resize (300 , 350 );
127+ resize (600 , 450 ); // 更适合横向电池的窗口大小
89128 setWindowTitle (tr (" Battery Widget Example" ));
90129
91- // 连接信号和槽
92-
93- // 电量控制
94- connect (slider, &QSlider::valueChanged, battery, [battery, animationCheckbox](int value) {
95- if (animationCheckbox->isChecked ()) {
96- battery->setValueAnimated (value);
97- } else {
98- battery->setValue (value);
99- }
100- });
101-
102- // 充电状态
103- connect (chargingCheckbox, &QCheckBox::toggled, battery, &BatteryWidget::setCharging);
104-
105- // 报警阈值
106- connect (alarmSlider, &QSlider::valueChanged, battery, [battery, alarmLabel](int value) {
107- battery->setAlarmValue (value);
108- alarmLabel->setText (tr (" Alarm threshold: %1%" ).arg (value));
109- });
130+ // ========== 颜色设置部分 ==========
110131
111- // 动画控制
112- connect (animationDurationSlider,
113- &QSlider::valueChanged,
114- battery,
115- [battery, durationLabel](int value) {
116- battery->setAnimationDuration (value);
117- durationLabel->setText (tr (" Animation duration: %1ms" ).arg (value));
118- });
119-
120- // 颜色选择 - 更新按钮颜色预览
132+ // 统一的颜色按钮更新函数
121133 auto updateColorButton = [](QPushButton *button, const QColor &color) {
122134 auto colorName = color.name (QColor::HexArgb).toUpper ();
123135
@@ -142,17 +154,62 @@ MainWindow::MainWindow(QWidget *parent)
142154 button->setText (colorName);
143155 };
144156
157+ // 初始化颜色按钮
145158 updateColorButton (powerColorButton, battery->powerColor ());
146159 updateColorButton (alarmColorButton, battery->alarmColor ());
147160 updateColorButton (borderColorButton, battery->borderColor ());
148161
162+ // ========== 信号连接 ==========
163+
164+ // 数值控制 - 避免循环信号
165+ connect (valueSlider, &QSlider::valueChanged, this , [battery, valueSpinBox](int value) {
166+ // 阻塞spinbox信号,避免循环
167+ valueSpinBox->blockSignals (true );
168+ battery->setValue (value);
169+ valueSpinBox->setValue (value);
170+ valueSpinBox->blockSignals (false );
171+ });
172+
173+ connect (valueSpinBox,
174+ QOverload<int >::of (&QSpinBox::valueChanged),
175+ this ,
176+ [battery, valueSlider](int value) {
177+ // 阻塞slider信号,避免循环
178+ valueSlider->blockSignals (true );
179+ battery->setValue (value);
180+ valueSlider->setValue (value);
181+ valueSlider->blockSignals (false );
182+ });
183+
184+ // 报警阈值控制 - 避免循环信号
185+ connect (alarmSlider, &QSlider::valueChanged, this , [battery, alarmSpinBox](int value) {
186+ alarmSpinBox->blockSignals (true );
187+ battery->setAlarmValue (value);
188+ alarmSpinBox->setValue (value);
189+ alarmSpinBox->blockSignals (false );
190+ });
191+
192+ connect (alarmSpinBox,
193+ QOverload<int >::of (&QSpinBox::valueChanged),
194+ this ,
195+ [battery, alarmSlider](int value) {
196+ alarmSlider->blockSignals (true );
197+ battery->setAlarmValue (value);
198+ alarmSlider->setValue (value);
199+ alarmSlider->blockSignals (false );
200+ });
201+
202+ // 充电状态
203+ connect (chargingCheckbox, &QCheckBox::toggled, battery, &BatteryWidget::setCharging);
204+
205+ // 颜色设置
149206 connect (powerColorButton,
150207 &QPushButton::clicked,
151208 this ,
152209 [this , battery, powerColorButton, updateColorButton]() {
153210 QColor color = QColorDialog::getColor (battery->powerColor (),
154211 this ,
155- tr (" Select the battery color " ));
212+ tr (" Select Power Color " ));
156213 if (color.isValid ()) {
157214 battery->setPowerColor (color);
158215 updateColorButton (powerColorButton, color);
@@ -165,7 +222,7 @@ MainWindow::MainWindow(QWidget *parent)
165222 [this , battery, alarmColorButton, updateColorButton]() {
166223 QColor color = QColorDialog::getColor (battery->alarmColor (),
167224 this ,
168- tr (" Select the alarm color " ));
225+ tr (" Select Alarm Color " ));
169226 if (color.isValid ()) {
170227 battery->setAlarmColor (color);
171228 updateColorButton (alarmColorButton, color);
@@ -178,24 +235,77 @@ MainWindow::MainWindow(QWidget *parent)
178235 [this , battery, borderColorButton, updateColorButton]() {
179236 QColor color = QColorDialog::getColor (battery->borderColor (),
180237 this ,
181- tr (" Select the border color " ));
238+ tr (" Select Border Color " ));
182239 if (color.isValid ()) {
183240 battery->setBorderColor (color);
184241 updateColorButton (borderColorButton, color);
185242 }
186243 });
187244
188- // 快速操作
189- connect (increaseButton, &QPushButton::clicked, battery, [battery]() {
245+ // 动画设置
246+ connect (animationDurationSlider,
247+ &QSlider::valueChanged,
248+ this ,
249+ [battery, durationLabel](int value) {
250+ battery->setAnimationDuration (value);
251+ durationLabel->setText (tr (" Animation duration: %1ms" ).arg (value));
252+ });
253+
254+ // 操作按钮
255+ connect (increaseButton, &QPushButton::clicked, this , [battery]() {
190256 battery->increaseValue (10 );
191257 });
192258
193- connect (decreaseButton, &QPushButton::clicked, battery , [battery]() {
259+ connect (decreaseButton, &QPushButton::clicked, this , [battery]() {
194260 battery->decreaseValue (10 );
195261 });
196262
197263 connect (resetButton, &QPushButton::clicked, battery, &BatteryWidget::reset);
198264
265+ connect (animateToButton, &QPushButton::clicked, this , [battery]() {
266+ battery->setValueAnimated (50 );
267+ });
268+
269+ // 电池信号连接 - 更新UI状态
270+ connect (battery,
271+ &BatteryWidget::valueChanged,
272+ this ,
273+ [valueLabel, valueSlider, valueSpinBox](int value) {
274+ valueLabel->setText (tr (" Current value: %1%" ).arg (value));
275+
276+ // 阻塞信号避免循环
277+ valueSlider->blockSignals (true );
278+ valueSlider->setValue (value);
279+ valueSlider->blockSignals (false );
280+
281+ valueSpinBox->blockSignals (true );
282+ valueSpinBox->setValue (value);
283+ valueSpinBox->blockSignals (false );
284+ });
285+
286+ connect (battery, &BatteryWidget::valueIncreased, this , [](int newValue) {
287+ qDebug () << " Value increased to:" << newValue;
288+ });
289+
290+ connect (battery, &BatteryWidget::valueDecreased, this , [](int newValue) {
291+ qDebug () << " Value decreased to:" << newValue;
292+ });
293+
294+ connect (battery, &BatteryWidget::valueReset, this , []() { qDebug () << " Value reset" ; });
295+
296+ connect (battery,
297+ &BatteryWidget::animationStarted,
298+ this ,
299+ [statusLabel](int oldValue, int newValue) {
300+ statusLabel->setText (tr (" Animating: %1% → %2%" ).arg (oldValue).arg (newValue));
301+ statusLabel->setStyleSheet (" color: orange;" );
302+ });
303+
304+ connect (battery, &BatteryWidget::animationFinished, this , [statusLabel](int value) {
305+ statusLabel->setText (tr (" Animation finished: %1%" ).arg (value));
306+ statusLabel->setStyleSheet (" color: green;" );
307+ });
308+
199309 connect (battery, &BatteryWidget::alarmStateChanged, this , [statusLabel](bool isAlarm) {
200310 if (isAlarm) {
201311 statusLabel->setText (tr (" Status: Low battery alarm!" ));
@@ -210,26 +320,19 @@ MainWindow::MainWindow(QWidget *parent)
210320 &BatteryWidget::chargingChanged,
211321 this ,
212322 [statusLabel, chargingCheckbox](bool charging) {
323+ chargingCheckbox->blockSignals (true );
213324 chargingCheckbox->setChecked (charging);
325+ chargingCheckbox->blockSignals (false );
326+
214327 if (charging) {
215328 statusLabel->setText (tr (" Status: Charging..." ));
216329 statusLabel->setStyleSheet (" color: blue;" );
217330 }
218331 });
219332
220- connect (battery,
221- &BatteryWidget::animationStarted,
222- this ,
223- [statusLabel](int oldValue, int newValue) {
224- statusLabel->setText (tr (" Animation: %1% → %2%" ).arg (oldValue).arg (newValue));
225- });
226-
227- connect (battery, &BatteryWidget::animationFinished, this , [statusLabel](int value) {
228- statusLabel->setText (tr (" The animation is completed: %1%" ).arg (value));
229- });
230-
231333 // 初始化状态
232334 battery->setValue (75 );
335+ valueLabel->setText (tr (" Current value: 75%" ));
233336}
234337
235338MainWindow::~MainWindow () {}
0 commit comments