10.8作业

优化登录框:

当用户点击取消按钮,弹出问题对话框,询问是否要确定退出登录,并提供两个按钮,yes|No,如果用户点击的Yes,则关闭对话框,如果用户点击的No,则继续登录

当用户点击的登录按钮,进行账号和密码的匹配,如果匹配成功,则弹出信息对话框,给出信息为,登录成功,并给出一个确定按钮,当用户点击该按钮后,关闭登录界面,弹出另一个界面

当账号和密码不匹配是,给出错误对话框,给出信息为账号和密码不匹配,是否重新登录,并提供两个按钮 Yes|No,如果用户点击了Yes,则清空密码框后,继续登录。如果用户点击的取消,则关闭登录界面

要求 :静态成员函数版本和对象版本各至少实现一个

代码展示

widget.h
cpp 复制代码
#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
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();
    }

}

运行结果


思维导图

相关推荐
南猿北者1 分钟前
Cmake学习笔记
笔记·学习·策略模式
witkey_ak98967 分钟前
python 可迭代对象相关知识点
开发语言·python
sTone8737525 分钟前
android studio之外使用NDK编译生成android指定架构的动态库
android·c++
呼啦啦啦啦啦啦啦啦32 分钟前
synchronized锁,ReentrantLock 锁
开发语言·
diablobaal1 小时前
云计算学习100天-第26天
学习·云计算
听风的码1 小时前
Vue2封装Axios
开发语言·前端·javascript·vue.js
卷卷卷土重来1 小时前
C++单例模式
javascript·c++·单例模式
测试老哥2 小时前
pytest+requests+allure自动化测试接入Jenkins学习
自动化测试·软件测试·学习·测试工具·职场和发展·jenkins·pytest
yuyanjingtao2 小时前
CCF-GESP 等级考试 2025年6月认证C++二级真题解析
c++·青少年编程·gesp·csp-j/s
素界UI设计2 小时前
建筑行业变革:用Three.js构建BIM数据可视化孪生平台
开发语言·javascript·信息可视化