完善登录界面的注册登录功能
头文件1
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QMovie>
#include <QDebug>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
QT_BEGIN_NAMESPACE
namespace Ui {class MainWindow;}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
//void on_btn1_clicked();
void my_slot1();
void my_slot2();
void my_slot3();
private:
Ui::MainWindow *ui;
QLineEdit *edti1;
QLineEdit *edti2;
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
QLabel *lab1;
QLabel *lab2;
QLabel *lab3;
QSqlDatabase db; //定义一个数据库的类对象
signals:
//void btn1_signal();
void jump();
void jump2();
};
#endif // MAINWINDOW_H
.cpp1
#include "mainwindow.h"
#include <QDebug>
#include <iostream>
#include <QIcon>
#include <QMovie>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//界面
this->setFixedSize(800,600); //设置固定尺寸
this->setWindowTitle("摇篮游行"); //设置窗口标签
this->setWindowIcon(QIcon(":/pict/123.webp")); //设置窗口图标
//登录按钮
btn1 = new QPushButton(QIcon(":/pict/213.jpg"),"登录",this); //构造一个按钮
btn1->resize(100,50); //设置按钮大小
btn1->move(550,420); //移动按钮
//退出按钮
btn2 = new QPushButton(QIcon(":/pict/12345.jpg"),"退出",this); //构造一个按钮
btn2->resize(btn1->size());
btn2->move(550,520);
//注册按钮
btn3 = new QPushButton(QIcon(":/pict/1234567.jpg"),"注册",this);
btn3->resize(btn1->size());
btn3->move(680,420);
//行编辑器
edti1 = new QLineEdit(this); //构造一个行编辑器
edti1->setPlaceholderText("繁星凝望着海洋");
edti1->resize(300,50);
edti1->move(180,420);
edti2 = new QLineEdit(this); //构造一个行编辑器
edti2->setPlaceholderText("海洋拥抱着风帆");
edti2->resize(300,50);
edti2->move(180,520);
//按钮事件
connect(btn1,SIGNAL(clicked()),this,SLOT(my_slot1()));
connect(btn2,SIGNAL(clicked()),this,SLOT(my_slot2()));
connect(btn3,SIGNAL(clicked()),this,SLOT(my_slot3()));
//标签
lab1 = new QLabel(this);
lab1->resize(80,50);
lab1->setPixmap(QPixmap(":/pict/321.webp"));
lab1->setScaledContents(true);
lab1->move(100,420);
lab2 = new QLabel(this);
lab2->resize(80,50);
lab2->setPixmap(QPixmap(":/pict/4321.webp"));
lab2->setScaledContents(true);
lab2->move(100,520);
//gif
QMovie *movie = new QMovie(":/pict/6.gif");
lab3 = new QLabel(this);
lab3->resize(800,400);
lab3->setMovie(movie);
movie->start();
lab3->setScaledContents(true);
lab3->move(0,0);
//如果没有则创建数据库
if(!db.contains("lamia.db"))
{
//添加一个数据库
db = QSqlDatabase::addDatabase("QSQLITE");
//设置名字
db.setDatabaseName("lamia.db");
}
//打开数据库
if(!db.open())
{
QMessageBox::information(this,"失败","数据库打开失败");
return ;
}
}
//登录按钮
void MainWindow::my_slot1()
{
//sql语句
QString sql = "select * from user";
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"提示","信息查询失败");
return;
}
int flag=0;
while(querry.next())
{
QString user = querry.record().value(0).toString();
QString word = querry.record().value(1).toString();
if(this->edti1->text()==user&&this->edti2->text()==word)
{
QMessageBox::information(this,"登录成功","登录成功!");
emit jump();
flag=1;
this->close();
}
}
if(flag==0)
{
QMessageBox::critical(
this,tr("摇篮游行"),
tr("账号或密码不正确,是否重新登录"),
QMessageBox::Yes | QMessageBox::Cancel,
QMessageBox::Cancel
);
}
}
//退出按钮
void MainWindow::my_slot2()
{
int ret = QMessageBox::question(
this,tr("摇篮游行"),
tr("是否退出"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if(ret==QMessageBox::Yes)
{
this->close();
}
}
//注册按钮
void MainWindow::my_slot3()
{
emit jump2();
}
MainWindow::~MainWindow()
{
}
头文件2
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QMovie>
#include <QDebug>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
namespace Ui {
class second;
}
class second : public QWidget
{
Q_OBJECT
public slots:
void jump_slot();
public:
explicit second(QWidget *parent = nullptr);
~second();
private:
Ui::second *ui;
QLabel *lab1;
};
#endif // SECOND_H
.cpp2
#include "second.h"
#include "ui_second.h"
second::second(QWidget *parent) :
QWidget(parent),
ui(new Ui::second)
{
ui->setupUi(this);
this->setFixedSize(800,600); //设置固定尺寸
this->setWindowTitle("摇篮游行"); //设置窗口标签
this->setWindowIcon(QIcon(":/pict/123.webp")); //设置窗口图标
lab1 = new QLabel(this);
lab1->resize(800,600);
lab1->setText("施工中");
lab1->setAlignment(Qt::AlignCenter);
lab1->move(0,0);
}
second::~second()
{
delete ui;
}
void second::jump_slot()
{
this->show();
}
头文件3
#ifndef THIRD_H
#define THIRD_H
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QMovie>
#include <QDebug>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
namespace Ui {
class third;
}
class third : public QWidget
{
Q_OBJECT
public:
explicit third(QWidget *parent = nullptr);
QLineEdit *edti1;
QLineEdit *edti2;
~third();
public slots:
void jump_slot();
void my_slot1();
void my_slot2();
private:
//定义ui界面
Ui::third *ui;
QPushButton *btn2;
QPushButton *btn3;
QLabel *lab1;
QSqlDatabase db; //定义一个数据库的类对象
};
#endif // THIRD_H
.cpp3
#include "third.h"
#include "ui_third.h"
third::third(QWidget *parent) :
QWidget(parent),
ui(new Ui::third)
{
ui->setupUi(this);
//界面
this->setFixedSize(450,400); //设置固定尺寸
this->setWindowTitle("账号注册"); //设置窗口标签
this->setWindowIcon(QIcon(":/pict/1234567.jpg")); //设置窗口图标
//注册按钮
btn3 = new QPushButton(QIcon(":/pict/4321.png"),"注册",this);
btn3->resize(100,50);
btn3->move(300,270);
//退出按钮
btn2 = new QPushButton(QIcon(":/pict/123123.png"),"退出",this);
btn2->resize(100,50);
btn2->move(300,340);
//标签
lab1 = new QLabel(this);
lab1->resize(500,250);
lab1->setPixmap(QPixmap(":/pict/54321.jpg"));
lab1->setScaledContents(false);
lab1->move(0,0);
//账号密码栏
edti1 = new QLineEdit(this); //构造一个行编辑器
edti1->setPlaceholderText("注册账号");
edti1->resize(200,50);
edti1->move(20,270);
edti2 = new QLineEdit(this); //构造一个行编辑器
edti2->setPlaceholderText("注册密码");
edti2->resize(200,50);
edti2->move(20,340);
connect(btn3,SIGNAL(clicked()),this,SLOT(my_slot1()));
connect(btn2,SIGNAL(clicked()),this,SLOT(my_slot2()));
//如果没有则创建数据库
if(!db.contains("lamia.db"))
{
//添加一个数据库
db = QSqlDatabase::addDatabase("QSQLITE");
//设置名字
db.setDatabaseName("lamia.db");
}
//打开数据库
if(!db.open())
{
QMessageBox::information(this,"失败","数据库打开失败");
return ;
}
//创建表
QString sql = "create table if not exists user("
"username varchar(20),"
"password varchar(20))";
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","创建表失败");
return ;
}
}
third::~third()
{
delete ui;
}
void third::jump_slot()
{
this->show();
}
void third::my_slot2()
{
int ret = QMessageBox::question(
this,tr("退出提示"),
tr("是否退出"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if(ret==QMessageBox::Yes)
{
this->close();
}
}
void third::my_slot1()
{
//获取输入栏中的内容
QString user = this->edti1->text();
QString password = this->edti2->text();
//确保账号密码都被输入
if(user.isEmpty()||password.isEmpty())
{
QMessageBox::information(this,"提示","请将信息填写完整");
return ;
}
//遍历查看账号是否以注册
//sql语句
QString sql = "select * from user";
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"提示","账号查询失败");
return;
}
int flag=0;
QString olduser="";
while(querry.next())
{
olduser = querry.record().value(0).toString();
if(olduser==user)
{
QMessageBox::warning(this,"错误","改账号已注册请重新输入");
this->edti1->clear();
this->edti2->clear();
flag=1;
}
}
//sql语句录入信息
if(flag==0)
{
sql = QString("insert into user(username,password)"
"values('%1','%2')").arg(user).arg(password);
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","注册失败");
return;
}else
{
QMessageBox::information(this,"成功","注册成功");
this->edti1->clear();
this->edti2->clear();
}
this->close();
}
}