student_manager 教学示例:菜单/工具栏加「删除选中」action(函数体待实现)

「记录」菜单与工具栏新增 actRemoveSelected(删除选中),connect 到 onRemoveSelected
槽。注意:onRemoveSelected() 函数体当前为空(实现被注释),仅完成 action 注册与
连接——槽逻辑(按选中行倒序 removeRow)留作课堂练习或后续补全,先占位。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
张宗平
2026-07-09 16:51:49 +08:00
parent d3e2e50f0e
commit d0496675e9
@@ -122,6 +122,7 @@ public:
auto* actAdd = new QAction(QStringLiteral("添加记录"), this);
auto* actSave = new QAction(QStringLiteral("保存"), this);
auto* actLoad = new QAction(QStringLiteral("读取"), this);
auto* actRemoveSelected = new QAction(QStringLiteral("删除选中"), this);
m_actImport = new QAction(QStringLiteral("后台导入"), this);
auto* actAbout = new QAction(QStringLiteral("关于"), this);
@@ -131,12 +132,14 @@ public:
QMenu* recordMenu = menuBar()->addMenu(QStringLiteral("记录(&R)"));
recordMenu->addAction(actAdd);
recordMenu->addAction(m_actImport);
recordMenu->addAction(actRemoveSelected);
menuBar()->addMenu(QStringLiteral("帮助(&H)"))->addAction(actAbout);
QToolBar* bar = addToolBar(QStringLiteral("main"));
bar->addAction(actAdd);
bar->addAction(actSave);
bar->addAction(actLoad);
bar->addAction(actRemoveSelected);
bar->addAction(m_actImport);
// QAction 显示出来 ≠ 生效:忘 connect 就是「菜单点了没反应」(Day3 坑卡)
@@ -144,6 +147,7 @@ public:
connect(actSave, &QAction::triggered, this, [this] { saveTo(m_dataPath); });
connect(actLoad, &QAction::triggered, this, [this] { loadFrom(m_dataPath); });
connect(m_actImport, &QAction::triggered, this, &MainWindow::onImport);
connect(actRemoveSelected, &QAction::triggered, this, &MainWindow::onRemoveSelected);
connect(actAbout, &QAction::triggered, this, [this] {
QMessageBox::about(this, QStringLiteral("关于"),
QStringLiteral("Day5 综合案例参考实现:Day1–4 全部知识点的收束。"));
@@ -165,6 +169,12 @@ public:
m_table->setItem(row, 2, new QTableWidgetItem(s.tag));
}
void onRemoveSelected() {
// for (int row = m_table->selectedRows().size() - 1; row >= 0; --row) {
// m_table->removeRow(m_table->selectedRows().at(row).row());
// }
}
bool saveTo(const QString& path) {
QJsonArray arr;
for (int row = 0; row < m_table->rowCount(); ++row) {