update 添加部分UI 完善处理逻辑

main
wang-qin928 3 weeks ago
parent fa7c9d4e85
commit 0d0cfa66fc

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2025-12-18T17:41:46. --> <!-- Written by QtCreator 4.11.1, 2025-12-22T16:45:06. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
@ -73,14 +73,14 @@
<data> <data>
<variable>ProjectExplorer.Project.Target.0</variable> <variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap"> <valuemap type="QVariantMap">
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MinGW 32-bit</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MinGW 32-bit</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win32_mingw73_kit</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win64_mingw73_kit</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value> <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value> <value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value> <value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0"> <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/UGit/build-StudentManager-Desktop_Qt_5_14_2_MinGW_32_bit-Debug</value> <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_64_bit-Debug</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@ -128,7 +128,7 @@
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value> <value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
</valuemap> </valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1"> <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_32_bit-Release</value> <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_64_bit-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@ -176,7 +176,7 @@
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value> <value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value>
</valuemap> </valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2"> <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_32_bit-Profile</value> <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_64_bit-Profile</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@ -296,7 +296,6 @@
</valuelist> </valuelist>
<value type="int" key="PE.EnvironmentAspect.Base">2</value> <value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/> <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">StudentManager2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/UGit/StudentManager/StudentManager.pro</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/UGit/StudentManager/StudentManager.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">D:/UGit/StudentManager/StudentManager.pro</value> <value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">D:/UGit/StudentManager/StudentManager.pro</value>
<value type="QString" key="RunConfiguration.Arguments"></value> <value type="QString" key="RunConfiguration.Arguments"></value>
@ -309,7 +308,7 @@
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value> <value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value> <value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value> <value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">D:/UGit/build-StudentManager-Desktop_Qt_5_14_2_MinGW_32_bit-Debug</value> <value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_64_bit-Release</value>
</valuemap> </valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value> <value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap> </valuemap>
@ -317,14 +316,14 @@
<data> <data>
<variable>ProjectExplorer.Project.Target.1</variable> <variable>ProjectExplorer.Project.Target.1</variable>
<valuemap type="QVariantMap"> <valuemap type="QVariantMap">
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MinGW 32-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MinGW 32-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win64_mingw73_kit</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win32_mingw73_kit</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value> <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value> <value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value> <value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0"> <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_64_bit-Debug</value> <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/UGit/build-StudentManager-Desktop_Qt_5_14_2_MinGW_32_bit-Debug</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@ -372,7 +371,7 @@
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value> <value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
</valuemap> </valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1"> <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_64_bit-Release</value> <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/UGit/build-StudentManager-Desktop_Qt_5_14_2_MinGW_32_bit-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@ -420,7 +419,7 @@
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value> <value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value>
</valuemap> </valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2"> <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_64_bit-Profile</value> <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_32_bit-Profile</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@ -540,6 +539,7 @@
</valuelist> </valuelist>
<value type="int" key="PE.EnvironmentAspect.Base">2</value> <value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/> <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">StudentManager2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/UGit/StudentManager/StudentManager.pro</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/UGit/StudentManager/StudentManager.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">D:/UGit/StudentManager/StudentManager.pro</value> <value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">D:/UGit/StudentManager/StudentManager.pro</value>
<value type="QString" key="RunConfiguration.Arguments"></value> <value type="QString" key="RunConfiguration.Arguments"></value>
@ -552,7 +552,7 @@
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value> <value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value> <value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value> <value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/wq/Desktop/build-StudentManager-Desktop_Qt_5_14_2_MinGW_64_bit-Debug</value> <value type="QString" key="RunConfiguration.WorkingDirectory.default"></value>
</valuemap> </valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value> <value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap> </valuemap>

@ -4,18 +4,19 @@
#include <QInputDialog> #include <QInputDialog>
#include <QMessageBox> #include <QMessageBox>
#include <QHeaderView> #include <QHeaderView>
#include <QLabel>
#include <QMenu> #include <QMenu>
#include <QDebug>
#include <QMenuBar> #include <QMenuBar>
//#include <QFile> #include <QFile>
//#include <QTextStream> #include <QTextStream>
//#include <QFileDialog> #include <QFileDialog>
#include <QFileInfo>
MainWindow::MainWindow(QWidget *parent) #include <QToolBar>
: QMainWindow(parent) #include <QToolButton>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{ {
setupUI(); setupUI();
setWindowTitle("学生信息管理系统");
} }
MainWindow::~MainWindow() {} MainWindow::~MainWindow() {}
@ -25,232 +26,317 @@ void MainWindow::setupUI()
QWidget *central = new QWidget(this); QWidget *central = new QWidget(this);
setCentralWidget(central); setCentralWidget(central);
// 顶部菜单栏 // Action 定义
QMenuBar *menuBar = this->menuBar(); QAction *actImport = new QAction("导入", this);
QAction *actSave = new QAction("保存", this);
fileMenu = menuBar->addMenu("文件(&F)"); // 快捷键 alt + F
editMenu = menuBar->addMenu("编辑(&E)"); // 快捷键 alt + E QAction *actAddCls = new QAction("新增班级", this);
// TODO QAction *actDelCls = new QAction("删除班级", this);
QAction *actRenCls = new QAction("重命名班级", this);
// 左侧班级树控件
// 工具栏(唯一一行)
QToolBar *toolBar = addToolBar("主工具栏");
toolBar->setMovable(false);
toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
// 默认显示“文件”相关
toolBar->addAction(actImport);
toolBar->addAction(actSave);
// 伪菜单栏按钮
QToolButton *fileBtn = new QToolButton(this);
fileBtn->setText("文件");
fileBtn->setToolButtonStyle(Qt::ToolButtonTextOnly);
fileBtn->setAutoRaise(true); // 看起来像菜单
QToolButton *editBtn = new QToolButton(this);
editBtn->setText("编辑");
editBtn->setToolButtonStyle(Qt::ToolButtonTextOnly);
editBtn->setAutoRaise(true);
// 放到菜单栏位置
QWidget *menuWidget = new QWidget(this);
QHBoxLayout *menuLayout = new QHBoxLayout(menuWidget);
menuLayout->setContentsMargins(6, 0, 0, 0);
menuLayout->setSpacing(10);
menuLayout->addWidget(fileBtn);
menuLayout->addWidget(editBtn);
menuLayout->addStretch();
menuBar()->setCornerWidget(menuWidget, Qt::TopLeftCorner);
// 按钮点击 → 切换工具栏
connect(fileBtn, &QToolButton::clicked, this, [=]() {
toolBar->clear();
toolBar->addAction(actImport);
toolBar->addAction(actSave);
});
connect(editBtn, &QToolButton::clicked, this, [=]() {
toolBar->clear();
toolBar->addAction(actAddCls);
toolBar->addAction(actDelCls);
toolBar->addAction(actRenCls);
});
// 核心控件初始化
tree = new QTreeWidget; tree = new QTreeWidget;
tree->setHeaderLabel("班级列表"); tree->setHeaderLabel("班级列表");
tree->setContextMenuPolicy(Qt::CustomContextMenu);
// 显示学生信息控件 table = new QTableWidget(0, 5);
table = new QTableWidget;
table->setColumnCount(5);
table->setHorizontalHeaderLabels({"姓名", "性别", "年龄", "学号", "电话"}); table->setHorizontalHeaderLabels({"姓名", "性别", "年龄", "学号", "电话"});
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
// 输入学生信息控件 // 右侧编辑面板
nameEdit = new QLineEdit; nameEdit = new QLineEdit;
genderEdit = new QLineEdit; genderEdit = new QLineEdit;
ageEdit = new QLineEdit; ageEdit = new QLineEdit;
idEdit = new QLineEdit; idEdit = new QLineEdit;
phoneEdit = new QLineEdit; phoneEdit = new QLineEdit;
addBtn = new QPushButton("新增学生"); addBtn = new QPushButton("新增");
delBtn = new QPushButton("删除学生"); delBtn = new QPushButton("删除");
updateBtn = new QPushButton("修改");
searchBtn = new QPushButton("查询");
refreshBtn = new QPushButton("刷新/清空查询");
// 布局 QVBoxLayout *rightLayout = new QVBoxLayout;
QVBoxLayout *rightLayout = new QVBoxLayout; // 垂直布局 rightLayout->addWidget(new QLabel("姓名:"));
rightLayout->addWidget(new QLabel("姓名")); // index 0 rightLayout->addWidget(nameEdit);
rightLayout->addWidget(nameEdit); // index 1 rightLayout->addWidget(new QLabel("性别:"));
rightLayout->addWidget(new QLabel("性别"));
rightLayout->addWidget(genderEdit); rightLayout->addWidget(genderEdit);
rightLayout->addWidget(new QLabel("年龄")); rightLayout->addWidget(new QLabel("年龄:"));
rightLayout->addWidget(ageEdit); rightLayout->addWidget(ageEdit);
rightLayout->addWidget(new QLabel("学号")); rightLayout->addWidget(new QLabel("学号:"));
rightLayout->addWidget(idEdit); rightLayout->addWidget(idEdit);
rightLayout->addWidget(new QLabel("电话")); rightLayout->addWidget(new QLabel("电话:"));
rightLayout->addWidget(phoneEdit); rightLayout->addWidget(phoneEdit);
rightLayout->addWidget(addBtn); rightLayout->addWidget(addBtn);
rightLayout->addWidget(updateBtn);
rightLayout->addWidget(delBtn); rightLayout->addWidget(delBtn);
rightLayout->addStretch(); // 设置可伸缩对齐 rightLayout->addWidget(searchBtn);
rightLayout->addWidget(refreshBtn);
QHBoxLayout *mainLayout = new QHBoxLayout(central); // 水平布局 rightLayout->addStretch();
mainLayout->addWidget(tree, 1); // index 1
mainLayout->addWidget(table, 3); // index 3 // 分页控件
mainLayout->addLayout(rightLayout, 2); // index 2 btnPrev = new QPushButton("上一页");
btnNext = new QPushButton("下一页");
// 指定信号 槽 pageLabel = new QLabel("第 1/1 页");
// 右键点击弹出增删改班级窗口 QHBoxLayout *pageLayout = new QHBoxLayout;
connect(tree, &QTreeWidget::customContextMenuRequested, // 右键点击信号 pageLayout->addStretch();
this, &MainWindow::showTreeMenu); pageLayout->addWidget(btnPrev);
// 左键点击班级名 刷新中间的学生信息 pageLayout->addWidget(pageLabel);
connect(tree, &QTreeWidget::itemClicked, // 左键点击信号 pageLayout->addWidget(btnNext);
this, &MainWindow::onClassClicked); pageLayout->addStretch();
// 左键点击执行添加学生逻辑
connect(addBtn, &QPushButton::clicked, // 布局组合
this, &MainWindow::addStudent); QVBoxLayout *centerLayout = new QVBoxLayout;
// 左键点击执行删除学生逻辑 centerLayout->addWidget(table);
connect(delBtn, &QPushButton::clicked, centerLayout->addLayout(pageLayout);
this, &MainWindow::deleteStudent);
// 点击学生单元格 回显学生信息 QHBoxLayout *mainLayout = new QHBoxLayout(central);
connect(table, &QTableWidget::cellClicked, // 单元格点击信号 mainLayout->addWidget(tree, 1);
this, &MainWindow::tableItemClicked); mainLayout->addLayout(centerLayout, 3);
mainLayout->addLayout(rightLayout, 1);
// 确认窗口大小
resize(900, 600); // 信号槽连接
setMinimumSize(800, 500); connect(actAddCls, &QAction::triggered, this, &MainWindow::addClass);
connect(actDelCls, &QAction::triggered, this, &MainWindow::deleteClass);
connect(actRenCls, &QAction::triggered, this, &MainWindow::renameClass);
connect(actImport, &QAction::triggered, this, &MainWindow::importData);
connect(actSave, &QAction::triggered, this, &MainWindow::saveData);
connect(tree, &QTreeWidget::itemClicked, this, &MainWindow::onClassClicked);
connect(addBtn, &QPushButton::clicked, this, &MainWindow::addStudent);
connect(delBtn, &QPushButton::clicked, this, &MainWindow::deleteStudent);
connect(updateBtn, &QPushButton::clicked, this, &MainWindow::updateStudent);
connect(searchBtn, &QPushButton::clicked, this, &MainWindow::searchStudent);
connect(refreshBtn, &QPushButton::clicked, this, &MainWindow::refreshAll);
connect(table, &QTableWidget::cellClicked, this, &MainWindow::tableItemClicked);
connect(btnPrev, &QPushButton::clicked, this, &MainWindow::prevPage);
connect(btnNext, &QPushButton::clicked, this, &MainWindow::nextPage);
resize(1000, 600);
} }
void MainWindow::showTreeMenu(const QPoint &pos) // 业务逻辑实现
QString MainWindow::currentClass() const
{ {
// 右键点击 后触发这个函数 auto item = tree->currentItem();
QMenu menu; return item ? item->text(0) : "";
QAction *add = menu.addAction("新建班级");
QAction *del = menu.addAction("删除班级");
QAction *ren = menu.addAction("重命名班级");
// 显示右键菜单 并获取用户选择
QAction *ret = menu.exec(tree->viewport()->mapToGlobal(pos));
// 执行用户选择的操作
if (ret == add)
addClass();
else if (ret == del)
deleteClass();
else if (ret == ren)
renameClass();
} }
void MainWindow::addClass() void MainWindow::refreshTable()
{ {
// 弹出对话框 输入班级名称 table->setRowCount(0);
QString name = QInputDialog::getText(this, "新建班级", "班级名称"); int total = displayList.size();
if (name.isEmpty()) int maxPage = qMax(1, (total + pageSize - 1) / pageSize);
return; if (currentPage > maxPage)
// 检查班级是否已存在 currentPage = maxPage;
if (classData.contains(name))
pageLabel->setText(QString("第 %1/%2 页").arg(currentPage).arg(maxPage));
int start = (currentPage - 1) * pageSize;
int end = qMin(start + pageSize, total);
for (int i = start; i < end; ++i)
{ {
QMessageBox::warning(this, "错误", "班级已存在,请重新输入"); int r = table->rowCount();
return; table->insertRow(r);
table->setItem(r, 0, new QTableWidgetItem(displayList[i].name));
table->setItem(r, 1, new QTableWidgetItem(displayList[i].gender));
table->setItem(r, 2, new QTableWidgetItem(QString::number(displayList[i].age)));
table->setItem(r, 3, new QTableWidgetItem(displayList[i].id));
table->setItem(r, 4, new QTableWidgetItem(displayList[i].phone));
} }
// 将新班级添加到数据结构和树控件中
// 由于是新班级,初始化为空列表
classData[name] = {};
// 在树控件中添加新节点
// 数据与UI同步更新
tree->addTopLevelItem(new QTreeWidgetItem({name}));
} }
void MainWindow::deleteClass() void MainWindow::onClassClicked(QTreeWidgetItem *item)
{ {
// 获取当前选中班级 // auto == QTreeWidgetItem* displayList = classData[item->text(0)];
auto item = tree->currentItem(); currentPage = 1;
if (!item) refreshTable();
return;
QString name = item->text(0); // 获取选中项的班级名称
// 弹出确认对话框
if (QMessageBox::question(this, "确认", "确定删除班级?") == QMessageBox::Yes)
{
// 从数据结构和树控件中删除班级
classData.remove(name);
// 数据与UI同步更新
delete item; // 避免内存泄漏
table->clearContents(); // 清理学生信息内容
table->setRowCount(0); // 清空表格行数
}
QMessageBox::information(this, "提示", "班级已删除");
} }
void MainWindow::renameClass() void MainWindow::addStudent()
{ {
// 获取当前选中班级 QString cls = currentClass();
auto item = tree->currentItem(); if (cls.isEmpty())
if (!item)
return; return;
// 获取旧班级名称
QString oldName = item->text(0); Student s{nameEdit->text(), genderEdit->text(), ageEdit->text().toInt(), idEdit->text(), phoneEdit->text()};
// 弹出对话框 输入新名称 if (s.name.isEmpty() || s.id.isEmpty())
QString newName = QInputDialog::getText(this, "重命名", "新名称"); {
// 检查新名称有效性 QMessageBox::warning(this, "错误", "姓名和学号不能为空"); //
if (newName.isEmpty())
return; return;
if (classData.contains(newName)) }
QMessageBox::warning(this, "提示", "班级已存在");
// 更新数据结构和树控件
classData[newName] = classData.take(oldName); // 从数据结构中移除旧键值对,并返回旧名称关联的值,赋给新名称
item->setText(0, newName);
}
void MainWindow::onClassClicked(QTreeWidgetItem *item) // 查重
{ for (const auto &st : classData[cls])
// 用户点击班级节点后触发此函数 {
Q_UNUSED(item) // 避免未使用参数警告 if (st.id == s.id)
refreshTable(); // 刷新右侧学生表格内容 {
QMessageBox::warning(this, "错误", "学号重复");
return;
}
}
classData[cls].append(s);
saveClassToFile(cls); // 自动保存
// 刷新输入框内容
nameEdit->clear();
genderEdit->clear();
ageEdit->clear();
idEdit->clear();
phoneEdit->clear();
refreshAll();
} }
QString MainWindow::currentClass() const // 只读成员函数 void MainWindow::updateStudent()
{ {
// 获取当前选中班级名称 auto == QTreeWidgetItem* QString cls = currentClass();
auto item = tree->currentItem(); int row = table->currentRow();
return item ? item->text(0) : ""; if (cls.isEmpty() || row < 0)
return;
int realIdx = (currentPage - 1) * pageSize + row;
Student &s = classData[cls][realIdx];
s.name = nameEdit->text();
s.gender = genderEdit->text();
s.age = ageEdit->text().toInt();
s.phone = phoneEdit->text();
saveClassToFile(cls);
refreshAll();
} }
void MainWindow::refreshTable() void MainWindow::deleteStudent()
{ {
QString cls = currentClass(); QString cls = currentClass();
table->setRowCount(0); // 先清空表格内容 int row = table->currentRow();
if (!classData.contains(cls)) if (row < 0 || QMessageBox::question(this, "确认", "确定删除?") != QMessageBox::Yes)
return; // 班级不存在 return;
auto &list = classData[cls]; // 获取当前班级的学生列表引用 int realIdx = (currentPage - 1) * pageSize + row;
table->setRowCount(list.size()); // 设置表格行数为学生数量 classData[cls].removeAt(realIdx);
for (int i = 0; i < list.size(); ++i) saveClassToFile(cls);
{ refreshAll();
// 遍历学生列表 填充表格内容
table->setItem(i, 0, new QTableWidgetItem(list[i].name));
table->setItem(i, 1, new QTableWidgetItem(list[i].gender));
table->setItem(i, 2, new QTableWidgetItem(QString::number(list[i].age)));
table->setItem(i, 3, new QTableWidgetItem(list[i].id));
table->setItem(i, 4, new QTableWidgetItem(list[i].phone));
}
} }
void MainWindow::addStudent() void MainWindow::searchStudent()
{ {
QString cls = currentClass(); QString cls = currentClass();
if (cls.isEmpty()) if (cls.isEmpty())
{
QMessageBox::warning(this, "提示", "请先在左侧选择一个班级再进行查询");
return; return;
}
Student s; // 1. 获取所有输入框的关键字,并去掉首尾空格
s.name = nameEdit->text(); QString nameKw = nameEdit->text().trimmed();
s.gender = genderEdit->text(); QString genderKw = genderEdit->text().trimmed();
s.id = idEdit->text(); QString ageKw = ageEdit->text().trimmed();
s.phone = phoneEdit->text(); QString idKw = idEdit->text().trimmed();
QString phoneKw = phoneEdit->text().trimmed();
// 处理年龄用toInt的第二个参数判断是否为有效数字 displayList.clear();
bool isAgeValid; // 检测是否转换成功
int age = ageEdit->text().toInt(&isAgeValid);
if (!isAgeValid || age <= 0 || age > 120) // 年龄有效条件:转换成功 + 年龄在合理范围 // 2. 遍历当前班级的所有学生
for (const auto &s : classData[cls])
{ {
QMessageBox::warning(this, "错误", "年龄必须是1-120的整数"); // 初始假设该学生符合条件
return; bool isMatch = true;
}
s.age = age; // 只有年龄有效,才赋值给结构体 // 3. 逐个字段进行条件校验(逻辑:如果输入框不为空,则必须匹配该条件)
// qDebug() << s.name << s.id << s.age << s.phone << s.gender; // 校验姓名
if (s.name.isEmpty() || s.gender.isEmpty() || s.id.isEmpty() || s.phone.isEmpty()) if (!nameKw.isEmpty() && !s.name.contains(nameKw, Qt::CaseInsensitive))
{ {
QMessageBox::warning(this, "错误", "信息不完整(姓名、性别、学号、电话不能为空)"); isMatch = false;
return;
} }
// 校验性别
for (auto &st : classData[cls]) if (isMatch && !genderKw.isEmpty() && !s.gender.contains(genderKw, Qt::CaseInsensitive))
{ {
if (st.id == s.id) isMatch = false;
}
// 校验年龄
if (isMatch && !ageKw.isEmpty() && !QString::number(s.age).contains(ageKw))
{ {
QMessageBox::warning(this, "错误", "学号重复"); isMatch = false;
return;
} }
// 校验学号
if (isMatch && !idKw.isEmpty() && !s.id.contains(idKw, Qt::CaseInsensitive))
{
isMatch = false;
}
// 校验电话
if (isMatch && !phoneKw.isEmpty() && !s.phone.contains(phoneKw, Qt::CaseInsensitive))
{
isMatch = false;
} }
classData[cls].append(s); // 将新学生追加到当前班级的学生列表 // 4. 如果所有非空条件都通过,则加入显示列表
refreshTable(); if (isMatch)
{
displayList.append(s);
}
}
currentPage = 1; // 重置到第一页
refreshTable(); // 刷新表格显示
}
void MainWindow::refreshAll()
{
QString cls = currentClass();
if (!cls.isEmpty())
displayList = classData[cls]; // 恢复初始状态
currentPage = 1;
// 刷新输入框内容 // 刷新输入框内容
nameEdit->clear(); nameEdit->clear();
@ -258,32 +344,246 @@ void MainWindow::addStudent()
ageEdit->clear(); ageEdit->clear();
idEdit->clear(); idEdit->clear();
phoneEdit->clear(); phoneEdit->clear();
refreshTable();
} }
void MainWindow::deleteStudent() // --- 文件与分页 ---
void MainWindow::saveClassToFile(const QString &className)
{ {
QString cls = currentClass(); // 拿到班级名称 QFile file(className + ".txt"); // 按班级名保存
int row = table->currentRow(); // 获取当前选中行 if (file.open(QIODevice::WriteOnly | QIODevice::Text))
if (cls.isEmpty() || row < 0) {
QTextStream out(&file);
for (const auto &s : classData[className])
{
out << s.name << "," << s.gender << "," << s.age << "," << s.id << "," << s.phone << "\n";
}
}
}
void MainWindow::saveData()
{
for (auto it = classData.begin(); it != classData.end(); ++it)
saveClassToFile(it.key());
QMessageBox::information(this, "提示", "所有班级已保存");
}
void MainWindow::importData()
{
// 1. 直接弹出文件选择框,不需要预先选中班级
QString path = QFileDialog::getOpenFileName(this, "导入班级文件", "", "文本文件 (*.txt);;所有文件 (*.*)");
if (path.isEmpty())
return; return;
if (QMessageBox::question(this, "确认", "删除该学生?") == QMessageBox::Yes) // 2. 提取文件名作为班级名
QFileInfo fileInfo(path);
QString className = fileInfo.baseName(); // 例如 "高三二班"
// 3. 检查班级是否已存在,不存在则新建
if (!classData.contains(className))
{ {
classData[cls].removeAt(row); classData[className] = QList<Student>();
refreshTable(); QTreeWidgetItem *newItem = new QTreeWidgetItem({className});
tree->addTopLevelItem(newItem);
tree->setCurrentItem(newItem); // 自动选中这个新创建的班级
} }
else
{
// 如果班级已存在,询问是追加还是清空
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "班级已存在",
QString("班级 '%1' 已存在,是否追加数据?\n(选“否”将清空原数据并覆盖)").arg(className),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if (reply == QMessageBox::Cancel)
return;
if (reply == QMessageBox::No)
{
classData[className].clear(); // 清空原数据实现“覆盖”
}
// 选中已有的班级节点
QList<QTreeWidgetItem *> items = tree->findItems(className, Qt::MatchExactly);
if (!items.isEmpty())
tree->setCurrentItem(items[0]);
}
// 4. 读取文件内容
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::critical(this, "错误", "无法打开文件");
return;
}
QTextStream in(&file);
int count = 0;
while (!in.atEnd())
{
QString line = in.readLine();
QStringList fields = line.split(",");
if (fields.size() >= 5)
{
Student s;
s.name = fields[0].trimmed();
s.gender = fields[1].trimmed();
s.age = fields[2].trimmed().toInt();
s.id = fields[3].trimmed();
s.phone = fields[4].trimmed();
classData[className].append(s);
count++;
}
}
file.close();
// 5. 更新显示
displayList = classData[className];
currentPage = 1;
refreshTable();
QMessageBox::information(this, "导入成功", QString("已成功创建/更新班级 '%1',并导入 %2 条学生记录。").arg(className).arg(count));
} }
void MainWindow::tableItemClicked(int row, int) // 点击学生信息进行回显 void MainWindow::prevPage()
{ {
QString cls = currentClass(); if (currentPage > 1)
if (cls.isEmpty()) {
return; currentPage--;
auto s = classData[cls][row]; refreshTable();
}
}
void MainWindow::nextPage()
{
if (currentPage * pageSize < displayList.size())
{
currentPage++;
refreshTable();
}
}
void MainWindow::tableItemClicked(int row, int)
{
int realIdx = (currentPage - 1) * pageSize + row;
if (realIdx < displayList.size())
{
const auto &s = displayList[realIdx];
nameEdit->setText(s.name); nameEdit->setText(s.name);
genderEdit->setText(s.gender); genderEdit->setText(s.gender);
ageEdit->setText(QString::number(s.age)); ageEdit->setText(QString::number(s.age));
idEdit->setText(s.id); idEdit->setText(s.id);
phoneEdit->setText(s.phone); phoneEdit->setText(s.phone);
}
}
// 班级管理槽函数(逻辑同前,移至菜单触发)
void MainWindow::addClass()
{
while (true)
{
bool ok;
// 弹出输入框
QString name = QInputDialog::getText(this, "新建班级", "请输入班级名称:", QLineEdit::Normal, "", &ok);
// 如果用户点击了“取消”或直接关闭窗口,则退出函数
if (!ok)
return;
name = name.trimmed(); // 去除首尾空格
// 逻辑判断 1不能为空
if (name.isEmpty())
{
QMessageBox::warning(this, "错误", "班级名称不能为空,请重新输入!");
continue; // 跳回循环开始,重新弹出输入框
}
// 逻辑判断 2检查是否已存在
if (classData.contains(name))
{
QMessageBox::warning(this, "错误", QString("班级“%1”已存在请换一个名称").arg(name));
continue; // 跳回循环开始,重新弹出输入框
}
// 校验通过:保存数据并更新界面
classData[name] = QList<Student>();
QTreeWidgetItem *newItem = new QTreeWidgetItem({name});
tree->addTopLevelItem(newItem);
tree->setCurrentItem(newItem); // 自动选中新创建的班级
break; // 成功创建后跳出循环
}
}
void MainWindow::deleteClass()
{
auto item = tree->currentItem();
if (!item)
{
QMessageBox::warning(this, "提示", "请先选择要删除的班级");
return;
}
if (item && QMessageBox::question(this, "确认", "删除班级及其信息?") == QMessageBox::Yes)
{
classData.remove(item->text(0));
delete item;
displayList.clear();
refreshTable();
}
}
void MainWindow::renameClass()
{
// 1. 获取当前选中的班级节点
auto item = tree->currentItem();
if (!item)
{
QMessageBox::warning(this, "提示", "请先选择要重命名的班级");
return;
}
QString oldName = item->text(0);
// 2. 使用循环处理重复或为空的情况
while (true)
{
bool ok;
// 弹出输入框,默认显示旧名称
QString newName = QInputDialog::getText(this, "重命名班级", "请输入新的班级名称:",
QLineEdit::Normal, oldName, &ok);
// 如果用户点击了“取消”,则直接退出
if (!ok)
return;
newName = newName.trimmed(); // 去除多余空格
// 情况 A名称没变直接退出
if (newName == oldName)
return;
// 情况 B名称为空
if (newName.isEmpty())
{
QMessageBox::warning(this, "错误", "班级名称不能为空,请重新输入!");
continue; // 跳回循环开始,再次弹出对话框
}
// 情况 C名称与现有其他班级重复
if (classData.contains(newName))
{
QMessageBox::warning(this, "错误", QString("班级“%1”已存在请换一个名称").arg(newName));
continue; // 跳回循环开始
}
// 3. 校验通过,更新底层数据结构 QMap
// 使用 take() 移除旧键并返回对应的数据列表,再赋值给新键
classData[newName] = classData.take(oldName);
// 4. 更新界面显示的名称
item->setText(0, newName);
break; // 完成重命名,跳出循环
}
} }

@ -8,6 +8,7 @@
#include <QPushButton> #include <QPushButton>
#include <QMap> #include <QMap>
#include <QList> #include <QList>
#include <QLabel>
struct Student struct Student
{ {
@ -28,48 +29,44 @@ public:
private slots: private slots:
// 班级管理 // 班级管理
void showTreeMenu(const QPoint &pos);
void addClass(); void addClass();
void deleteClass(); void deleteClass();
void renameClass(); void renameClass();
void onClassClicked(QTreeWidgetItem *item); void onClassClicked(QTreeWidgetItem *item);
// 学生管理 // 学生管理
void addStudent(); void addStudent();
void deleteStudent(); void deleteStudent();
// void updateStudent(); void updateStudent();
// void searchStudent(); void searchStudent();
void tableItemClicked(int row, int column); void tableItemClicked(int row, int column);
// 文件操作 与 分页
// void importData(); // 文件操作与分页
// void saveData(); void importData();
// void refreshAll(); void saveData();
// void prevPage(); void refreshAll();
// void nextPage(); void prevPage();
void nextPage();
private: private:
void setupUI(); void setupUI();
void refreshTable(); void refreshTable();
// void saveClassToFile(const QString &className); void saveClassToFile(const QString &className);
QString currentClass() const; QString currentClass() const;
private: // UI 控件
// 顶部菜单栏
QMenu *fileMenu;
QMenu *editMenu;
// 左侧班级树控件
QTreeWidget *tree; QTreeWidget *tree;
// 中间学生表格控件
QTableWidget *table; QTableWidget *table;
QLineEdit *nameEdit, *genderEdit, *ageEdit, *idEdit, *phoneEdit;
QPushButton *addBtn, *delBtn, *updateBtn, *searchBtn, *refreshBtn;
QPushButton *btnPrev, *btnNext;
QLabel *pageLabel;
QLineEdit *nameEdit; // 数据存储
QLineEdit *genderEdit;
QLineEdit *ageEdit;
QLineEdit *idEdit;
QLineEdit *phoneEdit;
QPushButton *addBtn;
QPushButton *delBtn;
// 班级数据存储
QMap<QString, QList<Student>> classData; QMap<QString, QList<Student>> classData;
QList<Student> displayList; // 当前显示的(过滤后的)数据
int currentPage = 1;
const int pageSize = 10; // 每页10条
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

Loading…
Cancel
Save