简述
本来说这个博客短时间内不会断更的,但是因为本身自己的项目比较赶后续就缺少更新了,同时也不知道该展示写啥了,自己项目中的很多东西因为牵扯到保密的问题,很多东西不能发到博客上,所以这个博客的主旨依旧设定为給各位喜欢编程的朋友做个入门的例子,可以参考学习
程序功能
根据自己的项目经验,把这个系统的主要四个功能提炼了出来,主要包括三块,第一块事GUI的设计,第大块文本的写,第三块业务逻辑的实现。这里面主要实现了增删改查四个功能,查询采用了多方面的模糊查询,数据展示用了tablewidget控件。
代码段
增加学生信息
InfoDlg info;
info.setState(0);
QObject::connect(this,&MainWindow::sigShowResult,&info,&InfoDlg::showResult);
QObject::connect(&info,&InfoDlg::sigInfo,this,[=](int state,QString id,QString name,int sex,QString brithday,QString major,int schoolyear){
if(state == 0)
{
QMap<QString,Student>::iterator itor = m_userMap.find(id);
if(itor!=m_userMap.end())
{
emit sigShowResult(false);
}
else
{
Student stu;
stu.id = id;
stu.sex = sex;
stu.name = name;
stu.brithday = brithday;
stu.major = major;
stu.schoolyear = schoolyear;
m_userMap.insert(id,stu);
emit sigShowResult(true);
}
}
});
info.exec();
InitTable();
删除学生信息
InfoDlg info;
info.setState(2);
QObject::connect(this,&MainWindow::sigSetInfo,&info,&InfoDlg::setInfo);
QObject::connect(&info,&InfoDlg::sigGetUser,this,[=](QString id){
QMap<QString,Student>::iterator itor = m_userMap.find(id);
if(itor!=m_userMap.end())
{
emit sigSetInfo(itor->name,itor->sex,itor->brithday,itor->major,itor->schoolyear);
}
});
info.initCombox(getUserList());
QObject::connect(this,&MainWindow::sigShowResult,&info,&InfoDlg::showResult);
QObject::connect(&info,&InfoDlg::sigInfo,this,[=](int state,QString id,QString name,int sex,QString brithday,QString major,int schoolyear){
if(state == 2)
{
QMap<QString,Student>::iterator itor = m_userMap.find(id);
if(itor!=m_userMap.end())
{
m_userMap.remove(id);
emit sigShowResult(true);
}
else
{
emit sigShowResult(false);
}
}
});
info.exec();
InitTable();
修改学生信息
InfoDlg info;
info.setState(1);
QObject::connect(this,&MainWindow::sigSetInfo,&info,&InfoDlg::setInfo);
QObject::connect(&info,&InfoDlg::sigGetUser,this,[=](QString id){
QMap<QString,Student>::iterator itor = m_userMap.find(id);
if(itor!=m_userMap.end())
{
emit sigSetInfo(itor->name,itor->sex,itor->brithday,itor->major,itor->schoolyear);
}
});
info.initCombox(getUserList());
QObject::connect(this,&MainWindow::sigShowResult,&info,&InfoDlg::showResult);
QObject::connect(&info,&InfoDlg::sigInfo,this,[=](int state,QString id,QString name,int sex,QString brithday,QString major,int schoolyear){
if(state == 1)
{
QMap<QString,Student>::iterator itor = m_userMap.find(id);
if(itor==m_userMap.end())
{
emit sigShowResult(false);
}
else
{
Student stu;
stu.id = id;
stu.sex = sex;
stu.name = name;
stu.brithday = brithday;
stu.major = major;
stu.schoolyear = schoolyear;
m_userMap[id] = stu;
emit sigShowResult(true);
}
}
});
info.exec();
InitTable();
查询学生信息
if(arg1 == "")
{
InitTable();
}
else
{
QVector<Student> vstu;
QMap<QString,Student>::iterator itor = m_userMap.begin();
while(itor!=m_userMap.end())
{
if(itor->id.contains(arg1)||itor->name.contains(arg1)||QString::number(itor->schoolyear).contains(arg1)||itor->major.contains(arg1))
{
vstu.push_back(itor.value());
}
itor++;
}
ui->tableWidget->clear();
ui->tableWidget->setRowCount(vstu.size());
QStringList header;
header<<"学号"<<"姓名"<<"性别"<<"出生年月"<<"专业"<<"学年";
ui->tableWidget->setHorizontalHeaderLabels(header);
for(int row = 0;row <vstu.size();row++)
{
ui->tableWidget->setItem(row,0,new QTableWidgetItem(vstu[row].id));
ui->tableWidget->setItem(row,1,new QTableWidgetItem(vstu[row].name));
QString sex ="男";
if(vstu[row].sex==0)
{
sex = "女";
}
ui->tableWidget->setItem(row,2,new QTableWidgetItem(sex));
ui->tableWidget->setItem(row,3,new QTableWidgetItem(vstu[row].brithday));
ui->tableWidget->setItem(row,4,new QTableWidgetItem(vstu[row].major));
ui->tableWidget->setItem(row,5,new QTableWidgetItem(QString::number(vstu[row].schoolyear)));
ui->tableWidget->item(row,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->tableWidget->item(row,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->tableWidget->item(row,2)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->tableWidget->item(row,3)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->tableWidget->item(row,4)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
ui->tableWidget->item(row,5)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}
}
GUI基本样式设计
setWindowFlags(Qt::FramelessWindowHint); //设置窗口无边框
setWindowTitle("学生信息管理系统");
setAttribute(Qt::WA_TranslucentBackground);
ui->widget->setAttribute(Qt::WA_StyledBackground, true);
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
//设置阴影距离
shadow->setOffset(0, 0);
//设置阴影颜色
//#9196A3
shadow->setColor(QColor(145,150,163,100));
//设置阴影圆角
shadow->setBlurRadius(19);
ui->widget->setGraphicsEffect(shadow);
ui->widget->setStyleSheet("#widget{background-color: qlineargradient(spread:pad, x1:0.489, y1:1, x2:0.432, y2:0.0851364, stop:0 rgba(62, 141, 244, 255), stop:1 rgba(240, 240, 244, 255));border-radius:15px;}");
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //禁止修改
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); //设置为可以选中单个
ui->tableWidget->verticalHeader()->setVisible(false); //隐藏列表头
ui->tableWidget->setAlternatingRowColors(true); // 隔行变色
ui->tableWidget->setPalette(QPalette(QColor("#c0c0c0"))); // 设置隔行变色的颜色 gray灰色
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableWidget->setColumnCount(6);
LoadFile();
InitTable();
ui->btn_insert->setIcon(QIcon(":/res/insert.png"));
ui->btn_delete->setIcon(QIcon(":/res/delete.png"));
ui->btn_update->setIcon(QIcon(":/res/update.png"));
ui->btn_close->setStyleSheet("QPushButton{border-image:url(:/res/closeg.png)}"
"QPushButton:hover{border-image: url(:/res/close.png);}"
"QPushButton:pressed{border-image: url(:/res/close.png}");
ui->btn_insert->setStyleSheet("QPushButton{border-radius:5px;background-color:rgb(0,0,0,0);border:1px solid gray;}"
"QPushButton:hover{border-radius:5px;background-color:rgb(153,204,51,50);border:1px solid gray;}"
"QPushButton:hover{border-radius:5px;background-color:rgb(153,204,51,50);border:1px solid gray;}");
ui->btn_delete->setStyleSheet("QPushButton{border-radius:5px;background-color:rgb(0,0,0,0);border:1px solid gray;}"
"QPushButton:hover{border-radius:5px;background-color:rgb(153,204,51,50);border:1px solid gray;}"
"QPushButton:hover{border-radius:5px;background-color:rgb(153,204,51,50);border:1px solid gray;}");
ui->btn_update->setStyleSheet("QPushButton{border-radius:5px;background-color:rgb(0,0,0,0);border:1px solid gray;}"
"QPushButton:hover{border-radius:5px;background-color:rgb(153,204,51,50);border:1px solid gray;}"
"QPushButton:hover{border-radius:5px;background-color:rgb(153,204,51,50);border:1px solid gray;}");
QPalette pal;
pal.setColor(QPalette::Background,QColor("#F4F7FC"));
setPalette(pal);
QPalette pal1 = ui->lineEdit_select->palette();
pal1.setColor(QPalette::PlaceholderText,QColor("#FF0000"));
ui->lineEdit_select->setStyleSheet("border:0px groove gray;border-radius:5px;padding:2px 4px;color:gray;"); //设置编辑框圆角
ui->lineEdit_select->setPlaceholderText(tr("请输入用户名/学号/学年/专业、进行查询"));
QAction *pTrailingAction = new QAction(this);
pTrailingAction->setIcon(QIcon(":/res/select.png"));
ui->lineEdit_select->addAction(pTrailingAction, QLineEdit::TrailingPosition);
文件读写
void MainWindow::LoadFile()
{
QFile m_file("user.dat");
int n;
m_userMap.clear();
if(m_file.open(QIODevice::ReadOnly))
{
QTextStream in(&m_file);
in>>n;
for(int i =0;i<n;i++)
{
Student stu;
in>>stu.id>>stu.name>>stu.sex>>stu.major>>stu.brithday>>stu.schoolyear;
if(stu.id != "")
m_userMap.insert(stu.id,stu);
}
m_file.close();
}
}
void MainWindow::SaveFile()
{
QFile m_file("user.dat");
if (!m_file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream aStream(&m_file); //用文本流读取文件
aStream<<m_userMap.size()<<endl;
QMap<QString,Student>::iterator itor = m_userMap.begin();
while(itor!=m_userMap.end())
{
aStream<<itor->id<<" "<<itor->name<<" "<<itor->sex<<" "<<itor->major<<" "<<itor->brithday<<" "<<itor->schoolyear<<endl;
itor++;
}
m_file.close();
}
程序运行截图
项目地址
https://download.csdn.net/download/qq_39625143/85218643
后记
如果没有积分可以联系博主留言邮箱博主看到会发送