本系统支持学生的学籍信息添加、删除、修改、查询功能,并且支持EXCEL表格导入和保存。系统界面如下:
保存后:
后端代码目录:
SIMS2.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = SIMS2
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES +=main.cpp\
mainwindow.cpp \
student.cpp \
studentdatabase.cpp \
delegate.cpp \
dialog.cpp \
datedelegate.cpp
HEADERS +=mainwindow.h \
studentdatabase.h \
student.h \
delegate.h \
dialog.h \
datedelegate.h
FORMS += mainwindow.ui
datedelegate.h:
#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H
#include <QStyledItemDelegate>
#include <QItemDelegate>
#include <QModelIndex>
#include <QPainter>
#include <QWidget>
#include<QDateTimeEdit>
class DateDelegate : public QItemDelegate
{
Q_OBJECT
public:
DateDelegate(QObject *parent = nullptr);
~DateDelegate();
QWidget *createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;//返回改变Model数据的widget,该widget是经过定制行为的Widget
void setEditorData(QWidget * editor, const QModelIndex & index) const;//将可操作的数据提供给widget
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const; //将widget的数据展示到Item中
void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;//确保widget能够正确显示到view中
private:
};
#endif // DATEDELEGATE_H
delegate.h:
#ifndef DELEGATE_H
#define DELEGATE_H
#include <QStyledItemDelegate>
#include <QItemDelegate>
#include <QModelIndex>
#include <QPainter>
#include <QWidget>
#define SEXCOMBOXCOL 1
#define ORIENTITIONCOMBOXCOL 4
class Delegate : public QItemDelegate
{
Q_OBJECT
public:
Delegate(QObject *parent = nullptr);
~Delegate();
void paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const;
private:
};
#endif // DELEGATE_H
dialog.h:
#ifndef DIALOG_H
#define DIALOG_H
#include<QtWidgets>
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<QHBoxLayout>
class dialog:public QDialog{
Q_OBJECT
public:
dialog(QWidget *parent = 0);
~dialog();
private:
//QLabel *searchLabel;
QLineEdit *searchLineEdit;
QPushButton *searchButton1;
QPushButton *searchButton2;
QPushButton *searchButton3;
QPushButton *searchButton4;
QHBoxLayout *searchLayout;
void initialize();
};
#endif // DIALOG_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<string>
#include<iostream>
using namespace std;
#include <QMainWindow>
#include <QWidget>
#include<QTableWidget>
#include<QStringList>
//#include<QLayout>
#include<QObject>
#include<QAction>
#include<QMenu>
#include<QLayout>
#include<QTableWidget>
#include<QObject>
#include<QSize>
#include<QString>
#include<QFileDialog>
#include<QFile>
#include<QMessageBox>
#include<QTextStream>
#include<Qtextedit>
#include<QContextMenuEvent>
#include<QVector>
#include<QComboBox>
#include<QLineEdit>
#include"studentdatabase.h"
#include"delegate.h"
#include"dialog.h"
#include"datedelegate.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void closeEvent(QCloseEvent *event);
private:
Ui::MainWindow *ui;
// QTableWidget *table;
// QLayout *layout;
bool saved;//文件保存标识符
QAction *newAction;
QAction *openAction;
QAction *saveAction;
QAction *exitAction;
QAction *addAction;
QAction *deleteAction;
QAction *moveUpAction;
QAction *moveDownAction;
//QAction *sortAction;
QAction *searchAction;
QAction *gradeAction;
QAction *majorAction;
QAction *gpaAction;
QAction *insertAction;
studentDatabase *stuDB;
dialog *searchDialog;
bool c[8];
QWidget *widget;
QLayout *layout;
QTableWidget *table;
QVector<QComboBox*> *QComboBoxList;
QLineEdit *searchQLineEditToolBar;
QAction *searchToolBar;
QAction *lastToolBar;
QAction *nextToolBar;
QPushButton *searchButton;
QPushButton *lastButton;
QPushButton *nextButton;
QString searchQstr;
int searchRow;
int searchColumn;
void tableInitialize();
int partition(int p, int q, int column);
void quickSort(int p, int q, int column);
void Swap(int p1,int p2, int column);
virtual void contextMenuEvent(QContextMenuEvent *event);
private slots:
void newFile();
void openFile();
void saveFile();
void addModify();
void deleteModify();
void moveUpModify();
void moveDownModify();
void sortView(int colunmIndex);
void searchView();
void gradeStatistic();
void majorStatistic();
void gpaStatistic();
void tableModify(int row,int column);
void insertBefore();
void insertAfter();
void deleteLine();
void print_s();
void searchQlineEditChanged(QString qstr);
void searchToolBarTriggered();
void lastToolBarTriggered();
void nextToolBarTriggered();
//void qtClose();
};
#endif // MAINWINDOW_H
student.h
#ifndef STUDENT_H
#define STUDENT_H
#include<string>
using namespace std;
class student{
private:
string name;
string orientation;
int id;
int age;
int GPA;
int GPARank;
int phoneNumber;//unused here
public:
student():name(""),orientation(""),id(-1),age(-1),GPA(-1),GPARank(-1){}
student(string name,string orientation,int id,int age,int GPA,int GPARank):
name(name),orientation(orientation),id(id),age(age),GPARank(GPARank){}
const string getName();
const string getOrientation();
const int getId();
const int getAge();
const int getGPA();
const int getGPARank();
bool operator ==(student stu);
bool operator ==(string name);
bool operator ==(int id);
};
#endif // STUDENT_H
studentdatabase.h
#ifndef STUDENTDATABASE_H
#define STUDENTDATABASE_H
#include<vector>
using namespace std;
#include"student.h"
class studentDatabase{
private:
vector<student> stu;
public:
const student getStudent(int index);
const int getSize();
void addStudent();
bool addStudent(student stu);
//bool addStudent(student stu);
bool deleteStudent(int index);
bool deleteStudent(student stu);
const int findStudent(student stu);
const int findStudent(string name);
const int findStudent(int id);
void sortByGPA();
bool clear();
};
#endif // STUDENTDATABASE_H
datedelegate.cpp
#include"datedelegate.h"
DateDelegate::DateDelegate(QObject *parent): QItemDelegate(parent){}
DateDelegate::~DateDelegate(){}
QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QDateTimeEdit *editer = new QDateTimeEdit(parent);
editer->setDisplayFormat("yyyy-MM-dd");//设置显示格式
editer->setCalendarPopup(true);//设置日历控件为悬空显示
editer->installEventFilter(const_cast<DateDelegate*>(this));//安装时间过滤器,使得代理能够获取定制控件的值
return editer;
}
void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString dateStr = index.model()->data(index).toString();//通过index获取model中相应项ITem的值
QDate date = QDate::fromString(dateStr,Qt::ISODate);
QDateTimeEdit *edit = static_cast<QDateTimeEdit *>(editor);//转换类型获取定制的控件
edit->setDate(date);//将Item项中的值展示到定制控件中
}
void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QDateTimeEdit *edit = static_cast<QDateTimeEdit *>(editor);
QDate date = edit->date();
model->setData(index,QVariant(date.toString(Qt::ISODate)));//将定制控件中的值展示到表格中
}
//调整定制控件的展示,确保可以展示到窗体View中
void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
delegate.cpp
#include "Delegate.h"
#include <QComboBox>
Delegate::Delegate(QObject *parent)
: QItemDelegate(parent)
{
}
Delegate::~Delegate()
{
}
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QItemDelegate::paint(painter, option, index);
}
QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QItemDelegate::sizeHint(option, index);
}
QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.isValid() && (index.column() == SEXCOMBOXCOL||index.column()==ORIENTITIONCOMBOXCOL))
{
QComboBox *editor = new QComboBox(parent);
editor->setEditable(true);
editor->installEventFilter(const_cast<Delegate *>(this));
return editor;
}
else
{
return QItemDelegate::createEditor(parent, option, index);
}
}
void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (index.isValid()){
if(index.column()==SEXCOMBOXCOL)
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *combox = static_cast<QComboBox *>(editor);
combox->addItem("(请选择)");
combox->addItem("男");
combox->addItem("女");
//combox->addItem("+3");
//combox->addItem("+4");
combox->setCurrentText(value);
}
else if(index.column()==ORIENTITIONCOMBOXCOL){
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *combox = static_cast<QComboBox *>(editor);
combox->addItem("(请选择)");
combox->addItem("计算机应用");
combox->addItem("软件工程");
combox->addItem("计算机科学");
combox->addItem("信息安全");
combox->addItem("计算机系统");
combox->setCurrentText(value);
}
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}
void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if (index.isValid() && (index.column() == SEXCOMBOXCOL || index.column()==ORIENTITIONCOMBOXCOL))
{
QComboBox *combox = static_cast<QComboBox *>(editor);
model->setData(index, combox->currentText());
}
else
{
QItemDelegate::setModelData(editor, model, index);
}
}
dialog.cpp
#include"dialog.h"
dialog::dialog(QWidget *parent):QDialog(parent){
initialize();
}
dialog::~dialog()
{
}
void dialog::initialize(){
this->setWindowTitle(tr("搜索"));
setWindowFlags(Qt::WindowCloseButtonHint);
//searchLabel = new QLabel(tr("&搜索"));
searchLineEdit = new QLineEdit();
//searchLabel->setBuddy(searchLineEdit);//快捷键绑定;
searchButton1 = new QPushButton(tr("&搜索"));
searchButton2 = new QPushButton(tr("&上一个"));
searchButton3 = new QPushButton(tr("&下一个"));
searchButton4 = new QPushButton(tr("&完成"));
searchLayout = new QHBoxLayout;
//searchLayout->addWidget(searchLabel);
searchLayout->addWidget(searchLineEdit);
searchLayout->addWidget(searchButton1);
searchLayout->addWidget(searchButton2);
searchLayout->addWidget(searchButton3);
searchLayout->addWidget(searchButton4);
setLayout(searchLayout);
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
//w.setLayout(layout);
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include <QApplication>
#define itemEmpty " "
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
saved = true;
//界面
this->setWindowTitle(tr("学生信息管理系统"));
//菜单
QMenu *file = menuBar()->addMenu(tr("&文件"));
newAction = new QAction(tr("新建..."),this);
connect(newAction,SIGNAL(triggered()),this,SLOT(newFile()));
file->addAction(newAction);
openAction = new QAction(tr("打开..."),this);
//openAction->setStatusTip(tr("1234"));
connect(openAction,SIGNAL(triggered()),this,SLOT(openFile()));
file->addAction(openAction);
saveAction = new QAction(tr("保存..."),this);
connect(saveAction,SIGNAL(triggered()),this,SLOT(saveFile()));
file->addAction(saveAction);
QMenu *modify = menuBar()->addMenu(tr("&编辑"));
addAction = new QAction(tr("增加一行"),this);
connect(addAction,SIGNAL(triggered()),this,SLOT(addModify()));
modify->addAction(addAction);
QAction*insertUp = new QAction(tr("上方插入一行"),this);
connect(insertUp,SIGNAL(triggered()),this,SLOT(insertBefore()));
modify->addAction(insertUp);
QAction*insertDown = new QAction(tr("下方插入一行"),this);
connect(insertDown,SIGNAL(triggered()),this,SLOT(insertAfter()));
modify->addAction(insertDown);
moveUpAction = new QAction(tr("上移一行"),this);
connect(moveUpAction,SIGNAL(triggered()),this,SLOT(moveUpModify()));
modify->addAction(moveUpAction);
moveDownAction = new QAction(tr("下移一行"),this);
connect(moveDownAction,SIGNAL(triggered()),this,SLOT(moveDownModify()));
modify->addAction(moveDownAction);
deleteAction = new QAction(tr("删除"),this);
connect(deleteAction,SIGNAL(triggered()),this,SLOT(deleteModify()));
modify->addAction(deleteAction);
//QMenu *exit = menuBar()->addMenu(tr("&退出"));
//connect(exit,SIGNAL(triggered()),this,SLOT(close()));
/*
//connect(button, &QPushButton::clicked, someFunction);
//QMenu *view = menuBar()->addMenu(tr("&查看"));
//sortAction = new QAction(tr("排序"),this);
//connect(sortAction,SIGNAL(triggered()),this,SLOT(sortView()));
//view->addAction(sortAction);
searchAction = new QAction(tr("&搜索"),this);
connect(searchAction,SIGNAL(triggered()),this,SLOT(searchView()));
view->addAction(searchAction);
*/
/*
QMenu *statistic = menuBar()->addMenu(tr("&统计"));
gradeAction = new QAction(tr("年级"));
connect(gradeAction,SIGNAL(triggered()),this,SLOT(gradeStatistic()));
statistic->addAction(gradeAction);
majorAction = new QAction(tr("专业"));
connect(majorAction,SIGNAL(triggered()),this,SLOT(majorStatistic()));
statistic->addAction(majorAction);
gpaAction = new QAction(tr("GPA"));
connect(gpaAction,SIGNAL(triggered()),this,SLOT(gpaStatistic()));
statistic->addAction(gpaAction);
*/
QComboBoxList = new QVector<QComboBox*>();
//右键菜单
//表格
widget = new QWidget();
this->setCentralWidget(widget);
layout = new QHBoxLayout;
table = new QTableWidget();
table->setColumnCount(8);
table->setRowCount(0);
table->horizontalHeader()->setHighlightSections(false);
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
connect(table,SIGNAL(cellChanged(int,int)),this,SLOT(tableModify(int,int)));
QStringList header;
header<<tr("姓名")<<tr("性别")<<tr("学号")<<tr("年级")<<tr("专业方向")<<tr("出生日期(年-月-日)")<<(tr("籍贯"))<<(tr("住址"));
//table->horizontalHeader()->setDefaultSectionSize(15);
connect(table->horizontalHeader(),SIGNAL(sectionClicked(int)),this, SLOT(sortView(int)));
table->setHorizontalHeaderLabels(header);
//table->setItem(0,0,new QTableWidgetItem("嘤嘤嘤?"));
//table->setItem(0,1,new QTableWidgetItem("嘤嘤嘤?"));
//table->show();
layout->addWidget(table);
widget->setLayout(layout);
//工具栏搜索:支持姓名、id两种精确搜索
//QToolBar *toolBar = addToolBar(tr("&搜索"));
//toolBar->addAction(searchAction);
//layout->addWidget(toolBar);
//工具栏搜索:支持专业、GPA、年龄三种模糊搜索
QComboBox*tmp = new QComboBox();
tmp->setEditable(false);
table->setItemDelegateForColumn(1,new Delegate(this));
QComboBoxList->push_back(tmp);
tmp = new QComboBox();
tmp->setEditable(false);
table->setItemDelegateForColumn(4,new Delegate(this));
//setItemIsEditable(ui.table, 0);
QComboBoxList->push_back(tmp);
//数据库呈现:这里无需呈现,因为刚打开程序,既没有添加也没有读入文件
//后续考虑加入历史纪录,每次打开软件后,自动开打开上一次的文件。这样需要先读一个墓碑文件,之后再读入用户文件,初始化数据库,再修改行数
stuDB = new studentDatabase();
//table->setRowCount(stuDB->getSize()+3);
for(int i = 0; i < table->columnCount();i++)
c[i] = true;
this->resize(QSize(1200,800));
//搜索功能初始化
searchQstr.clear();
searchRow = -1;
searchColumn = -1;
searchQLineEditToolBar = new QLineEdit;
/*
searchToolBar = new QAction(tr("&搜索"),this);
lastToolBar = new QAction(tr("&上一个"),this);
nextToolBar = new QAction(tr("&下一个"),this);
*/
searchButton = new QPushButton(tr("&搜索"));
lastButton = new QPushButton(tr("&上一个"));;
nextButton = new QPushButton(tr("&下一个"
""));;
//ui->mainToolBar->setVisible(false);
ui->mainToolBar->addWidget(searchQLineEditToolBar);
ui->mainToolBar->addWidget(searchButton);
ui->mainToolBar->addWidget(lastButton);
ui->mainToolBar->addWidget(nextButton);
connect(searchQLineEditToolBar,SIGNAL(textChanged(QString)),this,SLOT(searchQlineEditChanged(QString)));
connect(searchButton,SIGNAL(clicked()),this,SLOT(searchToolBarTriggered()));
connect(lastButton,SIGNAL(clicked()),this,SLOT(lastToolBarTriggered()));
connect(nextButton,SIGNAL(clicked()),this,SLOT(nextToolBarTriggered()));
//ui->mainToolBar->addAction(new QAction(tr("&完成"),this));
//ui->mainToolBar->addWidget(QPushButton(tr("&搜索")))
}
void MainWindow::tableInitialize(){
delete table;
delete layout;
delete QComboBoxList;
saved = false;
layout = new QHBoxLayout;
table = new QTableWidget();
QComboBoxList = new QVector<QComboBox*>();
table->setColumnCount(8);
table->setRowCount(0);
table->horizontalHeader()->setHighlightSections(false);
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
connect(table,SIGNAL(cellChanged(int,int)),this,SLOT(tableModify(int,int)));
QStringList header;
header<<tr("姓名")<<tr("性别")<<tr("学号")<<tr("年级")<<tr("专业方向")<<tr("出生日期(年-月-日)")<<(tr("籍贯"))<<(tr("住址"));//<<tr("GPA");
//connect(table->horizontalHeader(),SIGNAL(sectionClicked(int)),this, SLOT(sortView(int)));
connect(table->horizontalHeader(),SIGNAL(sectionDoubleClicked(int)),this, SLOT(sortView(int)));
//table->horizontalHeader()->setDefaultSectionSize(15);
table->setHorizontalHeaderLabels(header);
//table->setItem(0,0,new QTableWidgetItem("嘤嘤嘤?"));
//table->setItem(0,1,new QTableWidgetItem("嘤嘤嘤?"));
//table->show();
layout->addWidget(table);
widget->setLayout(layout);
for(int i = 0; i < table->columnCount();i++)
c[i] = true;
}
void MainWindow::newFile(){
qDebug()<<"newFile";
if(table->rowCount()>0&&!saved){
QMessageBox msgBox;
msgBox.setWindowTitle("请注意!");
msgBox.setText(tr("文件尚未保存!"));
msgBox.setInformativeText(tr("是否保存文件?"));
//msgBox.setDetailedText(tr("Differences here..."));
msgBox.setStandardButtons(QMessageBox::Save
| QMessageBox::Discard
| QMessageBox::Cancel);
msgBox.setButtonText (QMessageBox::Save,QString("确 定"));
msgBox.setButtonText (QMessageBox::Discard,QString("舍 弃"));
msgBox.setButtonText (QMessageBox::Cancel,QString("取 消"));
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
qDebug() << "保存";
saveFile();
return;
case QMessageBox::Discard:
qDebug() << "丢弃";
tableInitialize();
break;
case QMessageBox::Cancel:
qDebug() << "取消";
return;
}
}
}
void MainWindow::openFile(){
qDebug()<<"openFile";
if(table->rowCount()>0&&!saved){
QMessageBox msgBox;
msgBox.setWindowTitle("请注意!");
msgBox.setText(tr("文件尚未保存!"));
msgBox.setInformativeText(tr("是否保存文件?"));
//msgBox.setDetailedText(tr("Differences here..."));
msgBox.setStandardButtons(QMessageBox::Save
| QMessageBox::Discard
| QMessageBox::Cancel);
msgBox.setButtonText (QMessageBox::Save,QString("确 定"));
msgBox.setButtonText (QMessageBox::Discard,QString("舍 弃"));
msgBox.setButtonText (QMessageBox::Cancel,QString("取 消"));
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
qDebug() << "保存";
saveFile();
return;
case QMessageBox::Discard:
qDebug() << "丢弃";
break;
case QMessageBox::Cancel:
qDebug() << "取消";
return;
}
}
QString path = QFileDialog::getOpenFileName(this,
tr("Open File"),
".",
tr("Text Files(*.csv)"));
if(!path.isEmpty()) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("Read File"),
tr("Cannot open file:\n%1").arg(path));
return;
}
QTextStream in(&file);
//QtextEdit->setText(in.readAll());
tableInitialize();
while(!in.atEnd()){
QStringList str = in.readLine().split(",");
//table->setRowCount(table->rowCount()+1);
addModify();
for(int i = 0; i < table->columnCount();i++){
table->setItem(table->rowCount()-1,i,new QTableWidgetItem(str[i]));
//table->setItem(table->rowCount()-1,0,new QTableWidgetItem(str[0]));
//table->setItem(table->rowCount()-1,1,new QTableWidgetItem(str[1]));
//table->setItem(table->rowCount()-1,2,new QTableWidgetItem(str[2]));
//table->setItem(table->rowCount()-1,3,new QTableWidgetItem(str[3]));
//table->setItem(table->rowCount()-1,4,new QTableWidgetItem(str[4]));
//table->setItem(table->rowCount()-1,0,new QTableWidgetItem(str[5]));
}
}
file.close();
saved = true;
} /*else {
QMessageBox::warning(this, tr("Path"),
tr("You did not select any file."));
}*/
}
void MainWindow::saveFile(){
qDebug()<<"saveFile";
QString path = QFileDialog::getSaveFileName(this,
tr("Open File"),
".",
tr("Text Files(*.csv)"));
if(!path.isEmpty()) {
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("Write File"),
tr("Cannot open file:\n%1").arg(path));
return;
}
QTextStream out(&file);
out.setCodec("uft-8");
for(int i = 0; i < table->rowCount();i++){
for(int j = 0; j < table->columnCount();j++){
//out << i << j;
//out << table->item(i,j)->text().toStdString().data();
//
if(table->item(i,j)!=NULL){
out << table->item(i,j)->text();//.toStdString().c_str();
qDebug()<<table->item(i,j)->text();//.toStdString().c_str();
}
else
out << itemEmpty;
if(j < table->columnCount() - 1)
out << ',';
}
out << endl;
}
//out << textEdit->toPlainText();
file.close();
saved = true;
} else {
QMessageBox::warning(this, tr("请注意!"),
tr("文件未保存!"));
}
qDebug()<<"over!";
}
void MainWindow::addModify(){
qDebug()<<"addModify";
saved = false;
table->setRowCount(table->rowCount()+1);
for(int i = 0; i < table->columnCount();i++)
table->setItem(table->rowCount()-1,i,new QTableWidgetItem(""));
QComboBox*tmp = new QComboBox();
tmp->setEditable(false);
//connect(tmp,SIGNAL(currentIndexChanged(QString)),this,SLOT(print_s()));
table->setItemDelegateForColumn(1,new Delegate(this));
table->setItemDelegateForColumn(4,new Delegate(this));
table->setItemDelegateForColumn(5,new DateDelegate(this));
// table->setIte
//table->item(table->rowCount()-1,1)->setFlags(Qt::NoItemFlags);
QComboBoxList->push_back(tmp);
}
void MainWindow::deleteModify(){
qDebug()<<"deleteModify";
if (table->currentRow() != -1)
table->removeRow(table->currentRow());
}
void MainWindow::moveUpModify(){
qDebug()<<"moveUpModify";
if(table->currentRow() > 0){
saved = false;
Swap(table->currentRow(), table->currentRow()-1,0);
table->setCurrentCell(table->currentRow()-1,table->currentColumn());
}
}
void MainWindow::moveDownModify(){
qDebug()<<"moveDownModify";
saved = false;
if(table->currentRow() < table->rowCount()-1){
Swap(table->currentRow(), table->currentRow()+1,0);
//table->selectRow(table->currentRow()+1);
//table->item(table->currentRow(),table->currentColumn())->setSelected(false);
table->setCurrentCell(table->currentRow()+1,table->currentColumn());
//table->item(table->currentRow()+1,table->currentColumn())->setSelected(true);
}
}
void MainWindow::sortView(int columnIndex){
qDebug()<<"sortView"<<columnIndex;
saved = false;
quickSort(0, table->rowCount() - 1,columnIndex);
c[columnIndex]=!c[columnIndex];
}
void MainWindow::searchView(){
qDebug()<<"searchView";
searchDialog = new dialog;
searchDialog->show();
}
void MainWindow::tableModify(int row,int column){
qDebug()<<"modify";
qDebug()<<row;
qDebug()<<column;
saved = false;
}
void MainWindow::gradeStatistic(){
qDebug()<<"grade";
}
void MainWindow::majorStatistic(){
qDebug()<<"major";
}
void MainWindow::gpaStatistic(){
qDebug()<<"gpa";
}
MainWindow::~MainWindow()
{
delete ui;
delete openAction;
delete saveAction;
delete newAction;
delete openAction;
delete saveAction;
delete addAction;
delete deleteAction;
//delete sortAction;
delete searchAction;
delete gradeAction;
delete majorAction;
delete gpaAction;
delete stuDB;
delete widget;
delete table;
}
void MainWindow::Swap(int p1,int p2, int column) {
for(int i = 0; i < table->columnCount();i++){
// QString tmp = table->item(p1,i)->text();
//QTableWidgetItem *tmp = table->item(p1,i);
//table->item(p1,i)->setText(table->item(p2,i)->text());
//table->item(p2, i)->setText(tmp);
QTableWidgetItem* t1 = NULL;
QTableWidgetItem* t2 = NULL;
if(table->item(p1,i)!=NULL)
t1 = table->item(p1,i)->clone();
if(table->item(p2,i)!=NULL)
t2 = table->item(p2,i)->clone();
table->setItem(p1,i,t2);
table->setItem(p2,i,t1);
}
}
int MainWindow::partition(int p, int q, int column){
string pivot = table->item(q, column)->text().toStdString();
int i = p - 1;
for(int j = p; j <= q - 1;j++) {
if(c[column]&&(table->item(j,column)->text().toStdString()<=pivot)) {
i++;
Swap(j,i,column);
}
else if((!c[column])&&(table->item(j,column)->text().toStdString()>=pivot)){
i++;
Swap(j,i,column);
}
}
Swap(q,i+1,column);
return i+1;
}
void MainWindow::quickSort(int p, int q,int column){
qDebug()<<c[column]<<column;
if(p < q){
int r = partition(p,q, column);
quickSort(p, r - 1, column);
quickSort(r + 1, q, column);
}
}
void MainWindow::contextMenuEvent(QContextMenuEvent *event){
qDebug()<<event->pos();
qDebug()<<table->pos();
qDebug()<<(table->pos().x())<<' '<<table->pos().y()+ table->horizontalHeader()->height()*2 + ui->mainToolBar->height();
//qDebug()<<QCursor::pos().x()<<' '<<QCursor::pos().y();
if((event->pos().x() > table->pos().x())&&
(event->pos().y()>(table->pos().y()+ table->horizontalHeader()->height()*2 + ui->mainToolBar->height()))
&&(event->pos().x()<(table->pos().x()+table->horizontalHeader()->width()))
&&(event->pos().y()<(table->pos().y()+ table->horizontalHeader()->height()*2+table->rowHeight(0)*table->rowCount() + ui->mainToolBar->height()))){
QMenu *menu = new QMenu(this);
QAction *moveUp = new QAction(tr("&上移一行"));
menu->addAction(moveUp);
connect(moveUp,SIGNAL(triggered()),this,SLOT(moveUpModify()));
QAction *moveDown = new QAction(tr("&下移一行"));
menu->addAction(moveDown);
connect(moveDown,SIGNAL(triggered()),this,SLOT(moveDownModify()));
QAction *insertBefore = new QAction(tr("&上方插入"));
menu->addAction(insertBefore);
connect(insertBefore,SIGNAL(triggered()),this,SLOT(insertBefore()));
QAction *insertAfter = new QAction(tr("&下方插入"));
menu->addAction(insertAfter);
connect(insertAfter,SIGNAL(triggered()),this,SLOT(insertAfter()));
QAction *deleteLine = new QAction(tr("&删除该行"));
menu->addAction(deleteLine);
connect(deleteLine,SIGNAL(triggered()),this,SLOT(deleteLine()));
menu->exec(QCursor::pos());
//menu->show();
}
}
void MainWindow::insertBefore(){
qDebug()<<"insertB";
saved = false;
table->insertRow(table->currentRow());
table->setCurrentCell(table->currentRow()-1,table->currentColumn());
}
void MainWindow::insertAfter(){
qDebug()<<"insertA";
saved = false;
table->insertRow(table->currentRow()+1);
table->setCurrentCell(table->currentRow()+1,table->currentColumn());
}
void MainWindow::deleteLine(){
qDebug()<<"deleteL";
saved = false;
qDebug()<<table->currentRow();
table->removeRow(table->currentRow());
}
void MainWindow::print_s(){
}
void MainWindow::searchQlineEditChanged(QString qstr){
qDebug()<<"searchQlineEdit";
qDebug()<<qstr;
searchQstr.clear();
searchRow = -1;
searchColumn = -1;
searchQstr = searchQLineEditToolBar->text();
qDebug()<<searchQstr;
qDebug()<<flush;
}
void MainWindow::searchToolBarTriggered(){
qDebug()<<"searchToolBar";
if(searchQstr == "")return;
for(int i = 0; i < table->rowCount();i++){
for(int j = 0; j < table->columnCount();j++){
if(table->item(i,j)!=NULL && table->item(i,j)->text()==searchQstr){
table->setCurrentCell(i,j);
searchRow = i;
searchColumn=j;
return;
}
}
}
QMessageBox::about(NULL, tr("抱歉!"), tr("未找到!请更换关键词!"));
}
void MainWindow::nextToolBarTriggered(){
qDebug()<<"nextToolBar";
if(searchRow==-1||searchColumn==-1){
searchToolBarTriggered();
return;
}
for(int i = searchRow; i < table->rowCount();i++){
for(int j = 0; j < table->columnCount();j++){
qDebug()<<table->item(i,j)->text();
qDebug()<<i<<" "<<j;
qDebug()<<table->rowCount()<<" "<<table->columnCount();
if(table->item(i,j)!=NULL &&
(i!=searchRow || j != searchColumn)&&
table->item(i,j)->text()==searchQstr){
table->setCurrentCell(i,j);
searchRow = i;
searchColumn = j;
return;
}
}
}
QMessageBox::about(NULL, tr("抱歉!"), tr("没有下一个了!"));
}
void MainWindow::lastToolBarTriggered(){
qDebug()<<"lastToolBar";
if(searchRow==-1||searchColumn==-1){
searchToolBarTriggered();
return;
}
for(int i = searchRow; i >=0; i--){
for(int j = table->columnCount()-1; j >=0; j--){
if(table->item(i,j)!=NULL &&
(i!=searchRow || j != searchColumn)&&
table->item(i,j)->text()==searchQstr){
table->setCurrentCell(i,j);
searchRow = i;
searchColumn = j;
return;
}
}
}
QMessageBox::about(NULL, tr("抱歉!"), tr("没有上一个了!"));
/*
QDialog *d = new QDialog(this);
d->setWindowTitle("");
//d->setWindowFlags(Qt::WindowCloseButtonHint);
//d->setWindowFlags(Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint);
QLabel *q = new QLabel(tr("没有下一个了!"));
QHBoxLayout * qh = new QHBoxLayout;
qh->addWidget(q);
d->setLayout(qh);
d->exec();
*/
}
/*
void MainWindow::qtClose(){
quit();
}
*/
void MainWindow::closeEvent(QCloseEvent *event){
if(table->rowCount()>0&&!saved){
QMessageBox msgBox;
msgBox.setWindowTitle("请注意!");
msgBox.setText(tr("文件尚未保存!"));
msgBox.setInformativeText(tr("是否保存文件?"));
//msgBox.setDetailedText(tr("Differences here..."));
msgBox.setStandardButtons(QMessageBox::Save
| QMessageBox::Discard
| QMessageBox::Cancel);
msgBox.setButtonText (QMessageBox::Save,QString("确 定"));
msgBox.setButtonText (QMessageBox::Discard,QString("舍 弃"));
msgBox.setButtonText (QMessageBox::Cancel,QString("取 消"));
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
qDebug() << "保存";
saveFile();
event->accept();
case QMessageBox::Discard:
qDebug() << "丢弃";
event->accept();
break;
case QMessageBox::Cancel:
qDebug() << "取消";
event->ignore();
return;
}
}
}
student.cpp
#include"student.h"
const string student::getName(){
return name;
}
const string student::getOrientation(){
return orientation;
}
const int student::getId(){
return id;
}
const int student::getAge(){
return age;
}
const int student::getGPA(){
return GPA;
}
const int student::getGPARank(){
return GPARank;
}
bool student::operator ==(student stu){
return (age==stu.age)
&& (name==stu.name)
&& (orientation==stu.orientation)
&&(id == stu.id)
&&(GPA==stu.GPA)
&&(GPARank == stu.GPARank);
}
bool student::operator ==(string name){
return this->name == name;
}
bool student::operator ==(int id){
return this->id == id;
}
studentdatabase.cpp
#include<algorithm>
using namespace std;
#include"studentdatabase.h"
const student studentDatabase::getStudent(int index){
return stu[index];
}
const int studentDatabase::getSize(){
return stu.size();
}
void studentDatabase::addStudent(){
student *tmp = new student();
stu.push_back(*tmp);
delete tmp;
}
bool studentDatabase::addStudent(student stu){
if(findStudent(stu)==-1)return false;
this->stu.push_back(stu);
sortByGPA();
}
//bool studentDatabase::addStudent(student stu);
bool studentDatabase::deleteStudent(int index){
stu.erase(stu.begin()+index);
}
bool studentDatabase::deleteStudent(student stu){
int index = findStudent(stu);
if(index==-1)return false;
else{
this->stu.erase(this->stu.begin() + index);
return true;
}
}
const int studentDatabase::findStudent(student stu){
for(int i = 0;i < this->stu.size();i++){
if(this->stu[i]==stu)
return i;
}
return -1;
}
const int studentDatabase::findStudent(string name){
for(int i = 0;i<this->stu.size();i++){
if(this->stu[i]==name)
return i;
}
return -1;
}
const int studentDatabase::findStudent(int id){
for(int i = 0;i<this->stu.size();i++){
if(this->stu[i]==id)
return i;
}
return -1;
}
bool studentDatabase::clear(){
stu.clear();
}
bool sortCmp(student s1, student s2){
return s1.getGPA()<s2.getGPA();
}
void studentDatabase::sortByGPA(){
sort(stu.begin(),stu.end(),sortCmp);
}
mainwindow.ui