1 Star 0 Fork 2

Sugarscat/EmployeeManagement-for-Qt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
frmemplist.cpp 7.17 KB
一键复制 编辑 原始数据 按行查看 历史
Sugarscat 提交于 2022-07-18 16:18 . main
#include "frmemplist.h"
#include "ui_frmemplist.h"
#include "employeecontainer.h"
#include "frmupdate.h"
FrmEmpList::FrmEmpList(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::FrmEmpList)
{
ui->setupUi(this);
//让table使用我们自定义的menu
connect(ui->tblList, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
showData();
}
//读取本地数据
void FrmEmpList::readData()
{
ifstream ifs("Employee_list.txt", ios::in);
if (ifs)
{
vector <Employee*>().swap(*EmployeeContainer::employeelist);
string line;
int a = 1;
int id;
string name;
string phone;
string depart;
string s;
while (ifs >> s) {
if (a%4 == 1) {
id = atoi(s.c_str());
}
else if (a%4 == 2) {
name = s;
}
else if (a%4 == 3) {
phone = s;
}
else if (a%4 == 0) {
depart = s;
Employee *emp = new Employee(id, name, phone, depart);
EmployeeContainer::employeelist->push_back(emp);
}
++a;
}
QMessageBox::information(this, "提示", "读取成功!");
}
else
{
//无数据时,提示报错
QMessageBox::critical(this, "提示", "无本地数据!");
}
ifs.close();
}
FrmEmpList::~FrmEmpList()
{
delete ui;
}
void FrmEmpList::findOne()
{
ui->tblList->clear();
int j = 0;
string name = ui->letFind->text().toStdString();
for (int i = 0; i < EmployeeContainer::employeelist->size(); i++) {
Employee *emp = EmployeeContainer::employeelist->at(i);
//emp里的数据的类型是C++,将C++类型转成QString
if(emp->name == name)
{
QStringList header;
header << "编号" << "姓名" << "手机" << "部门";
ui->tblList->setHorizontalHeaderLabels(header);
ui->tblList->setRowCount(j+1);
ui->tblList->setItem(j, 0, new QTableWidgetItem(QString::number(emp->id)));
ui->tblList->setItem(j, 1, new QTableWidgetItem(QString::fromStdString(emp->name)));
ui->tblList->setItem(j, 2, new QTableWidgetItem(QString::fromStdString(emp->phone)));
ui->tblList->setItem(j, 3, new QTableWidgetItem(QString::fromStdString(emp->depart)));
place[j] = i;
j++;
}
}
}
void FrmEmpList::showData()
{
//构建一个表头
ui->tblList->clear();
QStringList header;
header << "编号" << "姓名" << "手机" << "部门";
ui->tblList->setHorizontalHeaderLabels(header);
ui->tblList->setRowCount(EmployeeContainer::employeelist->size());
for (int i = 0; i < EmployeeContainer::employeelist->size(); i++) {
Employee *emp = EmployeeContainer::employeelist->at(i);
//emp里的数据的类型是C++,将C++类型转成QString
ui->tblList->setItem(i, 0, new QTableWidgetItem(QString::number(emp->id)));
ui->tblList->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(emp->name)));
ui->tblList->setItem(i, 2, new QTableWidgetItem(QString::fromStdString(emp->phone)));
ui->tblList->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(emp->depart)));
}
}
void FrmEmpList::saveData()
{
if(QMessageBox::Yes == QMessageBox::question(this, "保存提示", "本地已有数据,是否覆盖?", QMessageBox::Yes | QMessageBox::No)){
ofstream ofs("Employee_list.txt", ios::out);
for (int i = 0;i < EmployeeContainer::employeelist->size(); i++)
{
Employee *emp = EmployeeContainer::employeelist->at(i);
string id = to_string(emp->id);
history_one = id + " " + emp->name + " " + emp->phone + " " + emp->depart;
ofs << history_one << endl;
}
QMessageBox::information(this, "提示", "保存成功!");
ofs.close();
}
}
void FrmEmpList::showMenu(const QPoint pos)
{
QMenu *menu = new QMenu(ui->tblList);
QAction *actDelete = new QAction("删除", ui->tblList);
QAction *actChange = new QAction("修改", ui->tblList);
menu->addAction(actDelete);
menu->addAction(actChange);
// 将鼠标移到光标位置
menu->move(cursor().pos());
menu->show();
//获取用户点了第几行
QModelIndex index = ui->tblList->indexAt(pos);
//取得行号
int row = index.row();
//处理action的功能,让actDelete控件与函数绑定
//删除用户点击的行
if(!isFind){
connect(actDelete, &QAction::triggered, [=](){
//确认用户是否删除
if(QMessageBox::Yes == QMessageBox::question(this, "删除提示", "确定删除嘛?", QMessageBox::Yes | QMessageBox::No)){
if(row >= 0){
//通过迭代器和row删除
EmployeeContainer::employeelist->erase(EmployeeContainer::employeelist->begin() + row);
QMessageBox::information(this, "提示", "删除成功!");
showData();//刷新数据
}else{
QMessageBox::critical(this, "提示", "未选中任何数据!");
}
}
});
//修改
connect(actChange, &QAction::triggered, [=](){
//确认用户是否删除
if(row >= 0){
//通过迭代器和row
FrmUpdate *u = new FrmUpdate(row);
u->show();
showData();//刷新数据
}else{
QMessageBox::critical(this, "提示", "未选中任何数据!");
}
});
showData();//刷新数据
}
else
{
connect(actDelete, &QAction::triggered, [=](){
//确认用户是否删除
if(QMessageBox::Yes == QMessageBox::question(this, "删除提示", "确定删除嘛?", QMessageBox::Yes | QMessageBox::No)){
if(row >= 0){
EmployeeContainer::employeelist->erase(EmployeeContainer::employeelist->begin() + place[row]);
QMessageBox::information(this, "提示", "删除成功!");
findOne();//刷新数据
}else{
QMessageBox::critical(this, "提示", "未选中任何数据!");
}
}
});
//修改
connect(actChange, &QAction::triggered, [=](){
//确认用户是否删除
if(row >= 0){
//通过迭代器和row
FrmUpdate *u = new FrmUpdate(place[row]);
u->show();
findOne();//刷新数据
}else{
QMessageBox::critical(this, "提示", "未选中任何数据!");
}
});
findOne();//刷新数据
}
}
void FrmEmpList::on_btnSearch_clicked()
{
isFind = true;
findOne();
}
void FrmEmpList::on_pushButton_clicked()
{
isFind = false;
showData();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Sugarscat/EmployeeManagement-for-Qt.git
[email protected]:Sugarscat/EmployeeManagement-for-Qt.git
Sugarscat
EmployeeManagement-for-Qt
EmployeeManagement-for-Qt
main

搜索帮助