2023/09/21 day5 qt

将注册的账号密码存储到数据库中

登录的账号密码与数据库中的账号密码进行匹配

头文件

cpp 复制代码
#ifndef DENGLU_H
#define DENGLU_H
#include <QMainWindow>
#include <QDebug>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include "second.h"
#include <QMessageBox>  //消息对话框类
#include "third.h"
#include <QSqlDatabase>   //数据库管理类
#include <QSqlQuery>      //执行sql语句的类
#include <QSqlRecord>     //数据库记录的类

QT_BEGIN_NAMESPACE
namespace Ui { class denglu; }
QT_END_NAMESPACE

class denglu : public QMainWindow
{
    Q_OBJECT

public:
    denglu(QWidget *parent = nullptr);
    ~denglu();
signals:
    void jump();    //自定义跳转信号函数
    void jump1();
private slots:
   void b1_clicked();
   void b2_clicked();
   void b3_clicked();

private:
    Ui::denglu *ui;
    QLabel *lab1;
    QLabel *lab2;
    QLabel *lab3;
    QLineEdit *edit1;
    QLineEdit *edit2;
    QPushButton *b1;
    QPushButton *b2;
    QPushButton *b3;
    Second *s1;
    third *s2;

    QSqlDatabase db;
};
#endif // DENGLU_H
cpp 复制代码
#ifndef THIRD_H
#define THIRD_H

#include <QWidget>
#include <QMessageBox>
#include <QDebug>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSqlDatabase>   //数据库管理类
#include <QSqlQuery>      //执行sql语句的类
#include <QSqlRecord>     //数据库记录的类
namespace Ui {
class third;
}

class third : public QWidget
{
    Q_OBJECT

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

public slots:
    void jump_slot1();
private slots:
    void on_btn1_clicked();

private:
    Ui::third *ui;

    //声明一个数据库的类对象
    QSqlDatabase db;
};

#endif // THIRD_H

源文件

cpp 复制代码
#include "denglu.h"
#include "ui_denglu.h"
denglu::denglu(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::denglu)
{
    ui->setupUi(this);
    this->setFixedSize(430,360); //设置固定主界面尺寸
    this->setWindowTitle("Widget"); //设置窗口标题
    this->setWindowIcon(QIcon(":/Image/icon/wodepeizhenshi.png"));
    //实例化一个标签
    lab1 = new QLabel(this);
    lab1->resize(430,180); //设置占窗体一半尺寸
    lab1->setPixmap(QPixmap(":/Image/icon/jinx.bmp"));
    lab1->setScaledContents(1);
    //实例化第二个标签
    lab2 = new QLabel(this);
    lab2->resize(45,30);  //设置尺寸
    lab2->move(this->x()+65,this->y()+200); //移动位置
    lab2->setPixmap(QPixmap(":/Image/icon/userName.jpg"));//填充图片
    lab2->setScaledContents(1);
    //实例化第三个标签
    lab3 = new QLabel(this);
    lab3->resize(45,30);  //设置尺寸
    lab3->move(lab2->x(),lab2->y()+60); //移动位置
    lab3->setPixmap(QPixmap(":/Image/icon/passwd.jpg"));//填充图片
    lab3->setScaledContents(1);
    //实例化行编辑器1
    edit1 = new QLineEdit(this);
    edit1->resize(200,32); //设置尺寸
    edit1->move(lab2->x()+55,lab2->y()); //移动位置
    edit1->setPlaceholderText("admin"); //设置默认值,占位文本
    edit1->setMaxLength(20); //设置最大文本容量
    //实例化行编辑器2
    edit2 = new QLineEdit(this);
    edit2->resize(200,32); //设置尺寸
    edit2->move(lab3->x()+55,lab3->y()); //移动位置
    edit2->setEchoMode(QLineEdit::Password); //设置回显模式
    edit2->setMaxLength(20); //设置最大文本容量
    //实例化按钮1
    b1 = new QPushButton(QIcon(":/Image/icon/login.png"),"登录",this);
    b1->resize(80,32); //设置尺寸
    b1->move(this->x()+100,lab3->y()+50); //移动位置

    //实例化按钮2
    b2 = new QPushButton(QIcon(":/Image/icon/cancel.png"),"取消",this);
    b2->resize(80,32); //设置尺寸
    b2->move(this->x()+230,b1->y()); //移动位置


    //实例化按钮3
    b3 = new QPushButton(this);
    b3->resize(80,32); //设置尺寸
    b3->setText("注册");
    b3->move(edit1->x()+220,edit1->y()+40); //移动位置




    //将当前界面的信号,与登录消息对话框函数连接
    connect(b1,&QPushButton::clicked,this,&denglu::b1_clicked);
    //将当前界面的信号,与取消消息对话框函数连接
    connect(b2,&QPushButton::clicked,this,&denglu::b2_clicked);
    //将当前界面的信号,与取消消息对话框函数连接
    connect(b3,&QPushButton::clicked,this,&denglu::b3_clicked);


    //将当前界面的信号,与s1界面的槽函数进行连接
    s1 = new Second;
    connect(this,&denglu::jump,s1,&Second::jump_slot);

    s2 = new third;
    connect(this,&denglu::jump1,s2,&third::jump_slot1);
}

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


//登录按钮,如果账号密码和数据库中存储的账号密码一直就登录成功,否则登陆失败
void denglu::b1_clicked()
{

    //判断自己的数据库对象中是否包含了要处理的数据库,如果没有包含则添加一个数据库,
    //如果包含了,就可以打开了
    if(!db.contains("mydb1.db"))
    {
        db = QSqlDatabase::addDatabase("QSQLITE");
        //设置数据库名字
        db.setDatabaseName("mydb1.db");
    }
    //此时已经有了一个名为mydatabase的数据
    //打开数据库
    if(!db.open())
    {
        QMessageBox::information(this,"失败","打开数据库失败");
        return ;
    }

    //查找名为user_info的数据表的内容
    QString username = this->edit1->text();
    QString sql = QString("select * from user_info where user_id = '%1'").arg(username);
    //语句执行者
    QSqlQuery q;
    if(!q.exec(sql))
    {
        QMessageBox::information(this,"提示","显示失败");
        return ;
    }

    QString dbpassword;

    while(q.next())
    {

         dbpassword = q.value("password").toString();

    }
    if(this->edit1->text() == "" ||this->edit2->text()== "")
    {
        QMessageBox::information(this,"提示","用户名或密码不能为空");
        return ;
    }
    else if(this->edit2->text()==dbpassword)
    {
        //直接调用静态成员函数完成对话框的实现
        int ret = QMessageBox::information(this,
                                    "信息",
                                    "登录成功",
                                    QMessageBox::Ok);
        if(ret ==QMessageBox::Ok)
        {
            //关闭登录界面
            this->hide();
            //跳转到其他页面
            emit jump();
        }

    }
    else
    {
        QMessageBox box(QMessageBox::Critical,
                    "出错",
                    "账号密码不匹配,是否重新登录",
                    QMessageBox::Ok|QMessageBox::Cancel,
                    this);   //父组件
        box.setDefaultButton(QMessageBox::Ok); //设置默认

        //调用exec函数允许对话框
        int ret = box.exec();
        //对结果进行判断

            if(ret == QMessageBox::Ok)
            {
                this->edit2->clear();
            }
            else if(ret == QMessageBox::Cancel)
            {
                this->hide();
            }
    }

}

void denglu::b2_clicked()
{
    int ret = QMessageBox::information(this,
                                       "退出",
                                       "是否确定要退出登录",
                                       QMessageBox::Yes|QMessageBox::No);
    if(ret == QMessageBox::Yes)
    {
        this->close();
    }
    else if(ret == QMessageBox::No)
    {
            //不做操作
    }
}

void denglu::b3_clicked()
{
    emit jump1();
}

注册界面

cpp 复制代码
#include "third.h"
#include "ui_third.h"

third::third(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::third)
{
    ui->setupUi(this);
    //判断自己的数据库对象中是否包含了要处理的数据库,如果没有包含则添加一个数据库,
    //如果包含了,就可以打开了
    if(!db.contains("mydb1.db"))
    {
        db = QSqlDatabase::addDatabase("QSQLITE");
        //设置数据库名字
        db.setDatabaseName("mydb1.db");
    }
    //此时已经有了一个名为mydatabase的数据
    //打开数据库
    if(!db.open())
    {
        QMessageBox::information(this,"失败","打开数据库失败");
        return ;
    }

    //数据库已经打开,需要创建一个数据表
    //准备sql语句
    QString sql = "create table if not exists user_info(user_id varchar(20) primary key,password varchar(20))";

    //准备语句执行者
    QSqlQuery q;

    //调用QSqlQuery类下的成员函数exec执行sql语句
    if(!q.exec(sql))
    {
        QMessageBox::information(this,"失败","创建数据表失败");
        return ;
    }

}

third::~third()
{
    delete ui;
}
void third::jump_slot1()
{
    this->show();
}

//注册按钮对应的槽函数
void third::on_btn1_clicked()
{
        //获取ui界面中要录入的数据
        //账号
        QString user_id = ui->ledit1->text();
        //密码
        QString password = ui->ledit2->text();

        //判断要确保每个编辑器下都有数据
        if(user_id.isEmpty() || password.isEmpty())
        {
            QMessageBox::information(this,"提示","请将信息填写完整");
            return ;
        }

        //准备sql语句向表中添加数据
        QString sql = QString("insert into user_info(user_id,password)"
                              "values('%1','%2')").arg(user_id).arg(password);

        qDebug()<<sql;

        //准备语句执行者
        QSqlQuery querry;
        if(!querry.exec(sql))
        {
            QMessageBox::information(this,"失败","添加失败");

            return ;
        }
        else
        {
            QMessageBox::information(this,"成功","添加成功");
        }
}
相关推荐
落落落sss18 分钟前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
简单.is.good36 分钟前
【测试】接口测试与接口自动化
开发语言·python
Yvemil71 小时前
MQ 架构设计原理与消息中间件详解(二)
开发语言·后端·ruby
程序员是干活的1 小时前
私家车开车回家过节会发生什么事情
java·开发语言·软件构建·1024程序员节
我是陈泽1 小时前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
优雅的小武先生1 小时前
QT中的按钮控件和comboBox控件和spinBox控件无法点击的bug
开发语言·qt·bug
Death2001 小时前
使用Qt进行TCP和UDP网络编程
网络·c++·qt·tcp/ip
虽千万人 吾往矣1 小时前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
创作小达人2 小时前
家政服务|基于springBoot的家政服务平台设计与实现(附项目源码+论文+数据库)
开发语言·python
郭二哈2 小时前
C++——list
开发语言·c++·list