qtqtqtqtqtqtqtqtqtqtqt
loginwidget.h
#include "LoginWidget.h"
LoginWidget::LoginWidget(QWidget *parent)
: QWidget(parent)
{
setupUI();
}
void LoginWidget::setupUI()
{
setWindowTitle("登录界面");
setFixedSize(300, 150);
QLabel *userLabel = new QLabel("账号:", this);
QLabel *passLabel = new QLabel("密码:", this);
m_userEdit = new QLineEdit(this);
m_passEdit = new QLineEdit(this);
m_passEdit->setEchoMode(QLineEdit::Password); // 密码显示模式
m_loginBtn = new QPushButton("登录", this);
m_cancelBtn = new QPushButton("取消", this);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QHBoxLayout *btnLayout = new QHBoxLayout;
QHBoxLayout *userLayout = new QHBoxLayout;
userLayout->addWidget(userLabel);
userLayout->addWidget(m_userEdit);
QHBoxLayout *passLayout = new QHBoxLayout;
passLayout->addWidget(passLabel);
passLayout->addWidget(m_passEdit);
btnLayout->addStretch();
btnLayout->addWidget(m_loginBtn);
btnLayout->addWidget(m_cancelBtn);
mainLayout->addLayout(userLayout);
mainLayout->addLayout(passLayout);
mainLayout->addLayout(btnLayout);
connect(m_loginBtn, &QPushButton::clicked, this, &LoginWidget::onLoginClicked);
connect(m_cancelBtn, &QPushButton::clicked, this, &LoginWidget::onCancelClicked);
}
void LoginWidget::onLoginClicked()
{
checkCredentials();
}
void LoginWidget::onCancelClicked()
{
close();
}
void LoginWidget::checkCredentials()
{
QString username = m_userEdit->text();
QString password = m_passEdit->text();
if(username == password && !username.isEmpty()) {
QMessageBox::information(this, "成功", "登录成功!");
emit jumpSignal();
close();
} else {
QMessageBox::warning(this, "失败", "账号或密码错误");
m_passEdit->clear();
m_passEdit->setFocus();
}
}