本项目以及配套数据库已经上传到优快云,大家可根据需要下载:
- 项目
https://download.youkuaiyun.com/download/qq_41544842/14934412 - 数据库
https://download.youkuaiyun.com/download/qq_41544842/14934435
我们发现在添加/修改如性别、学院等字段时,数据的内容是有要求的,是不可以由操作者随意改变的。因此,我们最好在表格中为这些字段加上下拉框。除此之外,在书籍的表格中,摘要这个字段最好换成长文本格式。
首先新建tabviewDelegate.h和tabviewDelegate.cpp
tabviewDelegate.h:
#ifndef TABVIEWDELEGATE_H
#define TABVIEWDELEGATE_H
#include <QItemDelegate>
#include <QComboBox>
class tabviewDelegate: public QItemDelegate
{
public:
tabviewDelegate(QObject *parent = 0);
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;
void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const;
QString type;
};
#endif // TABVIEWDELEGATE_H
tabviewDelegate.cpp
#include "tabviewDelegate.h"
#include <QTextEdit>
#include <QDebug>
tabviewDelegate::tabviewDelegate(QObject *parent)
{}
QWidget *tabviewDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{//设置单元格格式,编写下拉框的内容
if(type == "sex"){
QComboBox *editor = new QComboBox(parent);
editor->addItem(tr("男"));
editor->addItem(tr("女"));
return editor;
}
else if(type=="status"){
QComboBox *editor = new QComboBox(parent);
editor->addItem(tr("已借出"));
editor->addItem(tr("未借出"));
return editor;
}
else if(type=="warning"){
QComboBox *editor = new QComboBox(parent);
editor->addItem(tr("yes"));
editor->addItem(tr("no"));
return editor;
}
else if(type=="department"){
QComboBox *editor = new QComboBox(parent);
editor->addItem(tr("艺术学院"));
editor->addItem(tr("药学院"));
editor->addItem(tr("建筑与环境学院"));
editor->addItem(tr("临床医学院"));
editor->addItem(tr("生命科学学院"));
editor->addItem(tr("化学学院"));
editor->addItem(tr("历史文化学院"));
editor->addItem(tr("公共卫生学院"));
editor->addItem(tr("经济学院"));
editor->addItem(tr("机械工程学院"));
editor->addItem(tr("基础医学与法医学院"));
editor->addItem(tr("物理学院"));
editor->addItem(tr("电气工程学院"));
editor->addItem(tr("马克思主义学院"));
editor->addItem(tr("水利水电学院"));
editor->addItem(tr("商学院"));
editor->addItem(tr("材料科学与工程学院"));
editor->addItem(tr("数学学院"));
editor->addItem(tr("高分子科学与工程学院"));
editor->addItem(tr("口腔医学院"));
editor->addItem(tr("文学与新闻学院"));
editor->addItem(tr("轻工科学与工程学院"));
editor->addItem(tr("化学工程学院"));
editor->addItem(tr("外国语学院"));
editor->addItem(tr("国际关系学院"));
editor->addItem(tr("公共管理学院"));
editor->addItem(tr("电子信息学院"));
editor->addItem(tr("法学院"));
editor->addItem(tr("空天科学与工程学院"));
editor->addItem(tr("计算机学院"));
editor->addItem(tr("体育学院"));
editor->addItem(tr("网络空间安全学院"));
editor->addItem(tr("软件学院"));
editor->addItem(tr("灾后重建学院"));
return editor;
}
else{//书籍摘要那一列,长文本格式
QTextEdit *editor = new QTextEdit(parent);
return editor;
}
}
void tabviewDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{//把表格的列和上个函数编写的类型进行对应。哪个类型是下拉框,哪个类型是长文本
QString text =index.model()->data(index,Qt::DisplayRole).toString();
if(type == "sex"||type == "status"||type == "warning"||type == "department"){
QComboBox *cmb = static_cast<QComboBox*>(editor);
cmb->setCurrentText(text);
}
else{
QTextEdit *textedit = static_cast<QTextEdit*>(editor);
textedit->setText(text);
}
}
void tabviewDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{//单元格获取从下拉框中选择的那个数据
QString text;
if(type == "sex"||type == "status"||type == "warning"||type == "department"){
QComboBox *cmb = static_cast<QComboBox*>(editor);
text= cmb->currentText();
}
else{
QTextEdit *edit = static_cast<QTextEdit*>(editor);
text= edit->toPlainText();
}
model->setData(index,text);
}
void tabviewDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
该功能用于mainwindow这个界面,因此:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle(QStringLiteral("多功能书籍管理系统"));
model = new QSqlTableModel(this);
model->setTable("books");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model2 = new QSqlTableModel(this);
model2->setTable("users");
model2->setEditStrategy(QSqlTableModel::OnManualSubmit);
model2->select();
setTitle();
ReadOnlyDelegate * readOnlyDelegate = new ReadOnlyDelegate();
ui->tableView->setModel(model);
ui->tableView->verticalHeader()->setVisible(false);
ui->tableView->setItemDelegateForColumn(0, readOnlyDelegate);
ui->tableView->setColumnWidth(4,110);
ui->tableView->setColumnWidth(6,150);
ui->tableView_2->setModel(model2);
ui->tableView_2->verticalHeader()->setVisible(false);
ui->tableView_2->setItemDelegateForColumn(0, readOnlyDelegate);
ui->tableView_2->setColumnWidth(1,100);
//将表格的列和编写的类型进行对应,注意看好列数,以及看好这个字段属于哪个表
tabviewDelegate *textedit = new tabviewDelegate(this);
//books表中的摘要字段
ui->tableView->setItemDelegateForColumn(6,textedit);
tabviewDelegate *cmb = new tabviewDelegate(this);
cmb->type = "sex";//users表中的性别字段
ui->tableView_2->setItemDelegateForColumn(4,cmb);
tabviewDelegate *cmb2 = new tabviewDelegate(this);
cmb2->type = "status";//books表中的借还状态字段
ui->tableView->setItemDelegateForColumn(7,cmb2);
tabviewDelegate *cmb3 = new tabviewDelegate(this);
cmb3->type = "warning";//users表中的检验超期字段
ui->tableView_2->setItemDelegateForColumn(7,cmb3);
tabviewDelegate *cmb4 = new tabviewDelegate(this);//选择学院
cmb4->type = "department";//users表中的学院字段
ui->tableView_2->setItemDelegateForColumn(5,cmb4);
}
效果如下: