登录代码:
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_OBJECT
public:
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_OBJECT
public:
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() << "数据库打开失败";
}
}
思维导图: