用QT+MySQL編寫圖書管理系統(五)——在表格中新增下拉框、長文字格式
阿新 • • 發佈:2021-01-21
技術標籤:技術分享QTmysqlsql資料庫qtnavicat
我們發現在新增/修改如性別、學院等欄位時,資料的內容是有要求的,是不可以由操作者隨意改變的。因此,我們最好在表格中為這些欄位加上下拉框。除此之外,在書籍的表格中,摘要這個欄位最好換成長文字格式。
首先新建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);
}
效果如下: