1、登录界面
头文件
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include
- #include
- #include
- #include "second.h" //第二个界面头文件
- #include "third.h" //注册界面头文件
-
- #include
//数据库管理类 - #include
//执行sql语句的类 - #include
//数据库记录的类 - #include
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
-
- signals:
- void jump(); //自定义跳转信号函数
-
- void jump2(); //自定义跳转信号函数
-
- public:
- void jump_slot3(); //自定义跳转函数(注册页面)
-
- private slots:
-
- void on_pushButton_clicked();
-
- void on_pushButton_2_clicked();
-
- void on_pushButton_3_clicked();
-
- private:
- Ui::MainWindow *ui;
-
- Second *s1; //定义另一个界面的指针
-
- third *t1; //定义注册页面的指针
-
- QSqlDatabase db; //定义一个数据库的类
- };
- #endif // MAINWINDOW_H
源文件
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
-
- ui->label->setPixmap(QPixmap("D:\\QTtupian\\jimoaqing.gif"));
- ui->label->setScaledContents(true);
-
- //账号图标
- ui->label_2->setPixmap(QPixmap("D:\\QTtupian\\zhanghao.jpg"));
- ui->label_2->setScaledContents(true);
- //密码图标
- ui->label_3->setPixmap(QPixmap("D:\\QTtupian\\mima.jpg"));
- ui->label_3->setScaledContents(true);
-
- s1 = new Second; //给另一个界面实例化空间
- t1 = new third; //给注册页面实例化空间
-
- //将当前界面的信号,与s1界面的槽函数进行连接
- connect(this,&MainWindow::jump, s1, &Second::jump_slot); //连接聊天室
- connect(this,&MainWindow::jump2, t1, &third::jump_slot2); //连接注册
- connect(t1,&third::jump3, this, &MainWindow::jump_slot3); //从注册跳回
- /********************************************************/
-
- //判断自己的数据库对象中,是否包含了要处理的数据库
- if(!db.contains("zhanghu.db"))
- {
- db = QSqlDatabase::addDatabase("QSQLITE"); //参数为驱动类型
-
- //设置数据库的名字
- db.setDatabaseName("zhanghu.db");
- }
- //此时已经有一个名为zhanghu.db的数据库
-
- //打开数据库
- if(!db.open())
- {
- QMessageBox::information(this,"失败","数据库打开失败");
- return ;
- }
- //zhanghu_info
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
- //注册按钮对应槽函数
- void MainWindow::on_pushButton_3_clicked()
- {
- emit jump2(); //发送跳转函数
-
- this->hide(); //将当前页面隐藏
- }
-
- //自定义跳转函数
- void MainWindow::jump_slot3()
- {
- this->show();
- }
-
- //登录按钮对应槽函数
- void MainWindow::on_pushButton_clicked()
- {
- //准备sql语句
- QString sql = "select * from zhanghu_info";
-
- //准备语句执行者
- QSqlQuery querry;
-
- //执行sql语句
- if(!querry.exec(sql))
- {
- QMessageBox::information(this,"提示","显示失败");
- return ;
- }
-
- int i=0; //记录行数
- QString Z; //记录账号
- QString M; //记录密码
- while(querry.next())
- {
- //遍历每条记录中的每一项内容
- for(int j=0;j<querry.record().count();j++) //其中count为每一行数据的个数
- {
-
- if(ui->lineEdit->text()==querry.record().value(j).toString()&&
- ui->lineEdit_2->text()==querry.record().value(j+1).toString())
- {
- Z = querry.record().value(j).toString();
- M = querry.record().value(j+1).toString();
- }
- }
-
- i++;
- }
-
- //判断输入的账号密码是否与数据库表中已注册的账号密码一致
- if(ui->lineEdit->text()==Z&&ui->lineEdit_2->text()==M)
- {
- //信息对话框
- QMessageBox box(QMessageBox::Question,"成功","登录成功",
- QMessageBox::Ok,this);
- box.setDefaultButton(QMessageBox::Ok); //默认到ok上
-
- int ret = box.exec(); //运行对话框
- if(ret == QMessageBox::Ok)
- {
- emit jump(); //发送跳转信息
-
- this->hide(); //将当前界面隐藏
- }
- }
- else
- {
- //错误对话框
- QMessageBox box(QMessageBox::Question,"错误","账号密码不匹配,是否重新登录",
- QMessageBox::Ok|QMessageBox::Cancel,this);
- box.setDefaultButton(QMessageBox::Ok); //默认到ok上
- int ret = box.exec(); //运行对话框
- switch(ret)
- {
- case QMessageBox::Ok:
- ui->lineEdit->clear(); //清楚账号密码内容
- ui->lineEdit_2->clear();
- break;
- case QMessageBox::Cancel:
- //ret=box.close();
- break;
- }
- }
-
- }
-
- //退出按钮对应槽函数
- void MainWindow::on_pushButton_2_clicked()
- {
- //问题对话框
- int ret = QMessageBox::question(this,"问题","是否确定要退出登录",
- QMessageBox::Yes|QMessageBox::No,
- QMessageBox::No);
-
- switch(ret)
- {
- case QMessageBox::Yes:
- this->close(); //关闭窗口
- break;
- case QMessageBox::No:
- break;
- }
- }
主函数
- #include "mainwindow.h"
-
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
2、注册窗口
头文件
- #ifndef THIRD_H
- #define THIRD_H
-
- #include
- #include
- #include
- #include
//数据库管理类 - #include
//执行sql语句的类 - #include
//数据库记录的类 - #include
-
-
-
- namespace Ui {
- class third;
- }
-
- class third : public QDialog
- {
- Q_OBJECT
-
- public:
- explicit third(QWidget *parent = nullptr);
- ~third();
-
- signals:
- void jump3(); //自定义跳转信号函数
-
- public:
- void jump_slot2(); //接收跳转信号的槽函数
-
- private slots:
- void on_pushButton_clicked();
-
- void on_pushButton_2_clicked();
-
- private:
- Ui::third *ui;
-
- QSqlDatabase db;
-
- };
-
- #endif // THIRD_H
源文件
- #include "third.h"
- #include "ui_third.h"
-
- third::third(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::third)
- {
- ui->setupUi(this);
-
- ui->label->setPixmap(QPixmap("D:\\QTtupian\\jimoaqing.gif"));
- ui->label->setScaledContents(true);
- //账号图标
- ui->label_2->setPixmap(QPixmap("D:\\QTtupian\\zhanghao.jpg"));
- ui->label_2->setScaledContents(true);
- //密码图标
- ui->label_3->setPixmap(QPixmap("D:\\QTtupian\\mima.jpg"));
- ui->label_3->setScaledContents(true);
-
-
- /**********************************************************/
-
- //判断自己的数据库对象中,是否包含了要处理的数据库
- if(!db.contains("zhanghu.db"))
- {
- db = QSqlDatabase::addDatabase("QSQLITE"); //参数为驱动类型
-
- //设置数据库的名字
- db.setDatabaseName("zhanghu.db");
- }
-
- //此时已经有一个名为zhanghu.db的数据库
- //打开数据库
- if(!db.open())
- {
- QMessageBox::information(this,"失败","数据库打开失败");
- return ;
- }
-
- //创建一个表
- //准备sql语句
- QString sql="create table if not exists zhanghu_info("
- "zhanghao varchar(30),mima varchar(30))";
-
- //准备语句执行者
- QSqlQuery querry;
-
- //执行者执行sql语句
- if(!querry.exec(sql)) //注意要把sql语句放进函数中
- {
- QMessageBox::information(this,"失败","创建表失败");
- return ;
- }
-
- }
-
- third::~third()
- {
- delete ui;
- }
-
- //接收跳转信号对应的槽函数
- void third::jump_slot2()
- {
- this->show(); //将自己界面进行展示
- }
-
-
- //账户注册槽函数
- void third::on_pushButton_clicked()
- {
-
- //获取ui界面输入的账号和密码
- QString zhanghao = ui->lineEdit->text();
- QString mima = ui->lineEdit_2->text();
-
- //确保每个编辑器都有数据
- if(zhanghao.isEmpty()||mima.isEmpty())
- {
- QMessageBox::information(this,"提示","请将信息填写完整");
- return ;
- }
-
- //准备sql语句
- QString sql = "select * from zhanghu_info";
-
- //准备语句执行者
- QSqlQuery querry;
-
- //执行sql语句
- if(!querry.exec(sql))
- {
- QMessageBox::information(this,"提示","显示失败");
- return ;
- }
-
- int i=0; //记录行数
- QString Z; //记录账号
- QString M; //记录密码
- while(querry.next())
- {
- //遍历每条记录中的每一项内容
- for(int j=0;j<querry.record().count();j++) //其中count为每一行数据的个数
- {
-
- if(ui->lineEdit->text()==querry.record().value(j).toString())
- {
- QMessageBox::information(this,"失败","该账号已被注册请重新输入");
- return ;
- }
- }
-
- i++;
- }
-
- //准备sql语句
- QString sql2 = QString("insert into zhanghu_info(zhanghao,mima)"
- "values('%1',\"%2\")").arg(zhanghao).arg(mima);
-
- //准备语句执行者
- if(!querry.exec(sql2))
- {
- QMessageBox::information(this,"失败","注册失败");
- return ;
- }
- else
- {
- QMessageBox::information(this,"成功","注册成功");
- }
-
- }
-
-
- void third::on_pushButton_2_clicked()
- {
- emit jump3();
-
- this->hide();
- }

3、登录后界面
头文件
- #ifndef SECOND_H
- #define SECOND_H
-
- #include
-
- namespace Ui {
- class Second;
- }
-
- class Second : public QDialog
- {
- Q_OBJECT
-
- public:
- explicit Second(QWidget *parent = nullptr);
- ~Second();
-
- public slots:
- void jump_slot(); //接收跳转信号的槽函数
-
- private:
- Ui::Second *ui;
- };
-
- #endif // SECOND_H
源文件
- #include "second.h"
- #include "ui_second.h"
-
- Second::Second(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::Second)
- {
- ui->setupUi(this);
- }
-
- Second::~Second()
- {
- delete ui;
- }
-
- //接收跳转信号对应的槽函数
- void Second::jump_slot()
- {
- this->show(); //将自己界面进行展示
- }

4、思维导图
