1、添加注册功能到数据库
头文件
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
//消息对话框类头文件 - #include
- #include
- #include
//数据库管理类 - #include
//执行sql语句的类 - #include
//数据库记录的类 - #include "second.h"
-
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- signals:
- void my_signal(); //定义一个无参无返回值的信号函数
- void jump(); //自定义跳转信号函数
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
-
- private slots:
-
- void my_slot(); //自定义无参无返回值槽函数
-
- void on_dengBtn_clicked();
-
- void on_quBtn_clicked();
-
- void on_zhuBtn_clicked();
-
- private:
- Ui::Widget *ui;
-
- second *s1;
-
- QSqlDatabase db; //定义一个数据库的类对象
- };
- #endif // WIDGET_H
源文件
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
- s1 = new second;
- //使用qt5版本,把登录、注册、取消按钮连接
- connect(ui->dengBtn, &QPushButton::clicked, this, &Widget::my_slot);
- connect(ui->quBtn, &QPushButton::clicked, this, &Widget::my_slot);
- connect(ui->zhuBtn, &QPushButton::clicked, this, &Widget::my_slot);
- connect(this, &Widget::jump, s1, &second::jump_slot);
-
- this->setWindowTitle("灵能事务所"); //设置窗口标题
- this->setWindowIcon(QIcon(":/pic/biao.jpg"));
-
- //判断自己的数据库对象中是否包含了要处理的数据库,如果没有包含,则添加一个数据库,如果包含了,就可以打开
- if(!db.contains("mydatabase.db"))
- {
- //添加一个数据库,调用该类中的静态成员函数addDatabase
-
- db = QSqlDatabase::addDatabase("QSQLITE");
-
- //设置数据库的名字
- db.setDatabaseName("mydatabase.db");
- }
-
- //此时已经有一个名为mydatabase.db的数据库
- // 打开数据库
- if(!db.open())
- {
- QMessageBox::information(this, "失败", "打开失败");
- return;
- }
-
- //需要使用sql语句进行创建表的操作
- //1、准备sql语句
- QString sql = "create table if not exists stu_info(" //创建表
- "zczh integer primary key," //注册账号,主键
- "zcmm integer)"; //注册密码
- //准备语句执行者
- QSqlQuery querry;
-
- //让语句执行者执行sql语句
-
- if(!querry.exec(sql))
- {
- QMessageBox::information(this, "失败", "创建表失败");
- return;
- }
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::my_slot()
- {
- //emit jump();
- }
-
- //登录按钮对应的槽函数
- void Widget::on_dengBtn_clicked()
- {
- //传入账号密码
- QString zczh = ui->lineEdit1->text();
- QString zcmm = ui->lineEdit2->text();
-
- QString sql = QString("select * from stu_info where zczh = '%1' and zcmm = '%2' ").arg(zczh).arg(zcmm);
-
- //准备语句执行者
- QSqlQuery querry;
- if(!querry.exec(sql))
- {
-
- //1、调用构造函数实例化对象
- QMessageBox::information(this, "失败", "登录失败");
-
-
- //3、对结果进行判断
- if(QMessageBox::Ok)
- {
- ui->lineEdit1->clear();
- ui->lineEdit2->clear();
- qDebug()<<"继续登录";
- }else if(QMessageBox::Cancel)
- {
- close();
- }
- }else
- {
- //1、调用构造函数实例化对象
- QMessageBox::information(this, "成功", "登录成功");
-
- //3、对结果进行判断
- if(QMessageBox::Ok)
- {
- emit jump();
- }
- }
-
- }
-
- //取消
- void Widget::on_quBtn_clicked()
- {
- //1、调用静态函数实例化对象
- int ret = QMessageBox::information(this, //父组件
- "问题", //对话框标题
- "是否确定退出登录", //对话框文本内容
- QMessageBox::Yes | QMessageBox::No, //对话框提供的按钮
- QMessageBox::No); //默认选中的按钮
-
- //3、对结果进行判断
- if(ret == QMessageBox::Yes)
- {
- close();
- }
- }
-
- //注册
- void Widget::on_zhuBtn_clicked()
- {
- //获取ui界面中要录入的数据
- QString zczh = ui->lineEdit1->text();
- QString zcmm = ui->lineEdit2->text();
-
- //要确保每个编辑器中都有数据
- if(zczh.isEmpty() || zcmm.isEmpty())
- {
- QMessageBox::information(this, "提示", "账号或密码为空,请重新输入");
- return ;
- }
-
- //准备sql语句
- QString sql = QString("insert into stu_info(zczh, zcmm)"
- "values('%1', '%2')").arg(zczh).arg(zcmm);
-
- //准备语句执行者
- QSqlQuery querry;
- if(!querry.exec(sql))
- {
- QMessageBox::information(this, "失败", "注册失败");
- return;
- }else
- {
- QMessageBox::information(this, "成功", "注册成功");
- }
- }
ui界面:

