欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何用qt实现学生信息管理系统

程序员文章站 2022-05-08 14:35:17
...

源码资源https://download.csdn.net/download/it_create/10625713

以下是效果图

如何用qt实现学生信息管理系统

如何用qt实现学生信息管理系统

如何用qt实现学生信息管理系统

如何用qt实现学生信息管理系统

如何用qt实现学生信息管理系统

mainwindow.cpp

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

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

namespace Ui {
class MainWindow;
}

struct stdinformation
{
    QString name;
    QString number;
    QString  results;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

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


private slots:
    void on_change_pushButton_clicked();

    void on_add_pushButton_clicked();

    void on_delete_pushButton_clicked();

    void on_refer_pushButton_clicked();

    void tabletimeslot();

private:
    Ui::MainWindow *ui;
    QSqlDatabase db;
    QTimer *tabletime;
    deleteDialog student_delete;
    insertDialog student_insert;
    updateDialog student_update;
    selectDialog student_select;
    int t;

};

#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);
	
	//创建数据库,并检查数据库中的数据并在表中生成相应的行数
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("my.db");
    if(db.open())
    {
        qDebug()<<"打开成功";
    }
    QSqlQuery query;
    if(query.exec("create table student(StudentName VARCHAR(30),StudentNumber VARCHAR(30),"
               "StudentCollege VARCHAR(30),StudentAge VARCHAR(30),StudentSex VARCHAR(30))"))
    {
        qDebug()<<"创建成功";
    }

    query.exec("select * from student");
    t=0;
    while(query.next())
    {
        ui->information_tableWidget->insertRow(t);
        t++;
    }
	 
	//刷新表的定时器
    tabletime = new QTimer(this);
    tabletime->start(2000);
    connect(tabletime,SIGNAL(timeout()),this,SLOT(tabletimeslot()));


}

MainWindow::~MainWindow()
{
    delete ui;
    qDebug()<<"hello";
}

void MainWindow::on_change_pushButton_clicked()
{
    student_update.show();  //修改学生信息界面
}

void MainWindow::on_add_pushButton_clicked()
{
    int index_row = ui->information_tableWidget->rowCount();
    ui->information_tableWidget->setRowCount(index_row+1);
    student_insert.show();   //添加学生信息界面
}

void MainWindow::on_delete_pushButton_clicked()
{
    student_delete.show();  //删除学生信息界面
}

void MainWindow::on_refer_pushButton_clicked()
{
    student_select.show();  //查找学生信息界面
}

void MainWindow::tabletimeslot() //主界面刷新表
{
    ui->information_tableWidget->clearContents();
    //QString studentname,studentnumber,studentcollege,studentage,studentsex;
    QSqlQuery query;
    query.exec("select * from student");
    int t=0;
    while(query.next())
    {
        ui->information_tableWidget->setItem(t,0,new QTableWidgetItem(query.value(0).toString()));
        ui->information_tableWidget->setItem(t,1,new QTableWidgetItem(query.value(1).toString()));
        ui->information_tableWidget->setItem(t,2,new QTableWidgetItem(query.value(2).toString()));
        ui->information_tableWidget->setItem(t,3,new QTableWidgetItem(query.value(3).toString()));
        ui->information_tableWidget->setItem(t,4,new QTableWidgetItem(query.value(4).toString()));
        ui->information_tableWidget->setItem(t,5,new QTableWidgetItem(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();*/
        t++;
    }
}

insertdialog.h

#ifndef INSERTDIALOG_H
#define INSERTDIALOG_H

#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 "mainwindow.h"
#include <QSqlQuery>

insertDialog::insertDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::insertDialog)
{
    ui->setupUi(this);
}

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

void insertDialog::on_insert_pushButton_clicked()
{
    if(ui->studentname_lineEdit->text().isEmpty() || ui->studentnumber_ineEdit->text().isEmpty() ||
            ui->studentcollege_lineEdit->text().isEmpty() || ui->studentage_lineEdit->text().isEmpty() ||
            ui->studentsex_lineEdit->text().isEmpty())
    {
        QMessageBox::information(this,"提示","请输入完整");
    }
    else
    {
    QString student_name = ui->studentname_lineEdit->text();
    QString student_number = ui->studentnumber_ineEdit->text();
    QString student_college = ui->studentcollege_lineEdit->text();
    QString student_age =ui->studentage_lineEdit->text();
    QString student_sex =ui->studentsex_lineEdit->text();
    QSqlQuery query;
    int index_delete = 0;
    query.exec("select * from student");
    while(query.next())
    {
        if(student_number == query.value(1))
        {
            index_delete = 1;
            break;
        }
    }
    if(index_delete == 1)
    {
        QMessageBox::information(this,"提示","学号已存在");
    }
    else
    {
    QString insert_student = QString("insert into student values(\"%1\",\"%2\",\"%3\",\"%4\",\"%5\")").arg(
                student_name,student_number,student_college,student_age,student_sex);
    qDebug()<<insert_student;
    if(query.exec(insert_student))
    {
        QMessageBox::information(this,"成功","插入成功");
    }
    else
        QMessageBox::information(this,"失败","插入失败");
    }
    }
}

deletedialog.h

#ifndef DELETEDIALOG_H
#define DELETEDIALOG_H

#include <QDialog>

namespace Ui {
class deleteDialog;
}

class deleteDialog : public QDialog
{
    Q_OBJECT

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

private slots:
    void on_delete_pushButton_clicked();

private:
    Ui::deleteDialog *ui;
};

#endif // DELETEDIALOG_H

deletedialog.cpp

#include "deletedialog.h"
#include "ui_deletedialog.h"
#include "mainwindow.h"
#include <QSqlQuery>

deleteDialog::deleteDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::deleteDialog)
{
    ui->setupUi(this);
}

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

void deleteDialog::on_delete_pushButton_clicked()
{
    if(ui->scanf_lineEdit->text().isEmpty())
    {
        QMessageBox::information(this,"提示","请输入内容");
    }
    else
    {
    if(ui->studentname_radioButton->isChecked())
    {
        QString student_name;
        student_name = ui->scanf_lineEdit->text();
        QSqlQuery query;
        query.exec("select * from student");
        int t =0;
        while(query.next())
        {
            if(student_name == query.value(0).toString())
            {
                t= 1;
                break;
            }
        }
        if(t == 1)
        {
        QString delete_name = QString("delete from student where StudentName = \"%1\"").arg(student_name);
        qDebug()<<delete_name;
        if(query.exec(delete_name))
        {
            QMessageBox::information(this,"成功","删除成功");
        }
        else
        {
            QMessageBox::information(this,"失败","删除失败");
        }
    }
        else
        {
            QMessageBox::information(this,"提示","不存在的姓名");
        }
    }
    else if(ui->studentnumber_radioButton->isChecked())
    {
        QString student_number;
        student_number = ui->scanf_lineEdit->text();
        QSqlQuery query;
        query.exec("select * from student");
        int t =0;
        while(query.next())
        {
            if(student_number == query.value(1).toString())
            {
                t= 1;
                break;
            }
        }
        if( t== 1)
        {
        QString delete_student = QString("delete from student where StudentNumber = \"%1\"").arg(student_number);
        if(query.exec(delete_student))
        {
            QMessageBox::information(this,"成功","删除成功");
        }
        else
            QMessageBox::information(this,"失败","删除失败");
    }
        else
        {
            QMessageBox::information(this,"提示","不存在的学号");
        }
    }
    else
    {
        QMessageBox::information(this,"提示","请选择姓名或学号");
    }
    }
}