内容展示
说明
这是最近使用QT5.10,抽空完成的一个练手项目,各个功能均能实现,可能还存在一些不足,稍后一一介绍。学生管理系统源码文件免费下载
功能特点
1.增删改查:完整的增删改查,经简单测试,增删改查功能正常
2.数据显示:左边选中行,右侧显示选中行信息,当改行无数据时右侧信息清空。无法对无数据行进行删除和修改操作。
3.数据读写:由于qt版本和mysql版本不一致,无法将qt连接上mysql,故将其改成了txt存储。运行后自动读取txt文件,显示数据后将数据存入txt
4.页面切换:上下页面按钮可进行不同页面的切换,当处于第一页或最后一页时,再点击上一页或下一页会有提示。中间显示页码信息,一开始想做成输入数字后缺点会跳转页面的形式,但是,emmm,功能做完了,这块被忘记了,算了算了,小问题,无伤大雅。
5.查询和取消查询:查询是查询所有条件,额,精确查找,但是无法连续查找,无法查找单个条件,如仅仅查找姓名为小明的所有数据。完成查找后通过点击【刷新/清空查询】可用重置数据。
注意
1.新建数据时,如果age和grade未设置,新建后默认显示0,这是由于这两个类型是int而不是QString
2.保存的文件中不要有空行,如下图所示,空行会导致读取文件错误奔溃
源码展示
student.h
//学生管理系统-2023-1-wy
#ifndef STUDENT_H
#define STUDENT_H
#pragma execution_character_set("utf-8")
#include <QWidget>
#include <QList>
#include <QTableWidget>
#include <QMessageBox>
#include <QFile>
#include <QDir>
#include <QDebug>
#include "information.h"
namespace Ui {
class Student;
}
class Student : public QWidget
{
Q_OBJECT
public:
explicit Student(QWidget *parent = 0);
~Student();
private slots:
//新增
void create();
//修改
void change();
//删除
void del();
//查询
void search();
//上一页
void previewPage();
//下一页
void nextPage();
//选择某行
void selectRow(int r,int c);
//information中获取查询的条件
void slotSearch(QList<QString> list);
//刷新,查询取消
void refresh();
private:
struct student
{
// student() {}
QString name;
int age;
QString sex;
QString number;
int grade;
};
QList<student> stuList;
student stu;
Information *stuInformation;
private:
Ui::Student *ui;
//初始化
void init();
//信号绑定
void initConnect();
//显示数据,显示某页的全部数据
void display(int number,QList<student> list);
//显示选中某行的数据
void displaySelect();
//当前页码显示
void displayCurrentPage(int number,QList<student> list);
//读取文件
void readRecord();
//写入文件
void writeRecord();
//当前选中行的索引
int currentRow;
//所在页面第一行索引
int firstRowIndex;
//当前页面页码数
int m_currentPageNumber;
//查询条件
QList<QString> m_list;
//存储文件的路径
QString filePath;
//目标文件
QFile file;
//是否将数据存入文件 flag: 0 存入 、1 不存储
int flag;
};
#endif // STUDENT_H
student.cpp
#include "student.h"
#include "ui_student.h"
Student::Student(QWidget *parent) :
QWidget(parent),
ui(new Ui::Student)
{
ui->setupUi(this);
stuInformation = new Information();
init();
initConnect();
}
Student::~Student()
{
delete ui;
}
void Student::create()
{
QString t_strInformation = "是否将"+ui->lineEditName->text()+"写入表格?";
int result = QMessageBox::information(NULL,"warning",t_strInformation,QMessageBox::Yes|QMessageBox::No);
if(result == QMessageBox::Yes)
{
stu.name = ui->lineEditName->text();
stu.sex = ui->lineEditSex->text();
stu.age = ui->lineEditAge->text().toInt();
stu.number = ui->lineEditNumber->text();
stu.grade = ui->lineEditGrade->text().toInt();
stuList.append(stu);
displayCurrentPage(m_currentPageNumber,stuList);
display(m_currentPageNumber,stuList);
}
}
void Student::change()
{
//所选行无数据时禁止修改
if(ui->tableWidget->currentItem()==NULL ||ui->tableWidget->item(currentRow,0)->text().isNull())
return;
int t_index = ui->tableWidget->currentRow();
QString t_strInformation = "是否将"+ui->lineEditName->text()+"数据进行更改?";
int result = QMessageBox::information(NULL,"warning",t_strInformation,QMessageBox::Yes|QMessageBox::No);
if(result == QMessageBox::Yes)
{
stuList[t_index].name = ui->lineEditName->text();
stuList[t_index].sex = ui->lineEditSex->text();
stuList[t_index].age = ui->lineEditAge->text().toInt();
stuList[t_index].number = ui->lineEditNumber->text();
stuList[t_index].grade = ui->lineEditGrade->text().toInt();
display(m_currentPageNumber,stuList);
}
}
void Student::del()
{
//所选行无数据时禁止删除
if(ui->tableWidget->currentItem()==NULL ||ui->tableWidget->item(currentRow,0)->text().isNull())
return;
QString t_strWarning = "是否将"+ui->lineEditName->text()+"信息删除?";
int result = QMessageBox::warning(this,"Warning!",t_strWarning,QMessageBox::Yes|QMessageBox::No);
if(result == QMessageBox::Yes)
{
stuList.removeAt(ui->tableWidget->currentRow());
ui->tableWidget->clearContents();
ui->lineEditName->clear();
ui->lineEditSex->clear();
ui->lineEditAge->clear();
ui->lineEditNumber->clear();
ui->lineEditGrade->clear();
if(stuList.count()<=10)
m_currentPageNumber = 0;
if(stuList.count()%10 == 0)
{
refresh();
return;
}
displayCurrentPage(m_currentPageNumber,stuList);
display(m_currentPageNumber,stuList);
}
}
void Student::search()
{
stuInformation->show();
stuInformation->searchList;
}
void Student::previewPage()
{
if(m_currentPageNumber<=0)
QMessageBox::information(NULL,"提示","当前页面已经是第一页!!",QMessageBox::Yes);
else
{
ui->tableWidget->clearContents();
m_currentPageNumber--;
displayCurrentPage(m_currentPageNumber,stuList);
display(m_currentPageNumber,stuList);
}
}
void Student::nextPage()
{
int t_totalPage;
if(stuList.count()<10)
t_totalPage = 0;
else
t_totalPage = (stuList.count()-1)/10;
if(m_currentPageNumber == t_totalPage)
QMessageBox::information(NULL,"提示","当前页面已经是最后一页!!",QMessageBox::Yes);
else
{
ui->tableWidget->clearContents();
m_currentPageNumber++;
displayCurrentPage(m_currentPageNumber,stuList);
display(m_currentPageNumber,stuList);
}
}
void Student::selectRow(int r,int c)
{
currentRow = r;
displaySelect();
}
void Student::slotSearch(QList<QString> list)
{
//查询结果存放在临时的t_stuList中,不干扰stulist中的数据
QList<student> t_stuList;
int t_number = 0;
m_list = list;
flag = 1;
for(int i=0;i<stuList.count();i++)
{
//当查询条件和当前数据不一致时,且查询条件不为空,跳出此次循环进行下一次循环
if(!m_list[0].isNull() && m_list[0] != stuList[i].name)
continue;
if(!m_list[1].isNull() && m_list[1] != stuList[i].sex)
continue;
if(!m_list[2].isNull() && m_list[2] != QString::number(stuList[i].age))
continue;
if(!m_list[3].isNull() && m_list[3] != stuList[i].number)
continue;
if(!m_list[4].isNull() && m_list[4] != QString::number(stuList[i].grade))
continue;
t_stuList.append(stuList[i]);
}
if(t_stuList.count() == 0)
{
QMessageBox::about(NULL,"提示","抱歉!未查询到目标信息!");
}
else
{
ui->tableWidget->clearContents();
displayCurrentPage(t_number,t_stuList);
display(t_number,t_stuList);
}
}
void Student::refresh()
{
m_currentPageNumber = 0;
ui->tableWidget->clearContents();
ui->lineEditName->clear();
ui->lineEditSex->clear();
ui->lineEditAge->clear();
ui->lineEditNumber->clear();
ui->lineEditGrade->clear();
display(m_currentPageNumber,stuList);
displayCurrentPage(m_currentPageNumber,stuList);
}
void Student::init()
{
QString t_path = "D:/Practice/QTCreator/StudentManage/Record";
QDir dir;
flag = 0;
m_currentPageNumber = 0;
firstRowIndex = 0;
filePath = "D:/Practice/QTCreator/StudentManage/Record/student.text";
file.setFileName(filePath);
if(!dir.exists(t_path))
{
//目录不存在,创建目录,注意使用mkdir时,若指定创建的路径其上一层目录存在,则创建成功,否则创建失败。
dir.mkdir(t_path);
}
if(!file.exists(filePath))
{
//文件不存在,创建文件
file.open(QIODevice::WriteOnly);
}
//选择行为,设置以行为单位
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
//选择模式,选择单行模式,无法进行多行选择
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
readRecord();
display(m_currentPageNumber,stuList);
displayCurrentPage(m_currentPageNumber,stuList);
}
void Student::initConnect()
{
connect(ui->createButton,SIGNAL(clicked()),this,SLOT(create()));
connect(ui->deleteButton,SIGNAL(clicked()),this,SLOT(del()));
connect(ui->changeButton,SIGNAL(clicked()),this,SLOT(change()));
connect(ui->seekButton,SIGNAL(clicked()),this,SLOT(search()));
connect(ui->previewButton,SIGNAL(clicked()),this,SLOT(previewPage()));
connect(ui->nextButton,SIGNAL(clicked()),this,SLOT(nextPage()));
connect(ui->refreshButton,SIGNAL(clicked()),this,SLOT(refresh()));
connect(ui->tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(selectRow(int,int)));
connect(stuInformation,SIGNAL(signsearch(QList<QString>)),this,SLOT(slotSearch(QList<QString>)));
}
void Student::display(int number,QList<student> list)
{
m_currentPageNumber = number;
int t_countPage = list.count()/10;
int t_currentCount = (list.count())%10;
firstRowIndex = m_currentPageNumber*10;
if(list.count() == 0)
return;
if(list.count()%10 == 0)
{
t_currentCount = 10;
}
//第0/1页,1/3页形式,则当前页面必然有10条数据
if(m_currentPageNumber<t_countPage)
t_currentCount = 10;
//显示当前页面数据
for(int i=0;i<t_currentCount;i++)
{
ui->tableWidget->setItem(i,0,new QTableWidgetItem(list[firstRowIndex].name));
ui->tableWidget->setItem(i,1,new QTableWidgetItem(list[firstRowIndex].sex));
ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString::number(list[firstRowIndex].age)));
ui->tableWidget->setItem(i,3,new QTableWidgetItem(list[firstRowIndex].number));
ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(list[firstRowIndex].grade)));
firstRowIndex++;
}
//将数据写入文件
if(flag == 0)
writeRecord();
//flag重置为0
flag = 0;
}
void Student::displaySelect()
{
//tablewidget中当前选中行为空时,清空右边信息数据
if(ui->tableWidget->item(currentRow,0) == NULL ||ui->tableWidget->item(currentRow,0)->text().isNull())
{
ui->lineEditName->clear();
ui->lineEditSex->clear();
ui->lineEditAge->clear();
ui->lineEditNumber->clear();
ui->lineEditGrade->clear();
}
else
{
ui->lineEditName->setText(ui->tableWidget->item(currentRow,0)->text());
ui->lineEditSex->setText(ui->tableWidget->item(currentRow,1)->text());
ui->lineEditAge->setText(ui->tableWidget->item(currentRow,2)->text());
ui->lineEditNumber->setText(ui->tableWidget->item(currentRow,3)->text());
ui->lineEditGrade->setText(ui->tableWidget->item(currentRow,4)->text());
}
}
void Student::displayCurrentPage(int number,QList<student> list)
{
m_currentPageNumber = number;
int t_total;
if(list.count() == 0)
t_total = 0;
else
t_total = (list.count()-1)/10;
QString pagestr = QString::number(m_currentPageNumber)+("/")+QString::number(t_total);
ui->pageEdit->setText(pagestr);
}
void Student::readRecord()
{
QList<QString> t_strList;
QString readStr;
file.open(QIODevice::ReadOnly|QIODevice::Text);
while (!file.atEnd())
{
readStr = file.readLine().trimmed();
t_strList = readStr.split(",");
student t_stu;
t_stu.name = t_strList[0];
t_stu.sex = t_strList[1];
t_stu.age = t_strList[2].toInt();
t_stu.number = t_strList[3];
t_stu.grade = t_strList[4].toInt();
stuList.append(t_stu);
}
file.close();
}
void Student::writeRecord()
{
if(file.open(QIODevice::WriteOnly|QIODevice::Text))
{
for(int i = 0;i<stuList.count();i++)
{
QString str;
if(i == stuList.count()-1)
{
str = stuList[i].name+","+stuList[i].sex+","+QString::number(stuList[i].age)
+","+stuList[i].number+","+QString::number(stuList[i].grade);
}
else
{
str = stuList[i].name+","+stuList[i].sex+","+QString::number(stuList[i].age)
+","+stuList[i].number+","+QString::number(stuList[i].grade)+"\n";
}
file.write(str.toUtf8());
}
}
file.close();
}
student.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Student</class>
<widget class="QWidget" name="Student">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1050</width>
<height>450</height>
</rect>
</property>
<property name="windowTitle">
<string>学生系统</string>
</property>
<widget class="QTableWidget" name="tableWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>591</width>
<height>381</height>
</rect>
</property>
<row>
<property name="text">
<string>1</string>
</property>
</row>
<row>
<property name="text">
<string>2</string>
</property>
</row>
<row>
<property name="text">
<string>3</string>
</property>
</row>
<row>
<property name="text">
<string>4</string>
</property>
</row>
<row>
<property name="text">
<string>5</string>
</property>
</row>
<row>
<property name="text">
<string>6</string>
</property>
</row>
<row>
<property name="text">
<string>7</string>
</property>
</row>
<row>
<property name="text">
<string>8</string>
</property>
</row>
<row>
<property name="text">
<string>9</string>
</property>
</row>
<row>
<property name="text">
<string>10</string>
</property>
</row>
<column>
<property name="text">
<string>姓名</string>
</property>
</column>
<column>
<property name="text">
<string>性别</string>
</property>
</column>
<column>
<property name="text">
<string>年龄</string>
</property>
</column>
<column>
<property name="text">
<string>学号</string>
</property>
</column>
<column>
<property name="text">
<string>学分</string>
</property>
</column>
</widget>
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>630</x>
<y>20</y>
<width>391</width>
<height>381</height>
</rect>
</property>
<property name="title">
<string>学生信息</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>361</width>
<height>291</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>姓名:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEditName"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>性别:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineEditSex"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>年龄:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEditAge"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>学号:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="lineEditNumber"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>学分:</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="lineEditGrade"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>330</y>
<width>361</width>
<height>25</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="createButton">
<property name="text">
<string>新增</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="changeButton">
<property name="text">
<string>修改</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="deleteButton">
<property name="text">
<string>删除</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="seekButton">
<property name="text">
<string>查询</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>36</x>
<y>350</y>
<width>501</width>
<height>25</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="refreshButton">
<property name="text">
<string>刷新/清空查询</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>160</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="previewButton">
<property name="text">
<string>上一页</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="pageEdit">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="nextButton">
<property name="text">
<string>下一页</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
information.h
#ifndef INFORMATION_H
#define INFORMATION_H
#include <QWidget>
#include <QDebug>
namespace Ui {
class Information;
}
class Information:public QWidget
{
Q_OBJECT
public:
Information(QWidget *parent = nullptr);
~Information();
QList<QString> searchList;
signals:
void signsearch(QList<QString> list);
private slots:
//响应确认
void buttonOk();
//响应取消
void buttonCancel();
private:
Ui::Information *ui;
void initConnect();
void searchInformation();
};
#endif // INFORMATION_H
information.cpp
#include "information.h"
#include "ui_information.h"
Information::Information(QWidget *parent):QWidget(parent),ui(new Ui::Information)
{
ui->setupUi(this);
initConnect();
}
Information::~Information()
{
delete ui;
}
void Information::buttonOk()
{
searchInformation();
emit signsearch(searchList);
this->close();
}
void Information::buttonCancel()
{
this->close();
}
void Information::initConnect()
{
connect(ui->okButton,SIGNAL(clicked()),this,SLOT(buttonOk()));
connect(ui->cancelButton,SIGNAL(clicked()),this,SLOT(buttonCancel()));
}
void Information::searchInformation()
{
QList<QString> t_searchList;
t_searchList<<ui->lineEditName->text()<<ui->lineEditSex->text()<<ui->lineEditAge->text()<<ui->lineEditNumber->text()<<ui->lineEditGrade->text();
searchList = t_searchList;
}
information.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Information</class>
<widget class="QWidget" name="Information">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>444</width>
<height>250</height>
</rect>
</property>
<property name="windowTitle">
<string>查询</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>学生信息查询</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>性别:</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>姓名:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEditName"/>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEditAge"/>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="lineEditNumber"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>年龄:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineEditSex"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>学号:</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>学分:</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="lineEditGrade"/>
</item>
</layout>
</item>
<item row="1" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>1</number>
</property>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="okButton">
<property name="text">
<string>确认</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cancelButton">
<property name="text">
<string>取消</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>