用Qt5做一个影音播放软件(3)——使用sqlite进行登陆注册

用Qt5做一个影音播放软件(3)——使用sqlite进行登陆注册

1.先看效果

在这里插入图片描述
在这里插入图片描述
登录注册的数据会存在这个db文件中可以下载(SQLite数据库查看工具DB Browser for SQLite)
附上大佬的链接

https://blog.csdn.net/CASTANEA/article/details/80356653

在这里插入图片描述

2.实现(看清楚复制哈有些东西不是这一块的)

首先在.pro文件中 导入sql库
在这里插入图片描述
然后在main文件中

#include "widget.h"
#include"reg.h"
#include"home.h"
#include <QApplication>
#include <QSqlDatabase>
#include<QDebug>
int main(int argc, char *argv[])
{
    //创建数据库
    QSqlDatabase db =QSqlDatabase::addDatabase("QSQLITE");
    //设置数据库名称
    db.setDatabaseName("pwd.db");

    //打开数据库
    if(db.open())
    {
         qDebug()<<"open suc";
    }else{
         qDebug()<<"open fail";
    }
    //创建一个数据库表
    QString cmd= "create table if not exists userinfo(username varchar(64),password varchar(64))";
    //执行sql语句
    QSqlQuery query;
    if(query.exec(cmd))
    {
        qDebug()<<"exec suc";

    }else{
        qDebug()<<"exec fail";

    }

    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();


}

之后在注册的reg.h文件中 包括QSqlDatabase;QSqlQuery;QSqlTableModel

#include<QDialog>
#include<QSqlDatabase>
#include<QDebug>
#include<QString>
#include<QSqlQuery>
#include<QMessageBox>
#include<QSqlTableModel>

namespace Ui {
class reg;
}

class reg : public QDialog
{
    Q_OBJECT

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

private slots:
    void on_pushButton_2_clicked();//取消

    void on_pushButton_clicked();//注册

private:
    Ui::reg *ui;
};

在.cpp文件中进行实现

void reg::on_pushButton_2_clicked()//取消按钮
{
    this->close();
}


void reg::on_pushButton_clicked()//注册按钮
{
    QString username=ui->userEdit->text();//获取两个edit中的字符串
    QString password=ui->passEdit->text();
    QString password1=ui->pwdEdit->text();
    if(password==password1)
    {
        QString cmd=QString("insert into userinfo values('%1','%2')")
                .arg(username) .arg(password);
        QSqlQuery query;
        if(query.exec(cmd))
        {
            QMessageBox::information(this,"注册提示","注册成功");
            this->close();

        }else{
            QMessageBox::information(this,"注册提示","注册失败");
        }
        this->close();
    }
    else{
        QMessageBox::information(this,"注册提示","两次密码输入不一致");
    }
}

之后在weight.h文件中

#include <QWidget>
#include <QSqlDatabase>
#include<QDebug>
#include<QString>
#include<QSqlQuery>
#include<QMessageBox>
#include<QSqlTableModel>
#include<QPoint>
#include<reg.h>
#include<home.h>
#include<showplay.h>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    bool bPressFlag;
    QPoint beginDrag;
private slots:

    void mousePressEvent(QMouseEvent *event);//鼠标按压事件捕捉

    void mouseReleaseEvent(QMouseEvent *event);//鼠标释放事件捕捉

    void mouseMoveEvent(QMouseEvent *event);//鼠标移动事件捕捉

    void on_pushButton_3_clicked();//关闭界面

    void on_reg_btn_clicked();//注册

    void on_login_btn_clicked();//登录

    void on_pushButton_2_clicked();//缩小

private:
    Ui::Widget *ui;
    reg *reg1=new reg;
    home *home1=new home;
};

然后进行实现

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //把窗口边框去除   这是去除原来的关闭边框
    setWindowFlags(Qt::FramelessWindowHint | windowFlags());
    //把窗口背景设置为透明;
    setAttribute(Qt::WA_TranslucentBackground);

}

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

void Widget::on_pushButton_3_clicked()
{
   this->close();
}


void Widget::on_reg_btn_clicked()
{
    ui->nameEdit->clear();
    ui->pwdEdit->clear();
    reg1->show();


}


void Widget::on_login_btn_clicked()
{
    QString username=ui->nameEdit->text();
    QString password=ui->pwdEdit->text();

    QSqlTableModel *model =new QSqlTableModel;
    model->setTable("userinfo");
    model->setFilter(QString("username='%1' and password='%2'")
                     .arg(username) .arg(password));
    model->select();
    //检测是否查询到数据
    int row =model->rowCount();
    if(row>0)
    {
         QMessageBox::information(this,"登录提示","登录成功");
         this->close();
         home1->show();
    }else
    {
        QMessageBox::information(this,"登录提示","登录失败");
    }
    delete model;


}

void Widget::on_pushButton_2_clicked()
{
    this->setWindowState(Qt::WindowMinimized);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
    bPressFlag = true;
    beginDrag = event->pos();
    QWidget::mousePressEvent(event);
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    bPressFlag = false;
    QWidget::mouseReleaseEvent(event);
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if(bPressFlag)
    {
        QPoint relaPos(QCursor::pos() - beginDrag);
        move(relaPos);
    }
    QWidget::mouseMoveEvent(event);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值