关键是重写这四个函数:

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;

程序运行截图如下:

代码如下:

combodelegate.h

#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H#include <QItemDelegate>class ComboDelegate:public QItemDelegate
{Q_OBJECT
public:ComboDelegate(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;
};#endif // COMBODELEGATE_H

datedelegate.h

#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H#include <QItemDelegate>class DateDelegate:public QItemDelegate
{Q_OBJECT
public:DateDelegate(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;
};#endif // DATEDELEGATE_H

passportdelegate.h

#ifndef PASSPORTDELEGATE_H
#define PASSPORTDELEGATE_H#include <QItemDelegate>
#include <QLabel>class PassportDelegate:public QItemDelegate
{Q_OBJECT
public:PassportDelegate(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;
};#endif // PASSPORTDELEGATE_H

spindelegate.h

#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H#include <QItemDelegate>class SpinDelegate:public QItemDelegate
{Q_OBJECT
public:SpinDelegate(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;
};#endif // SPINDELEGATE_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private:Ui::Widget *ui;
};#endif // WIDGET_H

combodelegate.cpp

#include "combodelegate.h"
#include <QComboBox>
#include <QDebug>ComboDelegate::ComboDelegate(QObject *parent):QItemDelegate(parent)
{}QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QComboBox *editor=new QComboBox(parent);editor->addItem("程序员");editor->addItem("网管");editor->addItem("修电脑的");editor->addItem("送水的");editor->installEventFilter(const_cast<ComboDelegate*>(this));return editor;
}void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{QString str=index.model()->data(index).toString();QComboBox *box=static_cast<QComboBox*>(editor);int i=box->findText(str);box->setCurrentIndex(i);
}void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QComboBox *box=static_cast<QComboBox*>(editor);QString str=box->currentText();model->setData(index,str);
}void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{editor->setGeometry(option.rect);
}

datedelegate.cpp

#include "datedelegate.h"
#include <QDateTimeEdit>
#include <QDebug>DateDelegate::DateDelegate(QObject *parent):QItemDelegate(parent)
{}QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QDateTimeEdit *editor=new QDateTimeEdit(parent);editor->setDisplayFormat("yyyy-MM-dd");editor->setCalendarPopup(true);editor->installEventFilter(const_cast<DateDelegate*>(this));return editor;
}void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{QString dateStr=index.model()->data(index).toString();QDate date=QDate::fromString(dateStr,Qt::ISODate);QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor);edit->setDate(date);
}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)));
}void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{editor->setGeometry(option.rect);
}

main.cpp

#include "widget.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

passportdelegate.cpp

#include "passportdelegate.h"
#include <QCheckBox>
#include <QDebug>
#include <QFont>PassportDelegate::PassportDelegate(QObject *parent):QItemDelegate(parent)
{}QWidget *PassportDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QCheckBox *checkBox=new QCheckBox(parent);checkBox->setText("是否通过");QFont font;font.setBold(true);font.setPixelSize(18);checkBox->setFont(font);return checkBox;
}void PassportDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{QString str=index.model()->data(index).toString();QCheckBox *checkBox=static_cast<QCheckBox*>(editor);if(str=="通过"){checkBox->setChecked(Qt::Checked);}else{checkBox->setChecked(Qt::Unchecked);}
}void PassportDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QCheckBox *checkBox=static_cast<QCheckBox*>(editor);if(checkBox->isChecked()){model->setData(index,QString("通过"));}else{model->setData(index,QString("不通过"));}
}void PassportDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{editor->setGeometry(option.rect);
}

spindelegate.cpp

#include "spindelegate.h"
#include <QSpinBox>SpinDelegate::SpinDelegate(QObject *parent):QItemDelegate(parent)
{}QWidget *SpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QSpinBox *editor=new QSpinBox(parent);editor->setRange(0,100000);editor->installEventFilter(const_cast<SpinDelegate*>(this));return editor;}void SpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{int value=index.model()->data(index).toInt();QSpinBox *box=static_cast<QSpinBox*>(editor);box->setValue(value);
}void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QSpinBox *box=static_cast<QSpinBox*>(editor);int value=box->value();model->setData(index,value);
}void SpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{editor->setGeometry(option.rect);
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "combodelegate.h"
#include "datedelegate.h"
#include "spindelegate.h"
#include "passportdelegate.h"
#include <QStandardItem>
#include <QStandardItemModel>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);QStandardItemModel *model=new QStandardItemModel;model->setColumnCount(5);ComboDelegate *comboDelegate=new ComboDelegate;DateDelegate *dateDelegate=new DateDelegate;SpinDelegate *spinDelegate=new SpinDelegate;PassportDelegate *passportDelegate=new PassportDelegate;model->setHeaderData(0,Qt::Horizontal,"姓名");model->setHeaderData(1,Qt::Horizontal,"职业");model->setHeaderData(2,Qt::Horizontal,"出生日期");model->setHeaderData(3,Qt::Horizontal,"薪资");model->setHeaderData(4,Qt::Horizontal,"审核状态");model->insertRow(0,new QStandardItem("猪小明"));ui->tableView->setModel(model);ui->tableView->setItemDelegateForColumn(1,comboDelegate);ui->tableView->setItemDelegateForColumn(2,dateDelegate);ui->tableView->setItemDelegateForColumn(3,spinDelegate);ui->tableView->setItemDelegateForColumn(4,passportDelegate);
}Widget::~Widget()
{delete ui;
}

Qt工作笔记-对QItemDelegate自定义委托的理解相关推荐

  1. Qt工作笔记-代理及自定义委托,实现开关功能

    整体源码下载如下: https://download.csdn.net/download/qq78442761/10810431 效果图如下: 源码如下: adddialog.h #ifndef AD ...

  2. Qt工作笔记-QByteArray与自定义结构体之间的转化(可用于网络传输,以及数据回调)

    目录 理论 程序 理论 在Socket中,数据一般是char型(一般情况下,其实最好是用uchar来传输,避免莫名其妙的错误,但用char也是可以的),而在Qt里面可以使用QByteArray,这个在 ...

  3. Qt工作笔记-发送端发送Json格式的数据包,接收端解析数据包

    原理以及运行 原理是因为QJsonDocument提供了一个函数toJson 可以直接把Json数据转成QByteArray,这样就可以直接发送数据包,然后再按照常规方法解析即可,本源码中含解析内容, ...

  4. Qt工作笔记-QMainWindow自定义窗体中利用状态栏进行窗体放缩

    目录 原理 关键代码 原理 在QDialog和QWidget中一般使用重写: void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;void ...

  5. Qt工作笔记-自定义开关控件

    1.自定义开关控件: 2.点击有动画效果: 3.在动画效果中,不再响应信号: 运行截图如下: 输出响应信号: 源码如下: myonoff.h #ifndef MYONOFF_H #define MYO ...

  6. Qt工作笔记-依赖于QAbstractTableModel实现自定义TableModel

    目录 理论 源码 理论 设计模式里面有一句话叫 要依赖于抽象,不要依赖于具体. 但在Qt有些官方实例里,却依赖于具体实现某一子功能,这可以是为了方便把, 但在TableModel有一个依赖与抽象, 官 ...

  7. Qt工作笔记-自定义QSortFilterProxyModel实现多列分别过滤

    程序运行截图如下: 原理,重写QSortFilterProxyModel类中的filterAcceptsRow即可: 源码如下: mysortfilterproxymodel.h #ifndef MY ...

  8. Qt工作笔记-自定义菜单(右键菜单)

    所以,只要重写createWIdget()这个函数就阔以了! 把写好的界面放到这个parent里面不就阔以了! 写好的界面如下: 运行截图如下: 源码如下: mypopwidget.h #ifndef ...

  9. Qt工作笔记-自定义模型【继承QAbstractTableModel】

    程序运行截图如下: 代码如下: mymodel.h #ifndef MYMODEL_H #define MYMODEL_H#include <QAbstractTableModel> #i ...

最新文章

  1. C# Stream 和 byte[] 之间的转换
  2. WCF服务编程设计规范(6):队列服务、安全和服务总线
  3. pytorch安装-Windows(pip install失败)
  4. 【Paper】2015_Song_Consensus of Heterogeneous Agents with Linear Discrete Dynamics
  5. JNDI 和JDBC的区别
  6. Javascript 笔记(1)----函数
  7. [AtCoder Regular Contest 125] A-F全题解
  8. 武侠乂怎么修改服务器,武侠乂怎么操作 按键功能详细介绍
  9. Altium AD20修改原理图右下角标题栏,自定义标题栏,添加图片、标题、页码、时间、作者
  10. 前后端分离项目中,前端请求的常见问题
  11. 测试用例的设计方法及例子
  12. 风险模型—VaR模型2
  13. Apache Kafka核心组件和流程-协调器(消费者和组协调器)-设计-原理(入门教程轻松学)
  14. 蠕虫病毒html,蠕虫病毒的防治
  15. 计算机粘贴不起作用,教您复制粘贴不能用怎么解决
  16. 《缠中说禅108课》69:月线分段与上海大走势分析、预判
  17. 干货 | 使用京东云搭建视频直播网站
  18. PicPick 5.1.3 中文版,一个全功能的屏幕截图工具,图像编辑器,颜色选择器
  19. 2021练习题Python的
  20. 如何用excel做正交分析_如何在SPSS中进行正交设计及正交分析?

热门文章

  1. 除了陈真处外的深圳论坛SZ4J
  2. WSARecv参数lpNumberOfBytesRecvd的一个变态问题
  3. Nokia的北美困局
  4. adsl 路由器默认密码
  5. 无需破解,Python这个神器帮你免费获取资源,赶紧收藏!
  6. 原来咱们公司准备裁员还有一部份原因是这样的!
  7. 如何通过控制台访问openstack实例_如何通过seo提高网站设计的访问量
  8. 送书 | 数据分析如烹小鲜
  9. 关注 | 新冠病毒这次的突变毒株太可怕,与人受体亲和力提高了1000倍,传播提高70%!已经成为伦敦地区主要毒株...
  10. 免费Linux系统和生信宝典原创学习教程