QAbstractListModel QAbstractTableModel 与 QItemDelegate关系

首先强调一下,上面三点内容是为qt框架的model/view架构服务的。model是数据层,顾名思义存储的是数据,包括文字、图片等内容,主要包括QAbstractListModel QAbstractTableModel 。view是展示层,用户看到的就是view层,主要包括qlistview qtableview。model需要在view中呈现出来,这时候就需要渲染,即代理。这部分工作是由QItemDelegate负责的。

1,如果将model view delegate关联起来呢?

[virtual] void QAbstractItemView::setModel(QAbstractItemModel *model)
void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate)

2,如何渲染呢?

要知道对于table和list中数据展示,不仅形式多种多样,而且还有位置、对齐、尺寸等很多内容需要兼顾,如何把握好呢?--采用qitemdelegate对每一个单元格进行渲染。

[pure virtual] void QAbstractItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

This pure abstract function must be reimplemented if you want to provide custom rendering. Use the painter and style option to render the item specified by the item index.

If you reimplement this you must also reimplement sizeHint().

[virtual] void QItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

Reimplemented from QAbstractItemDelegate::paint().

Renders the delegate using the given painter and style option for the item specified byindex.

When reimplementing this function in a subclass, you should update the area held by the option's rect variable, using the option's state variable to determine the state of the item to be displayed, and adjust the way it is painted accordingly.

重载QItemDelegate的paint函数进行内容绘制,此时图片采用drawimage,文字采用drawtext,分析此函数,重点是明白三个参数的具体意义。

首先先去领悟一下官方文档中的解释。

对于第1个参数painter,画笔。无需多言,完成view的绘制;

对于第2个参数option,类型是QStyleOptionViewItem。包含了描述一个widget所需要的所有参数,比如位置和尺寸rect,状态state、画板palette。用法如:

option.state & QStyle::State_Selected

option.rect.width()

option.palette.highlightedText()

QStyleOption类的作用就是用于显示用的。该类的成员变量有,QRect,QFont等。都是用于控制item的显示使用的。

QRect,用于控制item的大小。QFont,用于控制item的字体。QPalette,用于控制item的颜色。

对于第3个参数index,用来定位model中的数据。其中column()获取当前index指向的item在第几列,row()获取当前index指向的item在第几行,data()获取当前item中的数据。

3,重载paint完成自定义渲染看似可以解决在单元格内指定位置处放置恰当控件的问题,但并不完全适用。因为painter只能drawText drawImage等一部分控件,对于painter不能绘制的控件,例如QMovie等,就无法实现将其显示在单元格内。面对此类型问题,可以采用自定义单元格控件的方法解决。看如下一段代码:

QStandardItemModel model(4, 2);
    QTableView tableView;
    tableView.horizontalHeader()->setDefaultSectionSize(162);
    tableView.verticalHeader()->setDefaultSectionSize(188);
    tableView.setModel(&model);
 
    CSpinBoxDelegate delegate;
    tableView.setItemDelegate(&delegate);
 
    for (int row = 0; row < 4; ++row) {
        for (int column = 0; column < 2; ++column) {
            QModelIndex index = model.index(row, column, QModelIndex());
            if (0 == row % 2)
            {
                CCatcherView* view = new CCatcherView;
                tableView.setIndexWidget(index, view);
            }
            else
            {
                model.setData(index, QVariant(row + column));
            }
        }
    }
调用QAbstractItemView类的setIndexWidget方法,在指定的index处插入合适的自定义widget。自定义widget内可以包括已经按照需求布局好的各类控件。如,左上角放一个label,右下角放一个button等等。该处理方法会造成单元格不可编辑,造成无法通过editor函数组来进行单元格内容和控件的更新。鉴于此,可以通过信号-槽或者绑定事件处理函数的方法对view对象进行处理。

4,editor函数组。

有四个函数:

//创建编辑器
    QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) 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;

QT之model-delegat-model---QAbstractListModel QAbstractTableModel 与 QItemDelegate关系相关推荐

  1. 学习QT之模型(Model)

    实现自定义模型可以通过QAbstractItemModel类继承,也可以通过QAbstractListModel和QAbstractTableModel类继承实现列表模型或者表格模型. 在数据库中,通 ...

  2. Pytorch: model.eval(), model.train() 讲解

    文章目录 1. model.eval() 2. model.train() 两者只在一定的情况下有区别:训练的模型中含有dropout 和 batch normalization 1. model.e ...

  3. 类似索引Model套Model之 iOS模型闲聊二

    看下界面, 这是类似于索引的页面, 只不过木有右侧索引条的布局. 如果想了解通讯录索引的,请移步iOS - 高仿通讯录之商品索引排序搜索. 提供思路如下: 分析界面及接口 用 MVC 设计模式来实现( ...

  4. Pytorch的model.train() model.eval() torch.no_grad() 为什么测试的时候不调用loss.backward()计算梯度还要关闭梯度

    使用PyTorch进行训练和测试时一定注意要把实例化的model指定train/eval model.train() 启用 BatchNormalization 和 Dropout 告诉我们的网络,这 ...

  5. Model、ModelMap、Map有什么关系?深入底层剖析

    Model.ModelMap.Map有什么关系? 首先为形参赋值的对象是同一个对象,也就是BindingAwareModelMap Model是一个接口 操作模型数据最顶层的接口 Map就是JDK中的 ...

  6. 第15.22节 PyQt(Python+Qt)入门学习:Model/View架构详解

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.简介 在PyQt和Qt中,Model/View架构是图形界面开发时用于管理数据和界面展现方式的关 ...

  7. Qt中QAbstractTableModel、QItemDelegate的联合使用

    1.继承QAbstractTableModel实现CurrencyModel,需要重写的函数有 introwCount(constQModelIndex&parent)const; //返回表 ...

  8. SharePoint Add-in Model (App Model) 介绍 – 概念、托管方式、开发语言

    SharePoint Add-in Model 是自 2013 版本以来引入的新的扩展性开发模型, SharePoint 开发者可以利用这种新模型来实现往常利用场解决方案 (Farm Solution ...

  9. php 初始化model,TP5 model类研究

    model类研究 仅做开发笔记,若有不足之处,请指出 简介 我们都知道thinkphp5增删改查的函数返回的都是模型对象 他与tp3.2是有区别的,现在主要研究该模型的运行机制和调用方法. 那么问题来 ...

最新文章

  1. linux进程间通信:shell管道 | 的实现
  2. 将Centos的yum源更换为国内的阿里云源
  3. RHCE 学习笔记(22) 网络用户
  4. 假笨说-我是如何走上JVM这条贼船的
  5. 笔记-中项案例题-2019年下-信息系统安全管理
  6. mysql手工注入——盲注
  7. Android:数据库增删改查、SQLite、ORM、Cursor
  8. PDH光端机常见故障及解决方法介绍
  9. ...android平板办公,教科书式安卓全 面屏平板:华为MatePad Pro构建智慧办公新体验...
  10. recv和send函数
  11. Spring AOP原理分析(一)-- AOP相关概念
  12. python代码加密
  13. android版本高低有啥好处与不好,WP跟安卓比流畅 但为什么就不好用呢?
  14. Mac 使用 扫描 仪
  15. windows 用choco 安装nvm
  16. 提醒:无线路由曝安全漏洞,请关闭WPS功能
  17. 墨者靶场-SQL手工注入漏洞测试(MySQL数据库-字符型)
  18. 微信公众号的简单常识
  19. 公厕智能离子净化器有多种除臭净味技术
  20. 模式识别——0.绪论

热门文章

  1. 使用SQL计算AUC值
  2. 连续系统的动态规划问题
  3. 小白入门:a在微信给好友b点赞了。b看到了点赞。但是却没有消息提示b,说a点赞了。这个要怎么定位问题?...
  4. 题解 CF186A 【Comparing Strings】
  5. leetcode 883. 三维形体投影面积(python)
  6. python各种编辑器、APP、软件下载
  7. 重庆新地标佛罗伦萨小镇将开业;雅高宣布2021年开业新酒店计划;阅文集团出售懒人听书股权​ | 美通企业周刊...
  8. MP4视频边播放边缓存
  9. 2021秋冬上海时装周开启,聚焦8大国内外品牌
  10. 最是人间留不住,朱颜辞镜花辞树