当前位置: 首页 > article >正文

QT 优化登录框

作业

优化登录框:

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

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

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

login.h代码

#ifndef LOGIN_H
#define LOGIN_H

#include <QWidget>

namespace Ui {
class Login;
}

class Login : public QWidget
{
    Q_OBJECT

public:
    explicit Login(QWidget *parent = nullptr);
    ~Login();

public slots:
    void jump_slot()   //用于接收跳转的槽函数
    {
        this->show();
    }

private:
    Ui::Login *ui;
};

#endif // LOGIN_H

widget.h代码

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QMovie>
#include<QString>
#include <QMessageBox>


class Widget : public QWidget
{
    Q_OBJECT


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

signals:

    void jump();

public slots:
    void login_clicked();
    void on_questIconbtn_clicked();

private:

    QPushButton *btn1;
    QPushButton *btn2;
    QLineEdit *edit1;
    QLineEdit *edit2;
    QLabel *lab1;
    QLabel *lab2;
    QLabel *lab3;
    QLabel *lab4;
    QMovie *movie;
};
#endif // WIDGET_H

login.cpp代码

#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
}

Login::~Login()
{
    delete ui;
}

main.cpp代码

#include "widget.h"
#include "login.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    Login s;

    QObject::connect(&w,&Widget::jump,&s,&Login::jump_slot);

    return a.exec();
}

widget.cpp代码

#include "widget.h"
#include <QtDebug>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //1、设置窗口
    this->setFixedSize(600,400);    //设置界面尺寸
    this->setWindowTitle("QQ");    //设置窗口标题
    //设置图标,使用匿名对象完成
    this->setWindowIcon(QIcon(":/icon/QQ.png"));

    //2、设置按钮
    //使用无参构造在堆区申请一个按钮
    this->btn1 = new QPushButton(this);
    btn1->setParent(this);  //将当前界面设置成组件的父组件
    btn1->setText("登录");    //设置按钮文本内容
    btn1->resize(80,40);    //重新设置按钮尺寸
    btn1->move(180,320);    //移动当前组件位置
    //设置样式表
    btn1->setStyleSheet("color:white;background-color:skyblue;border-radius:10px;");




    //使用有参构造,构造一个按钮,实例化对象时,顺便给定父组件
    this->btn2 = new QPushButton(this);
    btn2->setText("取消");
    //使用btn1的尺寸设置当前按钮的尺寸
    btn2->resize(btn1->size());
    //使用btn1的位置,确定btn2的位置
    btn2->move(btn1->x()+btn1->width()+10,btn1->y());
    //设置样式表
    btn2->setStyleSheet("color:white;background-color:skyblue;border-radius:10px;");

    //3、设置行编辑器
    //使用有参构造一个行编辑器
    this->edit1 = new QLineEdit(this);
    edit1->resize(250,40);  //重置尺寸
    edit1->move(150,160);
    edit1->setAlignment(Qt::AlignCenter);   //设置文本对齐方式

    edit1->setPlaceholderText("请输入QQ账号");    //设置占位文本

    //使用有参构造一个行编辑器
    this->edit2 = new QLineEdit(this);
    edit2->resize(edit1->size());   //重置尺寸
    edit2->move(150,220);  //移动位置
    edit2->setPlaceholderText("请输入QQ密码");    //设置占位文本
    edit2->setAlignment(Qt::AlignCenter);   //设置文本对齐方式
    edit2->setEchoMode(QLineEdit::Password);    //设置回显模式

    //4、设置标签
    this->lab1 = new QLabel(this);
    lab1->setText("账号");
    lab1->move(100,170);

    this->lab2 = new QLabel(this);
    lab2->setText("密码");
    lab2->move(100,230);

    this->lab3 = new QLabel(this);
    //lab3->setStyleSheet("background-color:skyblue;");
    lab3->move(0,0);
    lab3->setFixedSize(600,140);
    //给标签设置动图
    //创建一个movie对象
    this->movie = new QMovie(":/icon/bj.gif");
    //将动图对象放入到标签中
    lab3->setMovie(movie);
    //让动图动起来
    movie->start();
    //让标签内容自适应大小
    lab3->setScaledContents(true);

    //给标签设置静态图
    this->lab4 = new QLabel(this);
    lab4->resize(60,60);
    lab4->move(260,80);
    lab4->setPixmap(QPixmap(":/icon/tx.jpg"));
    lab4->setScaledContents(true);

    connect(btn1, &QPushButton::clicked, this, &Widget::login_clicked);
    connect(btn2, &QPushButton::clicked, this, &Widget::on_questIconbtn_clicked);

}

Widget::~Widget()
{
}
/*
void Widget::login_clicked()
{

    this->close();
    emit jump();
}
*/

//按钮1对应的槽函数
void Widget::login_clicked()
{
    if(edit1->text() == edit2->text())
    {

        QMessageBox box(QMessageBox::Information,  //图标,问题图标
                        "登录",   //对话框标题
                        "登录成功",    //对话框文本内容
                        QMessageBox::Ok,  //对话框提供的按钮
                        this);  //父组件

         box.exec();
        //2、对用户点击的按钮进行判断

        this->close();
        emit jump();

    }
    else
    {
        //1、实例化一个消息对话框的对象
        QMessageBox box(QMessageBox::Warning,  //图标,问题图标
                        "警告",   //对话框标题
                        "账号和密码不匹配,是否重新登录?",    //对话框文本内容
                        QMessageBox::Ok | QMessageBox::No,  //对话框提供的按钮
                        this);  //父组件
        //2、调用函数设置相关属性
        box.setButtonText(QMessageBox::Ok,"Yes");
        box.setDefaultButton(QMessageBox::No);

        //3、显示对话框 并返回用户点击的那个按钮值
        int btn = box.exec();

        //4、判断用户点击的按钮值
        if(btn == QMessageBox::Ok)
        {
            edit2->clear();
        }else if(btn == QMessageBox::No)
        {
            this->close();
        }
        edit2->clear();
    }
}



//取消按钮对应的槽函数
void Widget::on_questIconbtn_clicked()
{
    //1、实例化一个消息对话框的对象
    QMessageBox box(QMessageBox::Question,  //图标,问题图标
                    "问题",   //对话框标题
                    "是否要确定退出登录?",    //对话框文本内容
                    QMessageBox::Ok | QMessageBox::No,  //对话框提供的按钮
                    this);  //父组件
    //2、调用函数设置相关属性
    box.setButtonText(QMessageBox::Ok,"yes");
    box.setDefaultButton(QMessageBox::No);

    //3、显示对话框 并返回用户点击的那个按钮值
    int btn = box.exec();

    //4、判断用户点击的按钮值
    if(btn == QMessageBox::Ok)
    {
        this->close();
    }else if(btn == QMessageBox::No)
    {
        qDebug() << "继续登录";
    }
}

运行效果:

点击取消按钮效果

点击No效果

输入账号密码不一致

点击Yes按钮

输入账号密码一致

点击OK按钮

知识梳理

对话框资源


http://www.kler.cn/news/339317.html

相关文章:

  • C++ day03(作用域限定符、this、static、const)
  • 【Vue】Vue 快速教程
  • 多级代理与提权维权
  • 第Y2周:训练自己的数据集
  • ​​​​​​​如何使用Immersity AI将图片转换成3D效果视频
  • 如何通过视觉分析检测车辆逆行行为
  • 构建MySQL健康检查Web应用
  • Java-数据结构-Lambda表达式 (✪ω✪)
  • 深度图详解
  • 带隙基准Bandgap电路学习(一)
  • 职场上的人情世故,你知多少?这五点一定要了解
  • D31【python 接口自动化学习】- python基础之输入输出与文件操作
  • 五分钟极简带你快速入门若依框架实现二次开发
  • C++基础面试题 | C++中野指针和悬挂指针的区别?
  • paimon,基础查询语句测试
  • 力扣189.轮转数组
  • 如何录制微课教程?K12教育相关课程录制录屏软件推荐
  • 类与对象、封装、继承、多态
  • 性能学习5:性能测试的流程
  • 计算机组成原理:物理层 —— 编码与调制