最新消息:很久没有没有写qt了,qt也不是我的工作方向,看到本篇博文日积月累的点击量很不错,本人决定将该简易系统进行升级改造,提升的内容和方向远超本篇设计的系统,本篇系统其实是一个非常入门的操作,对于基础差的可以快速的理解一些操作,非常简单。升级后的系统内容比当前的这个系统扩展了非常多,也更复杂,所以要想理解,可以先把本篇博文这个先理解,对于基础不错的,则可以移步到我的新的开发的信息系统,本来那个系统我在一年前就已经完成,就想着弄得更完善,但是限于平时时间有限,可以先开放出来,供大家参考学习。后续有时间再继续升级,新的系统功能更强大,设计更合理,如果理解了的话,对于工作上的开发也会是一个不错的参考,新系统不采用任何第三方库,包括日志系统,也是纯手工打造。

新系统源码地址:https://gitee.com/dxl96/StdMsgSystem

信息系统博客新地址:https://blog.csdn.net/IT_CREATE/article/details/131612076

本系统源码资源:学生信息管理系统.zip · itcreat/qt资源 - Gitee.com

以下是效果图

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QString>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QtWidgets>
#include <QTimer>
#include "insertdialog.h"
#include "updatedialog.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    // 刷新表格
    void tableReflash(QString selectSql = QString());
    ~MainWindow();

private slots:
    //-----------------------------------界面转到槽函数----------------
    void on_add_pushButton_clicked();

    void on_change_pushButton_clicked();

    void on_delete_pushButton_clicked();

    void on_refer_pushButton_clicked();

    //-----------------------------------自定义槽函数----------------
    void changeBackground(); // 更改背景
private:
    Ui::MainWindow *ui;
    QSqlDatabase db;
    // 背景色集合
    QList<QString> backgrounds;
    // 当前指向景色集合的索引
    int backgroundIndex;

    // 初始化背景色集合
    void initBackgrounds();
    // 单元格设置
    void cellSetting(int row, int column, QString text);
    // 删除学生信息
    void deleteStudent();
    // 查询学生信息
    void selectStudent();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("学生信息管理系统");

    // 设置输入框提示信息
    ui->select_param_edit->setToolTip("查询支持模糊查询,输入姓名或者学号关键词即可");

    // 设置列表第一列宽度100
    ui->information_tableWidget->setColumnWidth(0,100);
    // 设置列表自动填充满窗口
    ui->information_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    // 设置列表列头
    ui->information_tableWidget->setColumnCount(7);
    ui->information_tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("*选择栏"));
    ui->information_tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("主键(id)"));
    ui->information_tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("学生姓名"));
    ui->information_tableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem("学生学号"));
    ui->information_tableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem("学生专业"));
    ui->information_tableWidget->setHorizontalHeaderItem(5, new QTableWidgetItem("学生年龄"));
    ui->information_tableWidget->setHorizontalHeaderItem(6, new QTableWidgetItem("学生性别"));

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("my.db");
    if(db.open())
    {
        qDebug()<<"打开成功";
    }
    QString createSql = "create table student(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,studentName VARCHAR(30),studentNumber VARCHAR(30),"
                        "studentCollege VARCHAR(30),studentAge VARCHAR(30),studentSex VARCHAR(30))";
    qDebug()<<createSql;
    QSqlQuery query;
    if(query.exec(createSql))
    {
        qDebug()<<"创建成功";
    } else {
        qDebug()<<"创建失败,表或已存在";
    }
    // 刷新表格数据
    tableReflash();

    // 初始化背景色集合数据
    initBackgrounds();

    // 定时改变背景,20秒切换一次
    QTimer* backgroundTimer = new QTimer(this);
    backgroundTimer->start(20000);
    connect(backgroundTimer,SIGNAL(timeout()),this,SLOT(changeBackground()));

    // 隐藏工具栏和状态栏
    this->statusBar()->hide();
    ui->mainToolBar->setVisible (false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 初始化背景色集合
void MainWindow::initBackgrounds()
{
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 white,stop:1 white);");
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #79F1A4,stop:1 #0E5CAD);");
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #97ABFF,stop:1 #123597);");
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #90F7EC,stop:1 #32CCBC);");
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #43CBFF,stop:1 #9708CC);");
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #5EFCE8,stop:1 #736EFE);");
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #FFD26F,stop:1 #3677FF);");
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #92FFC0,stop:1 #002661);");
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #C2FFD8,stop:1 #465EFB);");
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #81FFEF,stop:1 #F067B4);");
    this->backgrounds.append("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #FFF886,stop:1 #F072B6);");
}

// 更改背景
void MainWindow::changeBackground()
{
    if(backgroundIndex < backgrounds.size()) {
        this->setStyleSheet(backgrounds.at(backgroundIndex));
        backgroundIndex++;
    } else {
        backgroundIndex = 0;
    }
}

// 添加按钮点击事件
void MainWindow::on_add_pushButton_clicked()
{
    insertDialog* student_insert = new insertDialog(this);
    student_insert->setAttribute(Qt::WA_DeleteOnClose); // 关闭时销毁对象
    student_insert->setModal(true); // 设置为模态窗口,即窗口打开后只能该修改界面操作,不能在其他窗口进行操作,保证操作时的唯一性
    student_insert->show();
}

// 修改按钮点击事件
void MainWindow::on_change_pushButton_clicked()
{
    int rowCount = ui->information_tableWidget->rowCount();
    QList<int> ids;
    for(int row = 0;row<rowCount;row++) {
        QTableWidgetItem * item = ui->information_tableWidget->item(row,0);
        Qt::CheckState status = item->checkState();
        if(status == Qt::CheckState::Checked) {
            QLineEdit* idItem = (QLineEdit*) ui->information_tableWidget->cellWidget(row, 1);
            ids.append(idItem->text().toInt());
        }
    }
    if(ids.isEmpty()) {
        QMessageBox::information(this,"提示","请先勾选要修改的行");
        return;
    }
    if(ids.size() > 1) {
        QMessageBox::information(this,"提示","修改时只能勾选单行数据");
        return;
    }
    updateDialog* student_update = new updateDialog(this);
    student_update->setAttribute(Qt::WA_DeleteOnClose); // 关闭时销毁对象
    student_update->setModal(true); // 设置为模态窗口,即窗口打开后只能该修改界面操作,不能在其他窗口进行操作,保证操作时的唯一性
    student_update->load(ids.at(0));
    student_update->show();
}

// 删除按钮点击事件
void MainWindow::on_delete_pushButton_clicked()
{
    deleteStudent();
}

// 查询按钮点击事件
void MainWindow::on_refer_pushButton_clicked()
{
    selectStudent();
}

// 设置单元格信息
void MainWindow::cellSetting(int row, int column, QString text)
{
    QLineEdit* lineEdit = new QLineEdit(text);
    lineEdit->setStyleSheet("border: 0px;color:black"); // 设置该输入框无边框,黑色字体
    // 设置只读
    lineEdit->setReadOnly(true);
    ui->information_tableWidget->setCellWidget(row, column, lineEdit);
}

// 删除学生信息
void MainWindow::deleteStudent()
{
    int rowCount = ui->information_tableWidget->rowCount();
    QList<int> ids;
    for(int row = 0;row<rowCount;row++) {
        QTableWidgetItem * item = ui->information_tableWidget->item(row,0);
        Qt::CheckState status = item->checkState();
        if(status == Qt::CheckState::Checked) {
            QLineEdit* idItem = (QLineEdit*) ui->information_tableWidget->cellWidget(row, 1);
            ids.append(idItem->text().toInt());
        }
    }
    if(ids.isEmpty()) {
        QMessageBox::information(this,"提示","请先勾选要删除的行");
        return;
    }
    qDebug()<<"删除数据ids: "<<ids;
    QString idsStr = "";
    for(int i = 0;i< ids.size();i++) {
        if(i == 0) {
            idsStr = idsStr + QString::number(ids.at(i));
        } else {
            idsStr = idsStr + ","+QString::number(ids.at(i));
        }
    }
    QString sql = "delete from student where id in(" + idsStr + ")";
    qDebug()<<sql;
    QSqlQuery query;
    if(query.exec(sql)) {
        // 刷新表格
        tableReflash();
        QMessageBox::information(this,"成功","删除成功");
    } else {
        QMessageBox::information(this,"失败","删除失败");
    }
}

// 查询学生信息
void MainWindow::selectStudent()
{
    QString searchParam = ui->select_param_edit->text().trimmed();
    if(searchParam.isEmpty()) {
        tableReflash();
        return;
    }
    tableReflash(QString("select id,studentName,studentNumber,studentCollege,studentAge,studentSex from student where studentName like \"%1%2\" or studentNumber like \"%3%4\"")
                 .arg(searchParam,"%",searchParam,"%"));
}

// 刷新表格数据
void MainWindow::tableReflash(QString selectSql)
{
    // 先移除表格所有行
    for(int row = ui->information_tableWidget->rowCount() - 1;row >= 0; row--)
    {
        ui->information_tableWidget->removeRow(row);
    }
    // 查询数据添加到表格
    QSqlQuery query;
    if(selectSql.isEmpty()) {
        QString sql = "select id,studentName,studentNumber,studentCollege,studentAge,studentSex from student order by id desc";
        qDebug()<<sql;
        query.exec(sql);
    } else {
        qDebug()<<selectSql;
        query.exec(selectSql);
    }
    int row =0;
    while(query.next())
    {
        int index_row =ui->information_tableWidget->rowCount();
        ui->information_tableWidget->setRowCount(index_row+1);
        // 第一列插入复选框
        QTableWidgetItem *check = new QTableWidgetItem();
        check->setCheckState(Qt::Unchecked);
        check->setFlags(check->flags() ^ Qt::ItemIsEditable);
        ui->information_tableWidget->setItem(row,0,check); //插入复选框
        cellSetting(row,1, query.value(0).toString());
        cellSetting(row,2, query.value(1).toString());
        cellSetting(row,3, query.value(2).toString());
        cellSetting(row,4, query.value(3).toString());
        cellSetting(row,5, query.value(4).toString());
        cellSetting(row,6, query.value(5).toString());
        qDebug()<<query.value(0).toString()<<","<<query.value(1).toString()<<","<<query.value(2).toString()<<
                  ","<<query.value(3).toString()<<","<<query.value(4).toString()<<","<<query.value(5).toString();
        row++;
    }
}

insertdialog.h

#ifndef INSERTDIALOG_H
#define INSERTDIALOG_H

#include <QMainWindow>
#include <QDialog>

namespace Ui {
class insertDialog;
}

class insertDialog : public QDialog
{
    Q_OBJECT

public:
    explicit insertDialog(QWidget *parent = 0);
    ~insertDialog();

private slots:
    void on_insert_pushButton_clicked();

private:
    Ui::insertDialog *ui;
};

#endif // INSERTDIALOG_H

insertdialog.cpp

#include "insertdialog.h"
#include "ui_insertdialog.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QSqlQuery>

insertDialog::insertDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::insertDialog)
{
    ui->setupUi(this);
    setWindowTitle("新增学生信息");
}

insertDialog::~insertDialog()
{
    delete ui;
}

void insertDialog::on_insert_pushButton_clicked()
{
    QString student_name = ui->studentname_lineEdit->text().trimmed();
    QString student_number = ui->studentnumber_ineEdit->text().trimmed();
    QString student_college = ui->studentcollege_lineEdit->text().trimmed();
    QString student_age =ui->studentage_lineEdit->text().trimmed();
    QString student_sex =ui->studentsex_lineEdit->text().trimmed();
    if(student_name.isEmpty() || student_number.isEmpty() ||
            student_college.isEmpty() || student_age.isEmpty() ||
            student_sex.isEmpty())
    {
        QMessageBox::information(this,"提示","请输入完整");
        return;
    }

    QSqlQuery query;
    QString sql = QString("select count(1) from student where studentNumber = \"%1\"").arg(student_number);
    qDebug()<<sql;
    query.exec(sql);
    while(query.next())
    {
        int count = query.value(0).toInt();
        if(count > 0) {
            QMessageBox::information(this,"提示","学号已存在");
            return;
        }
    }

    QString insertSql = QString("insert into student (studentName,studentNumber,studentCollege,studentAge,studentSex) values (\"%1\",\"%2\",\"%3\",\"%4\",\"%5\")").arg(
                student_name,student_number,student_college,student_age,student_sex);
    qDebug()<<insertSql;
    if(query.exec(insertSql))
    {
        // 关闭窗口
        close();

        // 刷新列表
        MainWindow* mainWidget = (MainWindow*) parentWidget();
        mainWidget->tableReflash();
        QMessageBox::information(this,"成功","插入成功");

    }
    else
        QMessageBox::information(this,"失败","插入失败");

}

updatedialog.h

#ifndef UPDATEDIALOG_H
#define UPDATEDIALOG_H

#include <QDialog>

namespace Ui {
class updateDialog;
}

class updateDialog : public QDialog
{
    Q_OBJECT

public:
    explicit updateDialog(QWidget *parent = 0);
    // 通过学生表id字段值加载数据到修改界面
    void load(int id);
    ~updateDialog();

private slots:
    void on_update_pushButton_clicked();

private:
    Ui::updateDialog *ui;
    // id,学生信息的id字段值
    int id;
};

#endif // UPDATEDIALOG_H

updatedialog.cpp

#include "updatedialog.h"
#include "ui_updatedialog.h"
#include "mainwindow.h"
#include <QSqlQuery>
updateDialog::updateDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::updateDialog)
{
    ui->setupUi(this);
    setWindowTitle("修改学生信息");
}

void updateDialog::load(int id)
{
    this->id = id;
    QSqlQuery query;
    query.exec(QString("select studentName,studentNumber,studentCollege,studentAge,studentSex from student where id = %1").arg(QString::number(id)));
    QString studentName;
    QString studentNumber;
    QString studentCollege;
    QString studentAge;
    QString studentSex;
    while(query.next())
    {
        studentName = query.value(0).toString();
        studentNumber = query.value(1).toString();
        studentCollege = query.value(2).toString();
        studentAge = query.value(3).toString();
        studentSex = query.value(4).toString();
    }
    ui->number_lineEdit->setText(studentNumber);
    ui->number_lineEdit->setReadOnly(true);
    ui->namechange_lineEdit->setText(studentName);
    ui->numberchange_lineEdit->setText(studentNumber);
    ui->collegechange_lineEdit->setText(studentCollege);
    ui->agechange_lineEdit->setText(studentAge);
    ui->sexchange_lineEdit->setText(studentSex);
}

updateDialog::~updateDialog()
{
    delete ui;
}

void updateDialog::on_update_pushButton_clicked()
{
    QString number = ui->number_lineEdit->text().trimmed();
    QString namechange =ui->namechange_lineEdit->text().trimmed();
    QString numberchange = ui->numberchange_lineEdit->text().trimmed();
    QString collegechange = ui->collegechange_lineEdit->text().trimmed();
    QString agechange =ui->agechange_lineEdit->text().trimmed();
    QString sexchange =ui->sexchange_lineEdit->text().trimmed();
    if(number.isEmpty() || namechange.isEmpty() ||
            collegechange.isEmpty() || numberchange.isEmpty() ||
            agechange.isEmpty() || sexchange.isEmpty())
    {
        QMessageBox::information(this,"提示","请输入完整内容");
        return;
    }

    QString  countSql = QString("select count(1) from student where studentNumber = \"%1\" and id != %2").arg(numberchange, QString::number(id));
    qDebug()<<countSql;
    QSqlQuery query;
    query.exec(countSql);
    while(query.next())
    {
        int count = query.value(0).toInt();
        if(count > 0) {
            QMessageBox::information(this,"提示","已经存在相同的学号");
            return;
        }
    }

    QString sql = QString("update student set studentName=\"%1\" ,studentNumber = \"%2\","
                                     "studentCollege =\"%3\" ,studentAge =\"%4\",studentSex  =\"%5\" where id =%6")
            .arg(namechange,numberchange,collegechange,agechange,sexchange,QString::number(id));
    qDebug()<<sql;
    if(query.exec(sql))
    {
        // 关闭窗口
        close();

        // 刷新表格数据
        MainWindow* mainWindow = (MainWindow*) parentWidget();
        mainWindow->tableReflash();
        QMessageBox::information(this,"成功","修改成功");
    }
    else
        QMessageBox::information(this,"失败","修改失败");
}

Logo

快速构建 Web 应用程序

更多推荐