#include "passwordreset.h" #include "numpaddialog.h" #include #include "confirmdialog.h" #include #include #include #include #include #include #include "global.h" PasswordResetPage::PasswordResetPage(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(24); lay->setAlignment(Qt::AlignHCenter); QLabel* title = label("重置密码界面", 28, C::TEXT, true, col); title->setAlignment(Qt::AlignCenter); lay->addWidget(title); m_input = new QLineEdit(col); m_input->setReadOnly(true); m_input->setEchoMode(QLineEdit::Password); m_input->setAlignment(Qt::AlignCenter); m_input->setFixedSize(400, 88); m_input->setFocusPolicy(Qt::StrongFocus); m_input->installEventFilter(this); lay->addWidget(m_input, 0, Qt::AlignHCenter); m_hint = label("请输入 4 位有效密码", 18, C::TEXT_SUB, false, col); m_hint->setAlignment(Qt::AlignCenter); lay->addWidget(m_hint); QHBoxLayout* btnRow = new QHBoxLayout(); btnRow->addStretch(1); QPushButton* ok = button("确认", "", col); ok->setFixedSize(160, 72); connect(ok, &QPushButton::clicked, this, &PasswordResetPage::onConfirm); QPushButton* cancel = button("取消", "", col); cancel->setFixedSize(160, 72); connect(cancel, &QPushButton::clicked, this, &PasswordResetPage::onCancel); QPushButton* exit = button("退出", "", col); exit->setFixedSize(160, 72); connect(exit, &QPushButton::clicked, this, &PasswordResetPage::onCancel); btnRow->addWidget(ok); btnRow->addWidget(cancel); btnRow->addWidget(exit); btnRow->addStretch(1); lay->addLayout(btnRow); root->addWidget(col, 0, Qt::AlignHCenter); root->addStretch(1); } bool PasswordResetPage::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(ev); if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter) { onInputClicked(); return true; } } } return BasePage::eventFilter(obj, ev); } void PasswordResetPage::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_hint->setText("请输入 4 位有效密码"); m_hint->setStyleSheet(QString("color:%1;font-size:20px;").arg(C::TEXT_SUB)); } } void PasswordResetPage::onConfirm() { // 校验:4 位数字 static const QRegularExpression re("^\\d{4}$"); if (!re.match(m_value).hasMatch()) { m_hint->setText("密码须为 4 位数字"); m_hint->setStyleSheet(QString("color:%1;font-size:20px;font-weight:700;").arg(C::DANGER)); return; } ctx()->setPassword(m_value); clearInput(); ConfirmDialog::toast(this, "修改成功"); emit navigate(PageBoot); } void PasswordResetPage::onCancel() { clearInput(); emit navigate(PageBoot); } void PasswordResetPage::clearInput() { m_value.clear(); m_input->setText(m_value); m_hint->setText("请输入 4 位有效密码"); m_hint->setStyleSheet(QString("color:%1;font-size:20px;").arg(C::TEXT_SUB)); }