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();
    }

}

运行结果


思维导图

相关推荐
XFF不秃头4 小时前
力扣刷题笔记-三数之和
c++·笔记·算法·leetcode
滴滴滴嘟嘟嘟.4 小时前
Qt信号与槽机制
开发语言·qt
快起来搬砖了4 小时前
实现一个优雅的城市选择器组件 - Uniapp实战
开发语言·javascript·uni-app
Pafey4 小时前
VS2022 + Qt5.9 中文乱码/项目设置utf-8编码
c++·qt·中文乱码
wu~9704 小时前
开发思路篇:转账接口设计
java·开发语言
带娃的IT创业者4 小时前
实战:用 Python 搭建 MCP 服务 —— 模型上下文协议(Model Context Protocol)应用指南
开发语言·python·mcp
minji...4 小时前
C++ STL之list的使用
开发语言·c++
摘星星的屋顶4 小时前
论文阅读记录之《VelocityGPT 》
论文阅读·人工智能·深度学习·学习
万粉变现经纪人4 小时前
如何解决pip安装报错ModuleNotFoundError: No module named ‘python-dateutil’问题
开发语言·ide·python·pycharm·pandas·pip·httpx
Sammyyyyy4 小时前
macOS是开发的终极进化版吗?
开发语言·macos·开发工具