完成登录系统
main.h
#include "mainwindow.h"
#include "mean.h"
#include"newrega.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
mean me;
newrega reg;
w.show();
QObject::connect(&w,SIGNAL(jump_mean()),&me,SLOT(recvwdt()));
QObject::connect(&me,SIGNAL(jump_main()),&w,SLOT(recv()));
QObject::connect(&w,SIGNAL(jump_reg()),®,SLOT(recvreg()));
QObject::connect(®,SIGNAL(jump_main()),&w,SLOT(recv()));
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
this->setFixedSize(400,360);
ui->setupUi(this);
this->setWindowIcon(QIcon(":/new/prefix1/icon/1.png"));
// this->setStyleSheet("background-color:rgb(222,222,222)");
this->setWindowTitle("combat simulator");
// this->setWindowFlag(Qt::FramelessWindowHint);
//判断自己的数据库对象中,是否包含了要处理的数据库,如果没有包含则添加一个数据库,如果包含了,就可以打开了
if(!db.contains("mydatabase.db")){
//添加一个数据库,调用该类中的静态成员函数addDataBase
//函数原型: static QsqlDatabase addDatabase(const Qstring& type);
//参数: 驱动类型 QSQLITE SQLite version 3
//返回值: 数据库对象
db = QSqlDatabase::addDatabase("QSQLITE");
//设置数据库的名字
db.setDatabaseName("mydatabase.db");
}
//此时已经有一个名为mydatabase.db的数据库
//打开数据库
if(!db.open()){
QMessageBox::information(this,"失败","数据库打开失败");
return;
}
//需要使用sql语句进行创建表的操作
//准备sql语句
QString sql = "create table if not exists stu_info(" //创建表
"account varchar(16) primary key," //学号 主键
"password varchar(16))"; //姓名
//准备语句执行者
QSqlQuery querry;
//让语句执行者执行sql语句
//函数原型: bool exex(const QString& query);
//参数: 要执行的sql语句
//返回值:成功执行返回true,失败返回false
if(!querry.exec(sql)){
QMessageBox::information(this,"失败","创建失败");
return;
}
login = new QPushButton("login",this);
login->resize(80,30);
login->move(120,280);
login->setIcon(QIcon(":/new/prefix1/icon/2.png"));
connect(login,SIGNAL(clicked()),this,SLOT(on_login_clicked()));
reg = new QPushButton("register",this);
reg->resize(login->size());
reg->move(login->x()+120,login->y());
reg->setIcon(QIcon(":/new/prefix1/icon/reg.png"));
connect(reg,SIGNAL(clicked()),this,SLOT(on_reg_clicked()));
cancel = new QPushButton(this);
cancel->resize(40,40);
cancel->move(360,320);
cancel->setIcon(QIcon(":/new/prefix1/icon/cancel.png"));
connect(cancel,SIGNAL(clicked()),this,SLOT(on_cancel_clicked()));
account= new QLineEdit(this);
account->resize(200,30);
account->move(login->x(), login->y()-120);
account->setPlaceholderText("please enter your account");
password= new QLineEdit(this);
password->resize(200,30);
password->move(login->x(), login->y()-70);
password->setPlaceholderText("please enter your password");
title = new QLabel(this);
title->resize(400,150);
title->setPixmap(QPixmap(":/new/prefix2/title/title3.png"));
accounticon = new QLabel(this);
accounticon->resize(30,30);
accounticon->move(account->x()-40,account->y());
accounticon->setPixmap(QPixmap(":/new/prefix1/icon/lock.png"));
accounticon->setScaledContents(true);
accounticon->setScaledContents(true);
passwordicon = new QLabel(this);
passwordicon->resize(30,30);
passwordicon->move(password->x()-40,password->y());
passwordicon->setPixmap(QPixmap(":/new/prefix1/icon/lock glitch.png"));
passwordicon->setScaledContents(true);
passwordicon->setScaledContents(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_login_clicked(){
//准备sql语句
QString sql = "select * from stu_info";
//准备语句执行者
QSqlQuery querry;
//执行sql语句
if(!querry.exec(sql)){
QMessageBox::information(this,"提示","显示失败");
return ;
}
bool flag=0;
int i = 0; //记录行数
while(querry.next()){
//在该循环中,querry对象表示的是当前的记录
//可以使用成员函数: QSqlRecord record() const;获取当前记录
//可以使用QSqlRecord成员函数,count():获取当前记录中的项数
//可以是使用QsqlRecord成员函数,balue(index):获取当前记录中第index项的值
// querry.record().value(1).toString();
//遍历每条记录中的每一项的内容
for(int j=0;jtext() ==querry.record().value(j).toString() || password->text()==querry.record().value(j+1).toString()){
QMessageBox::information(this,"提示","登录成功",QMessageBox::Yes);
flag=1;
this->close();
emit jump_mean();
}
}
i++;
}
if(!flag){
QMessageBox::warning(this,"警告","账户或密码错误",
QMessageBox::Yes,QMessageBox::Yes);
}
}
void MainWindow::on_cancel_clicked(){
QMessageBox box(QMessageBox::Question,
"提示",
"是否退出?",
QMessageBox::Yes|QMessageBox::No,
this);
int ret= box.exec();
if(ret == QMessageBox::Yes){
close();
}
else if(ret == QMessageBox::No){
}
}
void MainWindow::on_reg_clicked(){
QMessageBox box(QMessageBox::Question,
"提示",
"是否前往注册 ?",
QMessageBox::Yes|QMessageBox::No,
this);
int ret= box.exec();
if(ret == QMessageBox::Yes){
this->close();
emit jump_reg();
}
else if(ret == QMessageBox::No){
}
}
void MainWindow::recv()
{
this->show();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include //数据库管理类
#include //执行sql语句的类
#include //数据库记录的类
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_login_clicked();
void on_cancel_clicked();
void on_reg_clicked();
void recv();
signals:
void jump_mean();
void jump_reg();
private:
Ui::MainWindow *ui;
QPushButton *login;
QPushButton *cancel;
QPushButton *reg;
QLineEdit *account;
QLineEdit *password;
QLabel *title;
QLabel *accounticon;
QLabel *passwordicon;
QSqlDatabase db;
};
#endif // MAINWINDOW_H
mean.cpp
#include "mean.h"
#include "ui_mean.h"
mean::mean(QWidget *parent) :
QWidget(parent),
ui(new Ui::mean)
{
ui->setupUi(this);
this->setFixedSize(400,360);
this->setWindowIcon(QIcon(":/new/prefix1/icon/1.png"));
// this->setStyleSheet("background-color:rgb(222,222,222)");
this->setWindowTitle("combat simulator");
logout = new QPushButton("logout",this);
logout->resize(80,30);
logout->move(120,280);
logout->setIcon(QIcon(":/new/prefix1/icon/cancel.png"));
connect(logout,SIGNAL(clicked()),this,SLOT(on_logout_clicked()));
}
mean::~mean()
{
delete ui;
}
void mean::recvwdt()
{
this->show();
}
void mean::on_logout_clicked(){
QMessageBox box(QMessageBox::Question,
"提示",
"是否前退出登录 ?",
QMessageBox::Yes|QMessageBox::No,
this);
int ret= box.exec();
if(ret == QMessageBox::Yes){
this->close();
emit jump_main();
}
else if(ret == QMessageBox::No){
}
}
mean.h
#ifndef MEAN_H
#define MEAN_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include //数据库管理类
#include //执行sql语句的类
#include //数据库记录的类
namespace Ui {
class mean;
}
class mean : public QWidget
{
Q_OBJECT
public:
explicit mean(QWidget *parent = nullptr);
~mean();
public slots:
void recvwdt();
void on_logout_clicked();
signals:
void jump_main();
private:
Ui::mean *ui;
QSqlDatabase db;
QPushButton *logout;
};
#endif // MEAN_H
newrega.cpp
#include "newrega.h"
#include "ui_newrega.h"
newrega::newrega(QWidget *parent) :
QWidget(parent),
ui(new Ui::newrega)
{
ui->setupUi(this);
this->setFixedSize(400,360);
this->setWindowIcon(QIcon(":/new/prefix1/icon/1.png"));
// this->setStyleSheet("background-color:rgb(222,222,222)");
this->setWindowTitle("combat simulator");
//判断自己的数据库对象中,是否包含了要处理的数据库,如果没有包含则添加一个数据库,如果包含了,就可以打开了
if(!db.contains("mydatabase.db")){
//添加一个数据库,调用该类中的静态成员函数addDataBase
//函数原型: static QsqlDatabase addDatabase(const Qstring& type);
//参数: 驱动类型 QSQLITE SQLite version 3
//返回值: 数据库对象
db = QSqlDatabase::addDatabase("QSQLITE");
//设置数据库的名字
db.setDatabaseName("mydatabase.db");
}
//此时已经有一个名为mydatabase.db的数据库
//打开数据库
if(!db.open()){
QMessageBox::information(this,"失败","数据库打开失败");
return;
}
//需要使用sql语句进行创建表的操作
//准备sql语句
QString sql = "create table if not exists stu_info(" //创建表
"account varchar(16) primary key," //学号 主键
"password varchar(16))"; //姓名
//准备语句执行者
QSqlQuery querry;
//让语句执行者执行sql语句
//函数原型: bool exex(const QString& query);
//参数: 要执行的sql语句
//返回值:成功执行返回true,失败返回false
if(!querry.exec(sql)){
QMessageBox::information(this,"失败","创建失败");
return;
}
confirm = new QPushButton("confirm",this);
confirm->resize(80,30);
confirm->move(120,280);
confirm->setIcon(QIcon(":/new/prefix1/icon/2.png"));
connect(confirm,SIGNAL(clicked()),this,SLOT(on_confirml_clicked()));
cancel = new QPushButton(this);
cancel->resize(40,40);
cancel->move(360,320);
cancel->setIcon(QIcon(":/new/prefix1/icon/cancel.png"));
connect(cancel,SIGNAL(clicked()),this,SLOT(on_cancel_clicked()));
account= new QLineEdit(this);
account->resize(200,30);
account->move(confirm->x(), confirm->y()-120);
account->setPlaceholderText("please enter your account");
password= new QLineEdit(this);
password->resize(200,30);
password->move(confirm->x(), confirm->y()-70);
password->setPlaceholderText("please enter your password");
title = new QLabel(this);
title->resize(400,150);
title->setPixmap(QPixmap(":/new/prefix2/title/regtitle.png"));
accounticon = new QLabel(this);
accounticon->resize(30,30);
accounticon->move(account->x()-40,account->y());
accounticon->setPixmap(QPixmap(":/new/prefix1/icon/lock.png"));
accounticon->setScaledContents(true);
accounticon->setScaledContents(true);
passwordicon = new QLabel(this);
passwordicon->resize(30,30);
passwordicon->move(password->x()-40,password->y());
passwordicon->setPixmap(QPixmap(":/new/prefix1/icon/lock glitch.png"));
passwordicon->setScaledContents(true);
passwordicon->setScaledContents(true);
}
newrega::~newrega()
{
delete ui;
}
void newrega::recvreg()
{
this->show();
}
void newrega::on_confirml_clicked(){
//获取ui界面要录入的数据
QString acc = account->text();
QString psd = password->text();
//要确保每个编辑器中都有数据
if(acc.isEmpty()==1 || psd.isEmpty()==1){
QMessageBox::information(this,"提示","请将信息填写完整");
return ;
}
//准备sql语句
QString sql = QString("insert into stu_info(account,password)"
"values(%1,%2)").arg(acc).arg(psd);
//准备语句执行者
QSqlQuery querry;
if(!querry.exec(sql)){
QMessageBox::information(this,"失败","注册失败");
}
else{
QMessageBox::information(this,"成功","注册成功");
}
}
void newrega::on_cancel_clicked(){
QMessageBox box(QMessageBox::Question,
"提示",
"是否前取消注册 ?",
QMessageBox::Yes|QMessageBox::No,
this);
int ret= box.exec();
if(ret == QMessageBox::Yes){
this->close();
emit jump_main();
}
else if(ret == QMessageBox::No){
}
}
newrega.h
#ifndef NEWREGA_H
#define NEWREGA_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include //数据库管理类
#include //执行sql语句的类
#include //数据库记录的类
namespace Ui {
class newrega;
}
class newrega : public QWidget
{
Q_OBJECT
public:
explicit newrega(QWidget *parent = nullptr);
~newrega();
public slots:
void recvreg();
void on_confirml_clicked();
void on_cancel_clicked();
signals:
void jump_main();
private:
Ui::newrega *ui;
QPushButton *confirm;
QPushButton *cancel;
QLineEdit *account;
QLineEdit *password;
QLabel *title;
QLabel *accounticon;
QLabel *passwordicon;
QSqlDatabase db;
};
#endif // NEWREGA_H