#include "mytableview.h"
#include <QFileDialog>
#include<QMessageBox>
#include <QtAlgorithms> //实现排序
#include <iconv.h>
#include <windows.h>
myTableView::myTableView()
{
//初始化插入数据
this->setColumnCount(6); //设置列数为5
this->setRowCount(0); //设置行数为10
QStringList header;
header<<"学号"<<"姓名"<<"性别"<<"出生年日"<<"籍贯"<<"住址";
this->setHorizontalHeaderLabels(header);
stu_number = 0; //初始化学生信息
this->is_modify = false; //未曾修改
this->is_loaded = false; //判断是否是被加载
loaded_file.clear();
this->temp_dialog = nullptr;
this->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置为不可编辑
this->show();
stus.clear(); //清空这个数据结构
}
myTableView::~myTableView()
{
//系统自动进行清空操作
}
void myTableView::add_info()
{
this->temp_dialog = new Dialog();
this->temp_dialog->exec();
if(this->temp_dialog->is_ok_toadd() == true)//可以增加
{
info_of_student temp = this->temp_dialog->get_newinput();
bool is_exist = false;
for(int i = 0; i < this->stus.size(); i++)
{
if(this->stus[i]._Id == temp._Id)
{
is_exist = true;
} //学号相同无法添加
}
if(is_exist == false)
{
this->insertRow(stu_number);
this->setItem(stu_number,0,new QTableWidgetItem(temp._Id));
this->setItem(stu_number,1,new QTableWidgetItem(temp._Name));
this->setItem(stu_number,2,new QTableWidgetItem(temp._Sex));
this->setItem(stu_number,3,new QTableWidgetItem(temp._Birth));
this->setItem(stu_number,4,new QTableWidgetItem(temp._Birthplace));
this->setItem(stu_number,5,new QTableWidgetItem(temp._Address));
this->stus.push_back(temp); //存进入temp
//增加一行
stu_number++;
qDebug()<<stu_number<<" "<<this->rowCount();
is_modify = true; //被修改了
}
else
{
QMessageBox::warning(nullptr,tr("警告"),tr("学号相同无法添加!"),QMessageBox::Ok);
}
}
}
void myTableView::delete_info(int cur)
{
auto a = stus.begin();
for(int i = 0; i < this->stus.size(); i++)
{
if(this->item(cur,0)->text() == stus[i]._Id)
{
break;
}
a++;
}
this->stus.erase(a); //清除这一行数据
this->removeRow(cur);
if(stu_number >= 1)
{
is_modify = true; //被修改了
stu_number--;
}
}
void myTableView::modify_info(int cur) //修改信息
{
this->temp_dialog = new Dialog();
//获取当前信息
info_of_student temp;
temp._Id = this->item(cur,0)->text();
temp._Name = this->item(cur,1)->text();
temp._Sex = this->item(cur,2)->text();
temp._Birth = this->item(cur,3)->text();
temp._Birthplace = this->item(cur,4)->text();
temp._Address = this->item(cur,5)->text();
QString save_ID = temp._Id; //保存下来的save
this->temp_dialog->set_info(temp);
this->temp_dialog->exec();
if(this->temp_dialog->is_ok_toadd() == true)//可以增加
{
info_of_student temp = this->temp_dialog->get_newinput();
if(temp._Id == save_ID) //没有改变学号
{
this->setItem(cur,0,new QTableWidgetItem(temp._Id));
this->setItem(cur,1,new QTableWidgetItem(temp._Name));
this->setItem(cur,2,new QTableWidgetItem(temp._Sex));
this->setItem(cur,3,new QTableWidgetItem(temp._Birth));
this->setItem(cur,4,new QTableWidgetItem(temp._Birthplace));
this->setItem(cur,5,new QTableWidgetItem(temp._Address));
auto a = stus.begin();
for(int i = 0; i < this->stus.size(); i++)
{
if(this->item(cur,0)->text() == stus[i]._Id)
{
break;
}
a++;
}
*a = temp;
}
else //改变了学号
{
bool is_exist = false;
for(int i = 0; i < this->stus.size(); i++)
{
if(this->stus[i]._Id == temp._Id && temp._Id != save_ID)
{
is_exist = true;
} //学号相同无法添加
}
if(is_exist == false)
{
this->setItem(cur,0,new QTableWidgetItem(temp._Id));
this->setItem(cur,1,new QTableWidgetItem(temp._Name));
this->setItem(cur,2,new QTableWidgetItem(temp._Sex));
this->setItem(cur,3,new QTableWidgetItem(temp._Birth));
this->setItem(cur,4,new QTableWidgetItem(temp._Birthplace));
this->setItem(cur,5,new QTableWidgetItem(temp._Address));
auto a = stus.begin();
for(int i = 0; i < this->stus.size(); i++)
{
if(this->item(cur,0)->text() == stus[i]._Id)
{
break;
}
a++;
}
*a = temp;
}
else
{
QMessageBox::warning(nullptr,tr("警告"),tr("学号相同无法修改!"),QMessageBox::Ok);
}
}
is_modify = true; //被修改了
}
}
void myTableView::sort_table(int row_num)
{
is_modify = true; //被修改了
if(row_num == 0)
{
this->sortItems(row_num);
}
else if(row_num == 1)
{
this->sortItems(row_num,Qt::SortOrder::AscendingOrder);
}
}
void myTableView::saveTable(int way)
{
QString fileName;
if(way == 0) //表示保存
{
if(this->loaded_file.isEmpty() == true)
{
fileName = QFileDialog::getSaveFileName(nullptr,tr("保存文件"),
tr("/root/Projects/product/bin/config/未命名.txt"),
tr("file(*.txt )"));
loaded_file = fileName; //记录下表格所存在的地址
}
else
{
fileName = loaded_file;
}
}
else if(way == 1) //表示另存为
{
fileName = QFileDialog::getSaveFileName(nullptr,tr("保存文件"),
tr("/root/Projects/product/bin/config/未命名.txt"),
tr("file(*.txt )"));
if(this->loaded_file.isEmpty() == true) loaded_file = fileName; //记录下表格所存在的地址
}
if(!fileName.isEmpty())
{
//将表格数据写入文件
if(QFileInfo(fileName).suffix().isEmpty())
{
fileName.append(".xls");
}
QFile file;
file.setFileName(fileName);
//打开文件
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox msg;
msg.setText("打开文件失败!");
msg.exec();
}
else
{
this->is_modify = false;
qDebug("open file success:::");
QTextStream out(&file);
int rowCount = this->rowCount();
int colCount = this->columnCount();
//将表头写入文件
qDebug("saveTable start:::");
//读取单元格的数据并写入文件
qDebug()<<"row count:::"<<rowCount<<"col count:::"<<colCount;
for(int row = 0; row < rowCount; row++)
{
for(int col = 0; col < colCount; col++)
{
QString text = this->item(row,col)->text();
qDebug()<<"text:::"<<text;
if(text.isEmpty())
{
out << "null" << "\t";
}
else
高程大作业学生管理系统,QT C++.zip
版权申诉
48 浏览量
2023-09-23
19:46:09
上传
评论 1
收藏 913KB ZIP 举报
学术菜鸟小晨
- 粉丝: 1w+
- 资源: 4953
最新资源
- 笔记实验六,spark,大数据分析
- ####蓝桥杯python的详细的信息介绍
- 电子万年历软件仿真(经过多次修改,保证正确性)
- Unity XR 手势射击控制脚本(适用于任何可手势识别的设备)
- 机械设计全自动电表(NB和IC卡表)控制和上壳装配线sw16可编辑非常好的设计图纸100%好用.zip
- 基于matlab的EAN-13条形码识别系统GUI界面.zip代码53
- matlab基于bp神经网络交通信号标志识别GUI界面13个标志.zip代码54
- 电子万年历答辩实物展示视频mp4格式
- 基于python实现的程序,包括哈希感知算法cvHash,图像切割cvsplit,固定目标检测cvRec(附文档ppt)等
- 计算0-10000之间所有偶数的和
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈