优化登录框:

当用户点击取消按钮,弹出问题对话框,询问是否要确定退出登录,并提供两个按钮,yes|No,如果用户点击的Yes,则关闭对话框,如果用户点击的No,则继续登录
当用户点击的登录按钮,进行账号和密码的匹配,如果匹配成功,则弹出信息对话框,给出信息为,登录成功,并给出一个确定按钮,当用户点击该按钮后,关闭登录界面,弹出另一个界面
当账号和密码不匹配是,给出错误对话框,给出信息为账号和密码不匹配,是否重新登录,并提供两个按钮 Yes|No,如果用户点击了Yes,则清空密码框后,继续登录。如果用户点击的取消,则关闭登录界面
要求 :静态成员函数版本和对象版本各至少实现一个

代码展示

widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QRadioButton>
#include <QMessageBox>
#include <QIcon>
#include <QLabel>
#include <QDebug>
#include <QPixmap>
#include <QPainter>
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

signals:
    void my_signal();
    void jump();
public slots:
    void my_slot();
    void cancel_click();
private:
    QLineEdit *edit1; // 账号编辑器
    QLineEdit *edit2; // 密码编辑器

};
#endif // WIDGET_H

widget.cpp
#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle("QQ 登录");
    this->setFixedSize(480,672); // 设置界面尺寸
    this->setStyleSheet("background-color:#271938");
    // 头像图标
    QLabel *iconLabel = new QLabel(this);
    iconLabel->resize(120,120);
    iconLabel->setPixmap(QPixmap(":/picture/QQ.png"));
    iconLabel->setScaledContents(true);
    iconLabel->move(178, 100); // 设置头像位置
    //构造账号编辑器
    edit1 = new QLineEdit(this);
    edit1->resize(384,64);
    edit1->setStyleSheet("background-color:#37335b;color:#a8a2ae;font-size:25px;border-radius:10px");
    edit1->setAlignment(Qt::AlignCenter);
    edit1->setPlaceholderText("手机号/QQ号/邮箱");
    edit1->move(48,251);
    //密码编辑器
    edit2 = new QLineEdit(this);
    edit2->resize(384,64);
    edit2->setStyleSheet("background-color:#37335b;color:#a8a2ae;font-size:25px;border-radius:10px");
    edit2->setAlignment(Qt::AlignCenter);
    edit2->setEchoMode(QLineEdit::Password);
    edit2->setPlaceholderText("请输入QQ密码");
    edit2->move(48,edit1->y()+85);
    //单选框
    QRadioButton *circleRadioButton = new QRadioButton(this);
    circleRadioButton->setText("已阅读并同意服务协议和QQ隐私保护指引");
    circleRadioButton->setStyleSheet("color: #868285; font-size: 18px;");
    circleRadioButton->move(48, edit2->y() + 85);
    circleRadioButton->setChecked(false); // 默认未选中
    //登录框
    QPushButton *loginButton = new QPushButton("登录",this);
    loginButton->setStyleSheet("background-color: #23335a;color:#685e75;font-size: 22px; border-radius: 10px;");
    loginButton->resize(180, 57);
    loginButton->move(48,circleRadioButton->y()+40);
    connect(loginButton,&QPushButton::clicked,this,&Widget::my_slot);

    //取消框
    QPushButton *cancelButton = new QPushButton("取消",this);
    cancelButton->setStyleSheet("background-color: #23335a;color:#685e75;font-size: 22px; border-radius: 10px;");
    cancelButton->resize(180, 57);
    cancelButton->move(loginButton->x()+200,circleRadioButton->y()+40);
    connect(cancelButton,&QPushButton::clicked,this,&Widget::cancel_click);
    //connect(cancelButton,SIGNAL(clicked()),this,SLOT(close()));
    // 超链接标签
    QLabel *linkLabel = new QLabel(this);
    linkLabel->setText("扫码登录 | 更多选项");
    linkLabel->setOpenExternalLinks(true); // 允许打开外部链接
    linkLabel->setStyleSheet("color: #2c71da; font-size: 20px;"); // 设置字体颜色
    linkLabel->move(141, loginButton->y()+ 120); // 设置位置

}

Widget::~Widget()
{

}


void Widget::my_slot() {
    QString username = edit1->text(); // 获取账号输入
    QString password = edit2->text(); // 获取密码输入

    // 判断输入内容
    if (username.isEmpty() || password.isEmpty()) {
        // 提示用户输入不能为空
        QMessageBox::warning(this, "输入错误", "账号或密码不能为空!");
    } else if(username == password){
        // 这里可以添加更多的判断逻辑,例如验证账号和密码?
        QMessageBox::information(this, "登录成功", "欢迎," + username + "!登录成功!");
        this->close();
        emit jump();
    } else if(username != password){
        int btn = QMessageBox::warning(this, "输入错误", "账号或密码不正确!",
                             QMessageBox::Yes|QMessageBox::No);
         if(btn == QMessageBox::No){
             this->close();
         }else if(btn == QMessageBox::Yes){
             edit2->clear();
         }

    }

}

void Widget::cancel_click()
{
    QMessageBox box(QMessageBox::Question,
                    "退出",
                    "是否要确定退出登录",
                    QMessageBox::Ok|QMessageBox::No,
                    this);
    box.setButtonText(QMessageBox::Ok,"确定");
    box.setButtonText(QMessageBox::No,"取消");
    box.setDefaultButton(QMessageBox::No);

    int btn = box.exec();
    if(btn == QMessageBox::Ok){
        this->close();
    }

}

运行结果

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

思维导图

在这里插入图片描述

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部