You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.5 KiB
C++
65 lines
1.5 KiB
C++
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
#include <QMessageBox>
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent), ui(new Ui::MainWindow)
|
|
{
|
|
ui->setupUi(this);
|
|
model = new QStandardItemModel(this);
|
|
model->setHorizontalHeaderLabels({"学号", "姓名", "成绩"});
|
|
ui->tableView->setModel(model);
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void MainWindow::on_addButton_clicked()
|
|
{
|
|
// 添加学生信息的逻辑
|
|
Student stu;
|
|
stu.id = ui -> idEdit->text();
|
|
stu.name = ui -> nameEdit->text();
|
|
stu.score = ui -> scoreEdit->text().toDouble();
|
|
// 判空
|
|
if (stu.id.isEmpty() || stu.name.isEmpty())
|
|
{
|
|
QMessageBox::warning(this, "错误", "学号和姓名不能为空!");
|
|
return;
|
|
}
|
|
students.push_back(stu);
|
|
// 刷新表格
|
|
refreshTable();
|
|
// 刷新输入框内容
|
|
ui->idEdit->clear();
|
|
ui->nameEdit->clear();
|
|
ui->scoreEdit->clear();
|
|
}
|
|
|
|
void MainWindow::on_deleteButton_clicked()
|
|
{
|
|
int row = ui->tableView->currentIndex().row();
|
|
if (row < 0)
|
|
return;
|
|
|
|
students.remove(row);
|
|
// 刷新表格
|
|
refreshTable();
|
|
}
|
|
|
|
void MainWindow::refreshTable()
|
|
{
|
|
model->removeRows(0, model->rowCount());
|
|
|
|
for (const Student &stu : students)
|
|
{
|
|
QList<QStandardItem *> row;
|
|
row << new QStandardItem(stu.id)
|
|
<< new QStandardItem(stu.name)
|
|
<< new QStandardItem(QString::number(stu.score));
|
|
model->appendRow(row);
|
|
}
|
|
}
|