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

}

运行结果


思维导图

相关推荐
mit6.8246 分钟前
[Meetily后端框架] AI摘要结构化 | `SummaryResponse`模型 | Pydantic库 | vs marshmallow库
c++·人工智能·后端
screenCui10 分钟前
macOS运行python程序遇libiomp5.dylib库冲突错误解决方案
开发语言·python·macos
R-G-B12 分钟前
【02】MFC入门到精通——MFC 手动添加创建新的对话框模板
c++·mfc·mfc 手动添加创建新的对话框
linux kernel26 分钟前
第七讲:C++中的string类
开发语言·c++
Tipriest_40 分钟前
[数据结构与算法] 优先队列 | 最小堆 C++
c++·优先队列·数据结构与算法·最小堆
玩代码40 分钟前
Java线程池原理概述
java·开发语言·线程池
宛西南浪漫戈命1 小时前
Centos 7下使用C++使用Rdkafka库实现生产者消费者
c++·centos·linq
泰勒疯狂展开1 小时前
Java研学-MongoDB(三)
java·开发语言·mongodb
zzywxc7871 小时前
AI技术通过提示词工程(Prompt Engineering)正在深度重塑职场生态和行业格局,这种变革不仅体现在效率提升,更在重构人机协作模式。
java·大数据·开发语言·人工智能·spring·重构·prompt
高hongyuan1 小时前
Go语言教程-占位符及演示代码
开发语言·后端·golang