请用qt给我写一个简单学生管理系统界面
时间: 2024-01-24 10:05:41 浏览: 139
一个简单的学生管理系统源码分享(QT)
很抱歉,由于我是一个AI语言模型,无法编写代码来创建GUI界面。但是,以下是一个使用Qt编写的简单学生管理系统的示例:
mainwindow.h
```c++
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_addButton_clicked();
void on_editButton_clicked();
void on_deleteButton_clicked();
private:
Ui::MainWindow *ui;
QStandardItemModel *model;
int selectedRow;
void clearForm();
};
#endif // MAINWINDOW_H
```
mainwindow.cpp
```c++
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// create student list model
model = new QStandardItemModel();
model->setHorizontalHeaderLabels(QStringList() << "ID" << "Name" << "Age" << "Gender" << "Email");
ui->tableView->setModel(model);
// set selection behavior
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
// set table column width
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// clear form
clearForm();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_addButton_clicked()
{
// get input values
QString id = ui->idLineEdit->text();
QString name = ui->nameLineEdit->text();
QString age = ui->ageLineEdit->text();
QString gender = ui->genderComboBox->currentText();
QString email = ui->emailLineEdit->text();
// validate input
if (id.isEmpty() || name.isEmpty() || age.isEmpty() || email.isEmpty()) {
QMessageBox::warning(this, "Warning", "Please fill in all the fields.");
return;
}
// add student to list
QList<QStandardItem*> row;
row.append(new QStandardItem(id));
row.append(new QStandardItem(name));
row.append(new QStandardItem(age));
row.append(new QStandardItem(gender));
row.append(new QStandardItem(email));
model->appendRow(row);
// clear form
clearForm();
}
void MainWindow::on_editButton_clicked()
{
// get input values
QString id = ui->idLineEdit->text();
QString name = ui->nameLineEdit->text();
QString age = ui->ageLineEdit->text();
QString gender = ui->genderComboBox->currentText();
QString email = ui->emailLineEdit->text();
// validate input
if (id.isEmpty() || name.isEmpty() || age.isEmpty() || email.isEmpty()) {
QMessageBox::warning(this, "Warning", "Please select a student and fill in all the fields.");
return;
}
// update student in list
model->setData(model->index(selectedRow, 0), id);
model->setData(model->index(selectedRow, 1), name);
model->setData(model->index(selectedRow, 2), age);
model->setData(model->index(selectedRow, 3), gender);
model->setData(model->index(selectedRow, 4), email);
// clear form
clearForm();
}
void MainWindow::on_deleteButton_clicked()
{
// check if a row is selected
if (selectedRow < 0) {
QMessageBox::warning(this, "Warning", "Please select a student to delete.");
return;
}
// remove selected row
model->removeRow(selectedRow);
// clear form
clearForm();
}
void MainWindow::clearForm()
{
// clear input fields
ui->idLineEdit->clear();
ui->nameLineEdit->clear();
ui->ageLineEdit->clear();
ui->genderComboBox->setCurrentIndex(0);
ui->emailLineEdit->clear();
// deselect row
ui->tableView->clearSelection();
// reset selected row
selectedRow = -1;
}
void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
// get selected row
selectedRow = index.row();
// set input fields
ui->idLineEdit->setText(model->index(selectedRow, 0).data().toString());
ui->nameLineEdit->setText(model->index(selectedRow, 1).data().toString());
ui->ageLineEdit->setText(model->index(selectedRow, 2).data().toString());
ui->genderComboBox->setCurrentText(model->index(selectedRow, 3).data().toString());
ui->emailLineEdit->setText(model->index(selectedRow, 4).data().toString());
}
```
阅读全文