Debian Codegraph @

kysdk-qtwidgets/test/testSwitchButton/widget.cpp

raw
1 /*
2 *
3 *
4 * Copyright (C) 2023, KylinSoft Co., Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Zhen Sun <[email protected]>
20 *
21 */
22
23 #include "widget.h"
24 #include <QBoxLayout>
25
26 Widget::Widget(QWidget *parent)
27 : KWidget(parent)
28 {
29 QVBoxLayout *mainLayout = new QVBoxLayout;
30 QHBoxLayout *hLayout = new QHBoxLayout;
31 m_pSwitchBtn1 = new KSwitchButton(this); // 构造一个kswitchbutton
32 m_pSwitchBtn1->setFixedSize(50, 24); // 设置button的大小
33 m_pSwitchBtn1->setCheckable(true); // 设置是否可选中
34 m_pSwitchBtn1->setChecked(true); // 设置是否为选中状态
35 m_pLabel1 = new QLabel(this);
36 m_pLabel1->setText(m_pSwitchBtn1->isChecked() ? "ON" : "OFF"); // 获取button是否是选中状态
37 // 监听状态改变信号stateChanged
38 connect(m_pSwitchBtn1, &KSwitchButton::stateChanged, this, [=]() {
39 m_pLabel1->setText(m_pSwitchBtn1->isChecked() ? "ON" : "OFF");
40 });
41 m_pBtn1 = new KBorderlessButton(this);
42 m_pBtn1->setChecked(true);
43 m_pBtn1->setText(m_pSwitchBtn1->isEnabled() ? "Enable" : "Disable");
44 connect(m_pBtn1, &KBorderlessButton::clicked, this, [=]() {
45 m_pSwitchBtn1->setEnabled(!m_pSwitchBtn1->isEnabled());
46 m_pBtn1->setText(m_pSwitchBtn1->isEnabled() ? "Enable" : "Disable");
47 });
48 hLayout->addWidget(m_pSwitchBtn1);
49 hLayout->addWidget(m_pLabel1);
50 hLayout->addWidget(m_pBtn1);
51 hLayout->addStretch();
52 mainLayout->addLayout(hLayout);
53
54 hLayout = new QHBoxLayout;
55 m_pSwitchBtn2 = new KSwitchButton(this);
56 m_pSwitchBtn2->setFixedSize(100, 48);
57 m_pSwitchBtn2->setEnabled(false); // 设置button状态为不可点击状态
58 m_pLabel2 = new QLabel(this);
59 m_pLabel2->setText(m_pSwitchBtn2->isChecked() ? "ON" : "OFF");
60 connect(m_pSwitchBtn2, &KSwitchButton::stateChanged, this, [=]() {
61 m_pLabel2->setText(m_pSwitchBtn2->isChecked() ? "ON" : "OFF");
62 });
63 m_pBtn2 = new KBorderlessButton(this);
64 m_pBtn2->setText(m_pSwitchBtn2->isEnabled() ? "Enable" : "Disable");
65 connect(m_pBtn2, &KBorderlessButton::clicked, this, [=]() {
66 m_pSwitchBtn2->setEnabled(!m_pSwitchBtn2->isEnabled());
67 m_pBtn2->setText(m_pSwitchBtn2->isEnabled() ? "Enable" : "Disable");
68 });
69 hLayout->addWidget(m_pSwitchBtn2);
70 hLayout->addWidget(m_pLabel2);
71 hLayout->addWidget(m_pBtn2);
72 hLayout->addStretch();
73 mainLayout->addLayout(hLayout);
74
75 hLayout = new QHBoxLayout;
76 m_pSwitchBtn3 = new KSwitchButton(this);
77 m_pSwitchBtn3->setFixedSize(200, 96);
78 m_pLabel3 = new QLabel(this);
79 m_pLabel3->setText(m_pSwitchBtn3->isChecked() ? "ON" : "OFF");
80 connect(m_pSwitchBtn3, &KSwitchButton::stateChanged, this, [=]() {
81 m_pLabel3->setText(m_pSwitchBtn3->isChecked() ? "ON" : "OFF");
82 });
83 m_pBtn3 = new KBorderlessButton(this);
84 m_pBtn3->setText(m_pSwitchBtn3->isEnabled() ? "Enable" : "Disable");
85 connect(m_pBtn3, &KBorderlessButton::clicked, this, [=]() {
86 m_pSwitchBtn3->setEnabled(!m_pSwitchBtn3->isEnabled());
87 m_pBtn3->setText(m_pSwitchBtn3->isEnabled() ? "Enable" : "Disable");
88 });
89 hLayout->addWidget(m_pSwitchBtn3);
90 hLayout->addWidget(m_pLabel3);
91 hLayout->addWidget(m_pBtn3);
92 hLayout->addStretch();
93 mainLayout->addLayout(hLayout);
94
95 baseBar()->setLayout(mainLayout);
96 }
97
98 Widget::~Widget()
99 {
100 }