33
44#include < QtWidgets>
55
6- MainWindow::MainWindow (QWidget *parent)
7- : QMainWindow(parent)
6+ using namespace Qt ::StringLiterals;
7+
8+ MainWindow::MainWindow (QWidget *parent) : QMainWindow(parent)
89{
910 // 创建开关按钮控件
1011 auto *switchButton = new SwitchButton (this );
12+ switchButton->setFixedSize (80 , 40 );
13+
14+ // 创建状态显示标签
15+ auto *statusLabel = new QLabel (tr (" Unchecked" ), this );
16+ statusLabel->setAlignment (Qt::AlignCenter);
17+ statusLabel->setFrameStyle (QFrame::StyledPanel | QFrame::Sunken);
18+ statusLabel->setMinimumHeight (30 );
1119
1220 // 创建状态控制
13- auto *stateCheckbox = new QCheckBox (tr (" Checked State" ), this );
14- stateCheckbox->setChecked (false );
21+ auto *stateCheckbox = new QCheckBox (tr (" Checked" ), this );
1522
1623 // 创建颜色选择控件
1724 auto *checkedColorButton = new QPushButton (this );
@@ -21,66 +28,68 @@ MainWindow::MainWindow(QWidget *parent)
2128
2229 // 创建动画控制
2330 auto *animationDurationSlider = new QSlider (Qt::Horizontal, this );
24- animationDurationSlider->setRange (50 , 1000 );
25- animationDurationSlider->setValue (120 );
26- auto *durationLabel = new QLabel (tr (" Animation duration: 120ms" ), this );
31+ animationDurationSlider->setRange (0 , 1000 );
32+ animationDurationSlider->setValue (switchButton->animationDuration ());
33+ auto *durationLabel
34+ = new QLabel (tr (" Animation duration: %1ms" ).arg (switchButton->animationDuration ()));
2735
28- // 创建状态显示标签
29- auto *statusLabel = new QLabel (tr (" Status: Unchecked" ), this );
30- statusLabel->setAlignment (Qt::AlignCenter);
31- statusLabel->setFrameStyle (QFrame::Box);
32- statusLabel->setMinimumHeight (30 );
36+ // ========== 布局设置 ==========
3337
34- // 布局设置
3538 auto *mainWidget = new QWidget (this );
36- auto *mainLayout = new QVBoxLayout (mainWidget);
39+ auto *mainLayout = new QHBoxLayout (mainWidget);
40+
41+ // 左侧:展示区域
42+ auto *displayLayout = new QVBoxLayout ();
43+ displayLayout->addStretch ();
44+ displayLayout->addWidget (switchButton, 0 , Qt::AlignCenter);
45+ displayLayout->addSpacing (10 );
46+ displayLayout->addWidget (statusLabel);
47+ displayLayout->addStretch ();
3748
38- // 控制面板区域
49+ // 右侧:控制面板
3950 auto *controlPanel = new QWidget (this );
40- auto *controlLayout = new QGridLayout (controlPanel);
41-
42- // 第一行:状态控制
43- int row = 0 ;
44- controlLayout-> addWidget ( new QLabel ( tr ( " Button state: " ), this ), row, 0 );
45- controlLayout ->addWidget (stateCheckbox, row, 1 );
46-
47- // 第二行:颜色控制 - 选中颜色
48- row++ ;
49- controlLayout-> addWidget ( new QLabel ( tr ( " Checked color: " ), this ), row, 0 );
50- controlLayout-> addWidget (checkedColorButton, row, 1 );
51-
52- // 第三行:颜色控制 - 未选中颜色
53- row++ ;
54- controlLayout ->addWidget (new QLabel ( tr ( " Unchecked color: " ), this ), row, 0 );
55- controlLayout ->addWidget (uncheckedColorButton, row, 1 );
56-
57- // 第四行:颜色控制 - 滑块颜色
58- row++ ;
59- controlLayout-> addWidget ( new QLabel ( tr ( " Thumb color: " ), this ), row, 0 );
60- controlLayout-> addWidget (thumbColorButton, row, 1 );
61-
62- // 第五行:颜色控制 - 滑块边框颜色
63- row++ ;
64- controlLayout ->addWidget (new QLabel ( tr ( " Thumb border color: " ), this ), row, 0 );
65- controlLayout-> addWidget (thumbBorderColorButton, row, 1 );
66-
67- // 第六行:动画控制
68- row++ ;
69- controlLayout->addWidget (durationLabel, row, 0 );
70- controlLayout->addWidget (animationDurationSlider, row, 1 );
71-
72- // 主布局组装
73- mainLayout->addWidget (switchButton );
51+ auto *controlLayout = new QVBoxLayout (controlPanel);
52+
53+ // 状态控制布局
54+ auto *stateGroup = new QGroupBox ( tr ( " State " ), this ) ;
55+ auto *stateLayout = new QVBoxLayout (stateGroup );
56+ stateLayout ->addWidget (stateCheckbox);
57+
58+ // 颜色控制布局
59+ auto *colorGroup = new QGroupBox ( tr ( " Color settings " ), this ) ;
60+ auto *colorLayout = new QGridLayout (colorGroup );
61+
62+ colorLayout-> addWidget ( new QLabel ( tr ( " Checked color: " ), this ), 0 , 0 );
63+ colorLayout-> addWidget (checkedColorButton, 0 , 1 );
64+ colorLayout-> addWidget ( new QLabel ( tr ( " Unchecked color: " ), this ), 1 , 0 ) ;
65+ colorLayout ->addWidget (uncheckedColorButton, 1 , 1 );
66+ colorLayout ->addWidget (new QLabel ( tr ( " Thumb color: " ), this ), 2 , 0 );
67+ colorLayout-> addWidget (thumbColorButton, 2 , 1 );
68+ colorLayout-> addWidget ( new QLabel ( tr ( " Thumb border: " ), this ), 3 , 0 );
69+ colorLayout-> addWidget (thumbBorderColorButton, 3 , 1 ) ;
70+
71+ // 动画控制布局
72+ auto *animationGroup = new QGroupBox ( tr ( " Animation settings " ), this );
73+ auto *animationLayout = new QVBoxLayout (animationGroup);
74+ animationLayout-> addWidget (durationLabel) ;
75+ animationLayout ->addWidget (animationDurationSlider );
76+
77+ // 组装控制面板
78+ controlLayout-> addWidget (stateGroup);
79+ controlLayout-> addWidget (colorGroup) ;
80+ controlLayout->addWidget (animationGroup );
81+ controlLayout->addStretch ( );
82+
83+ // 主布局
84+ mainLayout->addLayout (displayLayout, 2 );
7485 mainLayout->addWidget (controlPanel);
75- mainLayout->addWidget (statusLabel);
7686
7787 setCentralWidget (mainWidget);
78- resize (300 , 500 );
88+ resize (600 , 350 );
7989 setWindowTitle (tr (" Switch Button Example" ));
8090
81- // ========== 颜色设置部分 ==========
91+ // ========== 颜色按钮更新 ==========
8292
83- // 统一的颜色按钮更新函数
8493 auto updateColorButton = [](QPushButton *button, const QColor &color) {
8594 auto colorName = color.name (QColor::HexArgb).toUpper ();
8695
@@ -94,12 +103,10 @@ MainWindow::MainWindow(QWidget *parent)
94103 };
95104
96105 double luminance = getRelativeLuminance (color.red (), color.green (), color.blue ());
97-
98- // 根据WCAG标准选择对比度足够的文字颜色
99- QString textColor = luminance > 0.179 ? " black" : " white" ;
106+ QString textColor = luminance > 0.179 ? u" black" _s : u" white" _s;
100107
101108 button->setStyleSheet (
102- QString ( " background-color: %1; color: %2; border: 1px solid gray; padding: 5px;" )
109+ u " background-color: %1; color: %2; border: 1px solid gray; padding: 5px;" _s
103110 .arg (colorName)
104111 .arg (textColor));
105112 button->setText (colorName);
@@ -115,7 +122,6 @@ MainWindow::MainWindow(QWidget *parent)
115122
116123 // 状态控制
117124 connect (stateCheckbox, &QCheckBox::toggled, this , [switchButton, stateCheckbox](bool checked) {
118- // 阻塞信号避免循环
119125 stateCheckbox->blockSignals (true );
120126 switchButton->setChecked (checked);
121127 stateCheckbox->blockSignals (false );
@@ -126,9 +132,8 @@ MainWindow::MainWindow(QWidget *parent)
126132 &QPushButton::clicked,
127133 this ,
128134 [this , switchButton, checkedColorButton, updateColorButton]() {
129- QColor color = QColorDialog::getColor (switchButton->checkedColor (),
130- this ,
131- tr (" Select Checked Color" ));
135+ QColor color = QColorDialog::getColor (
136+ switchButton->checkedColor (), this , tr (" Select Checked Color" ));
132137 if (color.isValid ()) {
133138 switchButton->setCheckedColor (color);
134139 updateColorButton (checkedColorButton, color);
@@ -139,9 +144,8 @@ MainWindow::MainWindow(QWidget *parent)
139144 &QPushButton::clicked,
140145 this ,
141146 [this , switchButton, uncheckedColorButton, updateColorButton]() {
142- QColor color = QColorDialog::getColor (switchButton->uncheckedColor (),
143- this ,
144- tr (" Select Unchecked Color" ));
147+ QColor color = QColorDialog::getColor (
148+ switchButton->uncheckedColor (), this , tr (" Select Unchecked Color" ));
145149 if (color.isValid ()) {
146150 switchButton->setUncheckedColor (color);
147151 updateColorButton (uncheckedColorButton, color);
@@ -152,9 +156,8 @@ MainWindow::MainWindow(QWidget *parent)
152156 &QPushButton::clicked,
153157 this ,
154158 [this , switchButton, thumbColorButton, updateColorButton]() {
155- QColor color = QColorDialog::getColor (switchButton->thumbColor (),
156- this ,
157- tr (" Select Thumb Color" ));
159+ QColor color = QColorDialog::getColor (
160+ switchButton->thumbColor (), this , tr (" Select Thumb Color" ));
158161 if (color.isValid ()) {
159162 switchButton->setThumbColor (color);
160163 updateColorButton (thumbColorButton, color);
@@ -165,9 +168,8 @@ MainWindow::MainWindow(QWidget *parent)
165168 &QPushButton::clicked,
166169 this ,
167170 [this , switchButton, thumbBorderColorButton, updateColorButton]() {
168- QColor color = QColorDialog::getColor (switchButton->thumbBorderColor (),
169- this ,
170- tr (" Select Thumb Border Color" ));
171+ QColor color = QColorDialog::getColor (
172+ switchButton->thumbBorderColor (), this , tr (" Select Thumb Border Color" ));
171173 if (color.isValid ()) {
172174 switchButton->setThumbBorderColor (color);
173175 updateColorButton (thumbBorderColorButton, color);
@@ -183,7 +185,7 @@ MainWindow::MainWindow(QWidget *parent)
183185 durationLabel->setText (tr (" Animation duration: %1ms" ).arg (value));
184186 });
185187
186- // 开关按钮信号连接 - 更新UI状态
188+ // 开关按钮信号
187189 connect (switchButton, &SwitchButton::toggled, this , [stateCheckbox, statusLabel](bool checked) {
188190 // 更新状态复选框
189191 stateCheckbox->blockSignals (true );
@@ -192,28 +194,23 @@ MainWindow::MainWindow(QWidget *parent)
192194
193195 // 更新状态标签
194196 if (checked) {
195- statusLabel->setText (tr (" Status: Checked" ));
196- statusLabel->setStyleSheet (" color: green;" );
197+ statusLabel->setText (tr (" Checked" ));
198+ statusLabel->setStyleSheet (u " color: green;" _s );
197199 } else {
198- statusLabel->setText (tr (" Status: Unchecked" ));
199- statusLabel->setStyleSheet (" " );
200+ statusLabel->setText (tr (" Unchecked" ));
201+ statusLabel->setStyleSheet (QString{} );
200202 }
201203 });
202204
203205 connect (switchButton, &SwitchButton::animationStarted, this , [statusLabel](bool checked) {
204- QString state = checked ? " Checked" : " Unchecked" ;
206+ QString state = checked ? tr ( " Checked" ) : tr ( " Unchecked" ) ;
205207 statusLabel->setText (tr (" Animating to: %1" ).arg (state));
206- statusLabel->setStyleSheet (" color: orange;" );
208+ statusLabel->setStyleSheet (u " color: orange;" _s );
207209 });
208210
209211 connect (switchButton, &SwitchButton::animationFinished, this , [statusLabel](bool checked) {
210- QString state = checked ? " Checked" : " Unchecked" ;
211- statusLabel->setText (tr (" Animation finished : %1" ).arg (state));
212- statusLabel->setStyleSheet (checked ? " color: green;" : " " );
212+ QString state = checked ? tr ( " Checked" ) : tr ( " Unchecked" ) ;
213+ statusLabel->setText (tr (" Finished : %1" ).arg (state));
214+ statusLabel->setStyleSheet (checked ? u " color: green;" _s : QString{} );
213215 });
214-
215- // 初始化状态
216- switchButton->setChecked (false );
217216}
218-
219- MainWindow::~MainWindow () {}
0 commit comments