| 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 <QLabel> |
| 25 |
#include <QPushButton> |
| 26 |
#include <QVBoxLayout> |
| 27 |
|
| 28 |
static const QString password = "kylin"; |
| 29 |
Widget::Widget(QWidget *parent) |
| 30 |
: KWidget(parent) |
| 31 |
{ |
| 32 |
QVBoxLayout *vLayout = new QVBoxLayout(this); |
| 33 |
QHBoxLayout *hLayout = new QHBoxLayout(this); |
| 34 |
|
| 35 |
m_pPasswordEdit = new KPasswordEdit(this); |
| 36 |
hLayout->addWidget(m_pPasswordEdit); |
| 37 |
m_pPasswordEdit1 = new KPasswordEdit(this); |
| 38 |
m_pPasswordEdit2 = new KPasswordEdit(this); |
| 39 |
|
| 40 |
QPushButton *confirmBtn = new QPushButton("confirm", this); |
| 41 |
hLayout->addWidget(confirmBtn); |
| 42 |
|
| 43 |
vLayout->addLayout(hLayout); |
| 44 |
vLayout->addWidget(m_pPasswordEdit1); |
| 45 |
vLayout->addWidget(m_pPasswordEdit2); |
| 46 |
hLayout = new QHBoxLayout(this); |
| 47 |
QPushButton *enableBtn = new QPushButton("enable", this); |
| 48 |
hLayout->addWidget(enableBtn); |
| 49 |
QPushButton *loadingBtn = new QPushButton("isLoading:false", this); |
| 50 |
hLayout->addWidget(loadingBtn); |
| 51 |
vLayout->addLayout(hLayout); |
| 52 |
this->baseBar()->setLayout(vLayout); |
| 53 |
|
| 54 |
QString str("dwadwa"); |
| 55 |
m_pPasswordEdit1->setPlaceholderText(str); // 设置背景文字 |
| 56 |
m_pPasswordEdit1->setClearButtonEnabled(false); // 禁用ClearBtn按钮 |
| 57 |
m_pPasswordEdit2->setEchoModeBtnVisible(false); // 隐藏EchoMode按钮 |
| 58 |
bool flag = m_pPasswordEdit2->echoModeBtnVisible(); // 返回EchoMode按钮是否隐藏 |
| 59 |
m_pPasswordEdit2->setEchoMode(QLineEdit::EchoMode::Normal); // 明文输入 |
| 60 |
m_pPasswordEdit->setClearBtnVisible(false); // 隐藏ClearBtn按钮(启用但隐藏) |
| 61 |
bool flag1 = m_pPasswordEdit->clearBtnVisible(); // 返回ClearBtn按钮是否隐藏 |
| 62 |
|
| 63 |
connect(confirmBtn, &QPushButton::clicked, this, [=]() { |
| 64 |
if (m_pPasswordEdit->text() == password) |
| 65 |
m_pPasswordEdit->setState(LoginState::LoginSuccess); // 设置登录状态 |
| 66 |
|
| 67 |
else |
| 68 |
m_pPasswordEdit->setState(LoginState::LoginFailed); |
| 69 |
m_pPasswordEdit->setFocus(); |
| 70 |
}); |
| 71 |
connect(m_pPasswordEdit, &KPasswordEdit::returnPressed, this, [=]() { |
| 72 |
if (m_pPasswordEdit->text() == password) |
| 73 |
m_pPasswordEdit->setState(LoginState::LoginSuccess); |
| 74 |
|
| 75 |
else |
| 76 |
m_pPasswordEdit->setState(LoginState::LoginFailed); |
| 77 |
m_pPasswordEdit->setFocus(); |
| 78 |
}); |
| 79 |
connect(enableBtn, &QPushButton::clicked, this, [=]() { |
| 80 |
if (m_pPasswordEdit->isEnabled()) { |
| 81 |
m_pPasswordEdit->setEnabled(false); // 设置KLineEdit不可用 |
| 82 |
enableBtn->setText("disable"); |
| 83 |
} else { |
| 84 |
m_pPasswordEdit->setEnabled(true); |
| 85 |
enableBtn->setText("enable"); |
| 86 |
} |
| 87 |
}); |
| 88 |
connect(loadingBtn, &QPushButton::clicked, this, [=]() { |
| 89 |
if (!m_pPasswordEdit->isLoading()) { |
| 90 |
qDebug() << "is loading"; |
| 91 |
m_pPasswordEdit->setLoading(true); // 设置启用加载状态 |
| 92 |
loadingBtn->setText("isLoading:true"); |
| 93 |
} else { |
| 94 |
qDebug() << "is not loading"; |
| 95 |
m_pPasswordEdit->setLoading(false); |
| 96 |
loadingBtn->setText("isLoading:false"); |
| 97 |
} |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
Widget::~Widget() |
| 102 |
{ |
| 103 |
} |