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

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

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

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图书管理系统是一个常见的应用场景,Qt+SQLite是一种较为常见的实现方式。下面我简单介绍一下Qt+SQLite实现图书管理系统的原理架构以及代码实现。 ## 原理架构 Qt是一个跨平台的GUI应用程序开发框架,可以方便地进行界面设计和事件处理。而SQLite是一种轻量级的关系型数据库,它的特点是占用资源少、易于部署和使用。将两者结合起来,就可以实现一个轻量级的图书管理系统。 具体的实现过程如下: 1. 设计数据库表结构,包括书籍信息表、借阅信息表等。 2. 使用Qt提供的SQL API连接SQLite数据库,创建数据库表和索引等。 3. 编写界面代码,设计图书添加、删除、修改、查询等功能的界面。 4. 在界面代码中调用SQL API,实现对数据库的增删改查操作。 5. 编写业务逻辑代码,实现借阅、归还等功能。 ## 代码实现 下面是一个简单的图书管理系统的实现代码,包括数据库的创建和表结构的定义、界面设计和业务逻辑的实现。 ### 数据库的创建和表结构的定义 ```cpp #include <QSqlDatabase> #include <QSqlQuery> #include <QDebug> // 创建数据库连接 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("library.db"); // 打开数据库 if (!db.open()) { qDebug() << "Open database failed."; return; } // 创建书籍信息表 QSqlQuery query(db); query.exec("CREATE TABLE books (" "id INTEGER PRIMARY KEY AUTOINCREMENT, " "name TEXT, " "author TEXT, " "publisher TEXT, " "isbn TEXT UNIQUE, " "count INTEGER)"); // 创建借阅信息表 query.exec("CREATE TABLE borrow (" "id INTEGER PRIMARY KEY AUTOINCREMENT, " "book_id INTEGER, " "borrower TEXT, " "borrow_date TEXT, " "return_date TEXT, " "FOREIGN KEY(book_id) REFERENCES books(id))"); ``` ### 界面设计 使用Qt提供的界面设计工具,可以轻松地设计出图书管理系统的各个界面,包括添加、删除、修改、查询等功能。 ### 业务逻辑实现 ```cpp // 添加一本书籍 void addBook(const QString& name, const QString& author, const QString& publisher, const QString& isbn, int count) { QSqlQuery query(db); query.prepare("INSERT INTO books (name, author, publisher, isbn, count) " "VALUES (:name, :author, :publisher, :isbn, :count)"); query.bindValue(":name", name); query.bindValue(":author", author); query.bindValue(":publisher", publisher); query.bindValue(":isbn", isbn); query.bindValue(":count", count); query.exec(); } // 借阅一本书籍 void borrowBook(int bookId, const QString& borrower, const QString& borrowDate, const QString& returnDate) { QSqlQuery query(db); query.prepare("INSERT INTO borrow (book_id, borrower, borrow_date, return_date) " "VALUES (:bookId, :borrower, :borrowDate, :returnDate)"); query.bindValue(":bookId", bookId); query.bindValue(":borrower", borrower); query.bindValue(":borrowDate", borrowDate); query.bindValue(":returnDate", returnDate); query.exec(); // 减少书籍数量 query.prepare("UPDATE books SET count = count - 1 WHERE id = :id"); query.bindValue(":id", bookId); query.exec(); } // 查询所有书籍 QSqlQueryModel* queryAllBooks() { QSqlQueryModel* model = new QSqlQueryModel(); model->setQuery("SELECT * FROM books"); return model; } // 查询借阅信息 QSqlQueryModel* queryBorrowInfo(int bookId) { QSqlQueryModel* model = new QSqlQueryModel(); model->setQuery(QString("SELECT * FROM borrow WHERE book_id = %1").arg(bookId)); return model; } ``` 这样,一个简单的图书管理系统就实现了。当然,实际应用中还需要进行一些优化和完善,例如加入用户登录、权限管理、数据备份等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值