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_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    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_OBJECT

public:
    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_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    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_OBJECT

public:
    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数据库。

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

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

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-06-17 23:06:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-17 23:06:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-17 23:06:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-17 23:06:05       18 阅读

热门阅读

  1. IMP和引用码 位置关系

    2024-06-17 23:06:05       9 阅读
  2. 第一章 - 第10节- 计算机网络 - 课后习题

    2024-06-17 23:06:05       7 阅读
  3. 【Linux】shell——传递参数

    2024-06-17 23:06:05       5 阅读
  4. 颍川黄姓的高光时刻

    2024-06-17 23:06:05       8 阅读
  5. 编译期间生成代码(Lombok原理)

    2024-06-17 23:06:05       9 阅读
  6. 一个C++版本的web服务器

    2024-06-17 23:06:05       7 阅读
  7. C++语法14 双分支结构 if…else语句和逻辑运算符

    2024-06-17 23:06:05       5 阅读
  8. Selenium WebDriver - 其它

    2024-06-17 23:06:05       8 阅读
  9. uthash使用指南

    2024-06-17 23:06:05       7 阅读
  10. 深度学习中的热力图

    2024-06-17 23:06:05       5 阅读
  11. selenium常见难点解决方案

    2024-06-17 23:06:05       5 阅读