当前位置: 首页 > news >正文

QT6 学生管理系统以及登录(QSQLITE数据库)

一、准备工具以及环境

本文采用的是QT Creator6.5.3版本,代码基于C++语言,文中所用到的数据库是QSQLITE库。

因为做的是一个简单的学生管理系统,所以只是做到了简单的对数据库进行增删改查等操作,以及一个简单的登录界面。

二、UI界面以及结果展示

1、登录UI

所用到的控件分别是RadioButton、PushButton、Label、LineEdit等。

2、登录界面展示

为了使界面不单调,我在中间地方放了个Label标签,实现gif格式图片动画播放,这里可以省略,也可以用png图片代替。

RadioButton按钮区实现密码的隐藏与不隐藏控制。

3、管理界面UI

使用到的控件在图片中有,其中中间部分的数据显示,使用的是tableView控件。

4、管理界面展示

5、成果展示

三、实现过程

1、创建文件

1)

2)

3)

4)

5)

6)

最后点击下一步,点击完成,等待几秒即可完成创建。

2、添加sql

QT       += sql

3、头文件

主要实现连接数据库、操作数据库、以及简单的提示错误等。

登录界面使用到的头文件

#ifndef LOGIN_H
#define LOGIN_H#include <QMainWindow>
#include <student.h>#include <QLabel>
#include <QMovie>#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;
};
#endif // LOGIN_H

管理界面使用的头文件

#ifndef STUDENT_H
#define STUDENT_H#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QsqlError>
#include <QsqlQueryModel>
#include <QDebug>
#include <QMessageBox>namespace Ui {
class Student;
}class Student : public QMainWindow
{Q_OBJECTpublic:explicit Student(QWidget *parent = nullptr);~Student();
private:Ui::Student *ui;
};#endif // STUDENT_H

4、数据库的连接、数据库表的创建以及实现

void MainWindow::open_login_ui()  // 连接(打开)数据库
{this->login_ui = QSqlDatabase::addDatabase("QSQLITE");this->login_ui.setDatabaseName("login.db");if(!login_ui.open()){qDebug()<<"打开失败";}else{qDebug()<<"打开成功";}
}
void MainWindow::creat_login_ui() // 创建数据库表
{QSqlQuery query(login_ui);QString login = QString("create table login(""user int primary key not null,""password int not null)");if(!(query.exec(login))){qDebug()<<"数据库表创建失败";}else{qDebug()<<"数据库表创建成功";}
}
void MainWindow::movie_show()  // 动画的实现 (也可以选择放置图片)
{this->movie = new QMovie(":/img/6.gif");ui->label_show->setMovie(movie);movie->setSpeed(65);movie->start();
}

四、对数据库操作

登录界面用到的槽函数

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;QMovie *movie;QSqlDatabase login_ui;  // 账户密码管理public slots:void movie_show();void open_login_ui();  // 打开数据库构造函数void creat_login_ui();  // 创建数据表构造函数
private slots:void on_pushButton_login_clicked();void on_pushButton_register_clicked();void on_radioButton_clicked();
};
#endif // LOGIN_H

管理界面用到的槽函数

namespace Ui {
class Student;
}class Student : public QMainWindow
{Q_OBJECTpublic:explicit Student(QWidget *parent = nullptr);~Student();private slots:void on_pushButton_insert_clicked();void on_pushButton_find_clicked();void on_pushButton_change_clicked();void on_pushButton_del_clicked();void on_pushButton_clear_clicked();void link_sql();  // 连接数据库void create_tab(); // 创建数据库表private:Ui::Student *ui;QSqlDatabase db_student;QSqlQueryModel model;  // 储存结果集
};

1、增(添加)

void Student::on_pushButton_insert_clicked()  // 添加
{QSqlQuery query;int id = ui->lineEdit_id->text().toInt();if(id == 0){QMessageBox::critical(this,"错误","学生的学号不能为0",QMessageBox::Ok);return ;}QString name = ui->lineEdit_name->text();if(name == ""){QMessageBox::critical(this,"错误","学生的姓名不能为空",QMessageBox::Ok);return ;}double score = ui->lineEdit_score->text().toDouble();if(score<0 || score >100){QMessageBox::critical(this,"错误","学生的成绩不能小于0或者大于100",QMessageBox::Ok);return ;}QString list = QString("insert into student ""values(%1,'%2',%3)").arg(id).arg(name).arg(score);if(query.exec(list) == false){QMessageBox::critical(this,"错误","数据插入失败!",QMessageBox::Ok);return ;}ui->label_show->clear();ui->label_show->setText("插入成功!");
}

2、删(删除)

void Student::on_pushButton_del_clicked() // 删除
{ui->label_show->clear();// 获取用户输入的姓名QString name = ui->lineEdit_name->text();// 检查姓名是否为空if (name.isEmpty()) {QMessageBox::warning(this, "Error", "Please enter a name to delete.");return;}// 执行删除操作QSqlQuery query;query.prepare("DELETE FROM student WHERE name = ?");query.addBindValue(name);if (!query.exec()) {QMessageBox::critical(this, "Error", "Failed to delete data: " + query.lastError().text());return;}// 确认删除//QMessageBox::information(this, "Success", "Data deleted successfully.");ui->label_show->setText("删除成功!");
}

3、改(修改)

void Student::on_pushButton_change_clicked()  // 修改
{// 获取用户输入QString id = ui->lineEdit_id->text();QString name = ui->lineEdit_name->text();QString score = ui->lineEdit_score->text();// 验证输入if (id.isEmpty() || name.isEmpty() || score.isEmpty()) {QMessageBox::warning(this, "错误", "输入不能为空");return;}bool scoreOk;int scoreInt = score.toInt(&scoreOk);if (!scoreOk || scoreInt < 0 || scoreInt > 100) {QMessageBox::warning(this, "错误", "分数无效,请输入0-10之间的数字");return;}// 执行更新操作QSqlQuery query;query.prepare("UPDATE student SET name = :name, score = :score WHERE id = :id");query.bindValue(":name", name);query.bindValue(":score", scoreInt);query.bindValue(":id", id);if (!query.exec()) {QMessageBox::critical(this, "Error", "Failed to update data: " + query.lastError().text());return;}ui->label_show->clear();ui->label_show->setText("修改成功!");// 确认更新// QMessageBox::information(this, "Success", "Data updated successfully.");
}

4、查(查阅)

void Student::on_pushButton_find_clicked() // 查询
{ui->label_show->clear();this->model.setQuery("SELECT * FROM student");ui->tableView->setModel(&model);ui->tableView->show();ui->label_show->setText("查询成功!");
}

5、清空输入框

void Student::on_pushButton_clear_clicked()  // 清空
{ui->label_show->clear();ui->lineEdit_id->clear();ui->lineEdit_name->clear();ui->lineEdit_score->clear();ui->label_show->setText("清空成功!");
}

五、总结

本文采用的是SQLITE数据库,在后续的改进中可以采用MySql数据库。

同时,后续改进的过程中,可以添加科目的选择与修改,分数的统计,计算平均分、排序、导出文件等操作,实现一个比较完善的学生管理系统。

当然了,还可以增添其他的模块,实现如住宿管理、课程(选课)管理等功能。

相关文章:

  • “探索AIGC市场:腾讯元宝APP加入竞争,大模型产品的未来走向与个人选择“
  • 在 .NET Core 中构建工作服务和调度运行
  • Java 关于抽象 -- Java 语言的抽象类、接口和函数式接口
  • STM32项目分享:智能蓝牙手环
  • 攻防世界--杂项misc-2017_Dating_in_Singapore
  • 力扣爆刷第149天之TOP100五连刷(LRU、K个一组)
  • 专栏【汇总】
  • Ansible——shell模块
  • 面试题:如何避免索引失效?
  • LCD电子广告牌课程设计
  • R语言绘图 --- 桑基图(Biorplot 开发日志 --- 5)
  • Win10下CodeBlock实现socket TCP server/client
  • CSS--超出就显示滚动条并设置滚动条的样式
  • LeetCode 每日一题 2024/6/3-2024/6/9
  • Qt——窗口
  • 2017-08-04 前端日报
  • flask接收请求并推入栈
  • Laravel Telescope:优雅的应用调试工具
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • Vue--数据传输
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 技术发展面试
  • 力扣(LeetCode)357
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 通过npm或yarn自动生成vue组件
  • 微信小程序开发问题汇总
  • 运行时添加log4j2的appender
  • 走向全栈之MongoDB的使用
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​油烟净化器电源安全,保障健康餐饮生活
  • #NOIP 2014#Day.2 T3 解方程
  • #数据结构 笔记一
  • (03)光刻——半导体电路的绘制
  • (2)nginx 安装、启停
  • (Matalb时序预测)WOA-BP鲸鱼算法优化BP神经网络的多维时序回归预测
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (SERIES12)DM性能优化
  • (回溯) LeetCode 77. 组合
  • (三) diretfbrc详解
  • (数据结构)顺序表的定义
  • (一)、软硬件全开源智能手表,与手机互联,标配多表盘,功能丰富(ZSWatch-Zephyr)
  • (转)eclipse内存溢出设置 -Xms212m -Xmx804m -XX:PermSize=250M -XX:MaxPermSize=356m
  • (转)Google的Objective-C编码规范
  • (转)关于如何学好游戏3D引擎编程的一些经验
  • .htaccess配置重写url引擎
  • .NET开发人员必知的八个网站
  • .net实现客户区延伸至至非客户区
  • .vue文件怎么使用_我在项目中是这样配置Vue的
  • ::前边啥也没有
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • [ IOS ] iOS-控制器View的创建和生命周期
  • [ Linux ] git工具的基本使用(仓库的构建,提交)