day52:QTday5,QT连接数据库

一、完善登录界面,将注册信息添加到数据库中,并与之前的注册登录界面结合

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QMessageBox>
#include "chat.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

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

signals:
    void jump();

public slots:
    void loginBtn_slot();
    void closeBtn_slot();
    void registeBtn_slot();
    void jump_slot();

private:
    Ui::Widget *ui;

    QPushButton *loginBtn;
    QPushButton *closeBtn;
    QPushButton *registeBtn;
    QLabel *backLab;
    QLabel *userLab;
    QLabel *passwdLab;
    QLineEdit *userEdit;
    QLineEdit *passwdEdit;

    QSqlDatabase db;

    chat *ccc;
};
#endif // WIDGET_H

widget.cpp:

#include "widget.h"
#include "ui_widget.h"

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

    this->setFixedSize(600,500);
    this->setWindowTitle("QQ");
    this->setWindowIcon(QIcon(":/new/prefix1/icon/qq.png"));

    backLab = new QLabel(this);
    backLab->resize(600,200);
    backLab->move(0,0);
    backLab->setPixmap(QPixmap(":/new/prefix1/icon/background.png"));
    backLab->setScaledContents(true);

    userLab = new QLabel(this);
    userLab->setPixmap(QPixmap(":/new/prefix1/icon/user.png"));
    userLab->resize(50,50);
    userLab->move(140,250);
    userLab->setScaledContents(true);

    userEdit = new QLineEdit(this);
    userEdit->resize(250,50);
    userEdit->move(userLab->x()+80,userLab->y());
    userEdit->setPlaceholderText("QQ/手机号/邮箱");

    passwdLab = new QLabel(this);
    passwdLab->setPixmap(QPixmap(":/new/prefix1/icon/passwd.png"));
    passwdLab->resize(50,50);
    passwdLab->move(userLab->x(),userLab->y()+80);
    passwdLab->setScaledContents(true);

    passwdEdit = new QLineEdit(this);
    passwdEdit->resize(250,50);
    passwdEdit->move(passwdLab->x()+80,passwdLab->y());
    passwdEdit->setPlaceholderText("密码");
    passwdEdit->setEchoMode(QLineEdit::Password);

    loginBtn = new QPushButton(QIcon(":/new/prefix1/icon/login.png"),"登录",this);
    loginBtn->resize(120,50);
    loginBtn->move(passwdLab->x()+40,passwdLab->y()+100);

    closeBtn = new QPushButton(QIcon(":/new/prefix1/icon/close.png"),"退出",this);
    closeBtn->resize(120,50);
    closeBtn->move(loginBtn->x()+200,loginBtn->y());

    registeBtn = new QPushButton("注册账号",this);
    registeBtn->resize(80,30);
    registeBtn->move(50,loginBtn->y()+10);


    if(!db.contains("clientmsg.db"))    //如果数据库不存在
    {
        db=QSqlDatabase::addDatabase("QSQLITE");

        db.setDatabaseName("clientmsg.db");
    }

    if(!db.open())  //如果数据库未打开
    {
        QMessageBox::information(this,"错误","打开数据库错误");
        return ;
    }

    QString sql=QString("create table if not exists cli_msg("
                        "name char primary key,passwd char,stat char);");
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"失败","创建表格失败");
        return ;
    }

    ccc = new chat;

    //连接登录按钮和对应槽函数
    connect(loginBtn,&QPushButton::clicked,this,&Widget::loginBtn_slot);
    //连接退出按钮和对应槽函数
    connect(closeBtn,&QPushButton::clicked,this,&Widget::closeBtn_slot);
    //连接注册按钮和对应槽函数
    connect(registeBtn,&QPushButton::clicked,this,&Widget::registeBtn_slot);
    //连接登录和跳转槽函数
    connect(this,&Widget::jump,this,&Widget::jump_slot);

}

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

void Widget::loginBtn_slot()
{
    //要确保每个编辑器中都有数据
    if(userEdit->text().isEmpty() || passwdEdit->text().isEmpty())
    {
        QMessageBox::information(this,"提示","请将信息填写完整");
        return;
    }
    QString sql=QString("select * from cli_msg where name = '%1'").arg(userEdit->text());

    QSqlQuery querry;
    querry.exec(sql);
    if(!querry.first())         //表示账号不正确
    {
        QMessageBox errbox;
        //登录失败,弹出错误对话框
        errbox.setText("账号不存在,是否重新登录");
        errbox.setWindowTitle("错误对话框");
        errbox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
        errbox.setDefaultButton(QMessageBox::Yes);
        int ret1=errbox.exec();
        if(ret1==QMessageBox::Yes)
        {
            //同意,隐藏错误对话框,清空账号密码
            errbox.hide();
            userEdit->clear();
            passwdEdit->clear();
        }else if(ret1==QMessageBox::Cancel)
        {
            //取消,关闭错误对话框
            errbox.close();
        }
    }
    else                        //表示账号正确
    {
        QString sql1=QString("select * from cli_msg where passwd = '%1'").arg(passwdEdit->text());
        querry.exec(sql1);
        if(querry.first())      //表示密码和账号都正确
        {
            int ret2=QMessageBox::information(this,"信息对话框","登录成功",QMessageBox::Ok,QMessageBox::Ok);
            if(ret2 == QMessageBox::Ok)
            {
                //同意登录,跳转
                QString sql2=QString("UPDATE cli_msg SET stat = '%1' WHERE name = '%2'").arg("YES").arg(userEdit->text());
                querry.exec(sql2);
                this->close();
                emit jump();
            }
        }
        else                    //账号正确,但是密码错误
        {
            QMessageBox errbox;
            //登录失败,弹出错误对话框
            errbox.setText("密码错误,是否重新登录");
            errbox.setWindowTitle("错误对话框");
            errbox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
            errbox.setDefaultButton(QMessageBox::Yes);
            int ret1=errbox.exec();
            if(ret1==QMessageBox::Yes)
            {
                //同意,隐藏错误对话框,清空账号密码
                errbox.hide();
                userEdit->clear();
                passwdEdit->clear();
            }else if(ret1==QMessageBox::Cancel)
            {
                //取消,关闭错误对话框
                errbox.close();
            }
        }
    }
}

void Widget::closeBtn_slot()
{
    QMessageBox quesbox(QMessageBox::Question,
                        "问题对话框",
                        "确定退出?",
                        QMessageBox::Yes|QMessageBox::No,
                        this);
    int ret = quesbox.exec();
    if(ret == QMessageBox::Yes)
    {
        quesbox.close();
        this->close();
    }else if(ret == QMessageBox::No)
    {
        quesbox.hide();
    }
}

void Widget::registeBtn_slot()
{
    //要确保每个编辑器中都有数据
    if(userEdit->text().isEmpty() || passwdEdit->text().isEmpty())
    {
        QMessageBox::information(this,"提示","请将信息填写完整");
        return;
    }

    QString sql=QString("insert into cli_msg values("
                        "'%1','%2','%3')").arg(userEdit->text()).arg(passwdEdit->text()).arg("No");
    QSqlQuery querry;
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"失败","用户注册失败");
        return ;
    }
    QMessageBox::information(this,"成功","用户注册成功");
}

void Widget::jump_slot()
{
    this->hide();
    ccc->show();
}

main.cpp:

#include "widget.h"

#include <QApplication>

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

二、思维导图: 有道云笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值