说明:
作为练习的学生管理系统,使用vs2015,qt5.10完成(未使用数据库)。
上图:
功能描述 :
1.查找,导入,导出功能未完善,暂不可用
2.新建,删除,修改功能正常
3.在左侧输入数据点击新建,tablewidget显示对应数据
4.tablewidget点击某个数据,点击删除即可删除数据
5.点击某个数据,左侧属性栏显示对应数据,此时修改属性栏数据并点击更改即可完成修改
源码:
StudentInfo.h
#pragma once
#include <QtWidgets/QMainWindow>
#include <QList>
#include <QDebug>
#include "ui_StudentInfo.h"
class StudentInfo : public QWidget
{
Q_OBJECT
public:
StudentInfo(QWidget *parent = Q_NULLPTR);
~StudentInfo();
private slots:
void createInfo(); //新建
void deleteInfo(); //删除
void searchInfo(); //查找信息
void changeInfo(); //更改信息
void showInfo(int,int); //点击表格某行显示信息之属性栏
private:
Ui::StudentInformation ui;
QList<QString> m_studentList;
QString m_name;
bool m_sex;
int m_age;
int m_grade;
void init();
void initConnect();
void getInfo(); //获取属性栏信息
};
StudentInfo.cpp
#include "StudentInfo.h"
StudentInfo::StudentInfo(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
init();
initConnect();
}
StudentInfo::~StudentInfo()
{
}
void StudentInfo::getInfo()
{
m_studentList.clear();
m_studentList.append(ui.nameEdit->text());
m_studentList.append(ui.sexEdit->text());
m_studentList.append(ui.ageEdit->text());
m_studentList.append(ui.gradeEdit->text());
}
void StudentInfo::showInfo(int row,int)
{
row = ui.studentTable->currentRow();
ui.nameEdit->setText(ui.studentTable->item(row,0 )->text() );
ui.sexEdit->setText(ui.studentTable->item(row, 1)->text());
ui.ageEdit->setText(ui.studentTable->item(row, 2)->text());
ui.gradeEdit->setText(ui.studentTable->item(row, 3)->text());
}
void StudentInfo::createInfo()
{
getInfo();
int row = 0;
row = ui.studentTable->rowCount();
ui.studentTable->setRowCount(row + 1);
for (int i = 0;i < 4;i++)
ui.studentTable->setItem(row, i, new QTableWidgetItem(m_studentList.at(i)));
}
void StudentInfo::deleteInfo()
{
ui.studentTable->removeRow(ui.studentTable->currentRow());
}
void StudentInfo::searchInfo()
{
}
void StudentInfo::changeInfo()
{
ui.studentTable->setSelectionMode(QAbstractItemView::SingleSelection); //设置选择模式,选择单行
getInfo();
for (int i = 0;i < 4;i++)
ui.studentTable->setItem(ui.studentTable->currentRow(), i, new QTableWidgetItem(m_studentList.at(i)));
}
void StudentInfo::init()
{
this->setWindowTitle(QStringLiteral("学生信息表"));
/*设置表格为整行选中*/
ui.studentTable->setSelectionBehavior(QAbstractItemView::SelectRows);
}
void StudentInfo::initConnect()
{
connect(ui.createButton, SIGNAL(clicked()), this, SLOT(createInfo()));
connect(ui.deleteButton, SIGNAL(clicked()), this, SLOT(deleteInfo()));
connect(ui.searchButton, SIGNAL(clicked()), this, SLOT(searchInfo()));
connect(ui.changeButton, SIGNAL(clicked()), this, SLOT(changeInfo()));
connect(ui.studentTable, SIGNAL(cellClicked(int,int)), this, SLOT(showInfo(int,int)));
}
StudentInfo.ui:
(PS:一个简单的练习,如果有什么不对欢迎指正!)