用QT+MySQL编写图书管理系统(五)——在表格中添加下拉框、长文本格式

本文档介绍如何在Qt环境中,针对特定字段如性别、学院等使用下拉框,以及将书籍摘要字段设为长文本格式。通过创建`tabviewDelegate`类实现QTableView的自定义委托,确保数据输入的规范性。同时,展示了在`MainWindow`中如何应用这些自定义编辑器,使得用户在编辑表格时只能从预设选项中选择或输入长文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本项目以及配套数据库已经上传到优快云,大家可根据需要下载:

  1. 项目
    https://download.youkuaiyun.com/download/qq_41544842/14934412
  2. 数据库
    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);

}

效果如下:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值