Files
QT_wuhan_YT/pages/passwordmgmtlogin.cpp
T
2026-10-14 18:22:41 +08:00

123 lines
3.6 KiB
C++

#include "passwordmgmtlogin.h"
#include "numpaddialog.h"
#include <QKeyEvent>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include "global.h"
PasswordMgmtLoginPage::PasswordMgmtLoginPage(AppContext* ctx, QWidget* parent)
: BasePage(ctx, parent)
{
QVBoxLayout* root = new QVBoxLayout(this);
root->addStretch(1);
QWidget* col = new QWidget(this);
col->setFixedWidth(700);
QVBoxLayout* lay = new QVBoxLayout(col);
lay->setSpacing(50);
lay->setAlignment(Qt::AlignHCenter);
QLabel* title = label("密码管理登录界面", 60, C::TEXT, true, col);
title->setAlignment(Qt::AlignCenter);
lay->addWidget(title);
QLabel* sub = label("请输入管理员密码", 40, C::TEXT_SUB, false, col);
sub->setAlignment(Qt::AlignCenter);
lay->addWidget(sub);
m_input = new QLineEdit(col);
m_input->setReadOnly(true);
m_input->setEchoMode(QLineEdit::Password);
m_input->setAlignment(Qt::AlignCenter);
m_input->setFixedSize(400, 90);
m_input->setStyleSheet("font-size:60px;");
m_input->setFocusPolicy(Qt::StrongFocus);
m_input->installEventFilter(this);
lay->addWidget(m_input, 0, Qt::AlignHCenter);
// 错误提示(预留固定高度,出现/消失不影响下方按钮位置)
m_error = new QLabel(col);
m_error->setAlignment(Qt::AlignCenter);
m_error->setFixedHeight(56);
m_error->setStyleSheet("color:#E5484D;font-size:40px;font-weight:700;");
lay->addWidget(m_error);
QHBoxLayout* btnRow = new QHBoxLayout();
btnRow->setSpacing(80);
btnRow->addStretch(1);
QPushButton* ok = button("确认", "", col);
ok->setFixedSize(160, 120);
ok->setStyleSheet("font-size:60px;");
connect(ok, &QPushButton::clicked, this, &PasswordMgmtLoginPage::onConfirm);
QPushButton* cancel = button("取消", "", col);
cancel->setFixedSize(160, 120);
cancel->setStyleSheet(cancelButtonStyle(60)); // 选中时红底白字
connect(cancel, &QPushButton::clicked, this, &PasswordMgmtLoginPage::onCancel);
btnRow->addWidget(ok);
btnRow->addWidget(cancel);
btnRow->addStretch(1);
lay->addLayout(btnRow);
root->addWidget(col, 0, Qt::AlignHCenter);
root->addStretch(1);
}
bool PasswordMgmtLoginPage::eventFilter(QObject* obj, QEvent* ev)
{
if (obj == m_input) {
if (ev->type() == QEvent::MouseButtonPress) {
onInputClicked();
return true;
}
if (ev->type() == QEvent::KeyPress) {
QKeyEvent* ke = static_cast<QKeyEvent*>(ev);
// 空格当“点击”:Tab 聚焦后按空格弹出数字键盘
if (Keys::space(ke->key())) {
onInputClicked();
return true;
}
}
}
return BasePage::eventFilter(obj, ev);
}
void PasswordMgmtLoginPage::onInputClicked()
{
NumPadDialog dlg(this);
dlg.setMaxLen(4);
dlg.setValue(m_value);
if (dlg.exec() == QDialog::Accepted) {
m_value = dlg.value();
m_input->setText(m_value);
m_error->setText("");
}
}
void PasswordMgmtLoginPage::onConfirm()
{
if (m_value == ctx()->adminPassword()) {
clearInput();
emit navigate(PageMgmt);
} else {
m_value.clear(); // 输错后清空输入框
m_input->setText(m_value);
m_error->setText("管理员密码错误");
}
}
void PasswordMgmtLoginPage::onCancel()
{
clearInput();
emit navigate(PageBoot);
}
void PasswordMgmtLoginPage::clearInput()
{
m_value.clear();
m_input->setText(m_value);
m_error->setText("");
}