登录代码:
widget.h
#ifndef REGISTER_H
#define REGISTER_H#include <QWidget>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>namespace Ui {
class Register;
}class Register : public QWidget
{Q_OBJECTpublic:explicit Register(QWidget *parent = nullptr);~Register();public slots:void jump_slot(); //接收跳转信号的槽函数private slots:void on_registerbtn_clicked();private:Ui::Register *ui;};#endif // REGISTER_H
widget.cpp:
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);r = new Register;//连接登录注册按钮的信号与跳转函数QObject::connect(this, &Widget::jump_sign, r, &Register::jump_slot);
}Widget::~Widget()
{delete ui;
}void Widget::jumptoLogin_slot()
{this->show();
}//登录按钮的槽函数实现
void Widget::on_loginbtn_clicked()
{ // 初始化数据库连接QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");db.setDatabaseName("G:\\study\\Qt\\C++&Qt\\9.21\\work-Login\\mydatabase.db"); // 设置数据库文件的名称// 打开数据库if (!db.open()){QMessageBox::information(this, "失败", "数据库打开失败");}//从Edit中提取用户名和密码QString username = ui->usernameEdit->text();QString password = ui->passwordEdit->text();//准备sql语句QString sql = QString("select username, password from users where username='%1' and password=%2").arg(username).arg(password);qDebug()<<sql;//准备语句执行者QSqlQuery querry;//执行sql语句if(querry.exec(sql)){while(querry.next()){QString tempname = querry.value(0).toString();QString temppassword = querry.value(1).toString();if(username==tempname && password==temppassword){QMessageBox::information(this,"成功","登陆成功");return;}}QMessageBox::information(this,"失败","账号密码错误");}else{QMessageBox::information(this,"失败","查找错误,请检测账号密码格式");return;}
}
//注册按钮的槽函数实现
void Widget::on_registerbtn_clicked()
{this->hide(); //隐藏登陆界面emit jump_sign(); //发送跳转信号
}
register.h:
#ifndef REGISTER_H
#define REGISTER_H#include <QWidget>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>namespace Ui {
class Register;
}class Register : public QWidget
{Q_OBJECTpublic:explicit Register(QWidget *parent = nullptr);~Register();public slots:void jump_slot(); //接收跳转信号的槽函数private slots:void on_registerbtn_clicked();private:Ui::Register *ui;};#endif // REGISTER_H
register.cpp:
#include "register.h"
#include "ui_register.h"Register::Register(QWidget *parent) :QWidget(parent),ui(new Ui::Register)
{ui->setupUi(this);}Register::~Register()
{delete ui;
}void Register::jump_slot()
{this->show();
}void Register::on_registerbtn_clicked()
{// 初始化数据库连接QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");db.setDatabaseName("G:\\study\\Qt\\C++&Qt\\9.21\\work-Login\\mydatabase.db"); // 设置数据库文件的名称// 打开数据库if (db.open()){qDebug() << "已打开数据库";// 创建表QSqlQuery query;if (query.exec("CREATE TABLE if not exists users (id INTEGER PRIMARY KEY, username TEXT UNIQUE, password INTEGER)")) {qDebug() << "用户表已创建";//从Edit中提取用户名和密码QString username = ui->usernameEdit->text();QString password = ui->passwordEdit->text();//要确保每个编辑器中都有数据if(username.isEmpty() || password==0){QMessageBox::information(this,"提示","请将信息填写完整");return;}// 插入数据QString sql = QString("INSERT INTO users (username, password) VALUES ('%1', %2)").arg(username).arg(password);qDebug()<<sql;if (query.exec(sql)) {qDebug() << "数据插入成功 ";} else {QMessageBox::information(this, "失败", "数据有误");qDebug() << "数据插入失败";}} else {qDebug() << "用户表创建失败";}// 关闭数据库连接db.close();}else{QMessageBox::information(this, "失败", "打开失败");qDebug() << "数据库打开失败";}
}
思维导图: