Concepts
     不像MVC模式,Model/View模式并没有单独用来处理事件和用户交互的组件(controller)。通常,视图负责向用户呈现模型中的数据,并处理用户的输入。有时,为了让输入更加灵活,使用Delegate处理交互。Delegate组件提供输入功能,还负责渲染某个Item。Delegate的标准接口在QAbstractItemDelegate中定义。
     Delegate希望实现paint()和sizeHint()函数来呈现自己的内容。但是,简单的基于Widget的Delegate通过QItemDelegate子类化,而不是QAbstractItemDelegate,并且有这些函数的默认实现。
     Delegate的Editors可以用过Widget管理Edit过程或者直接处理事件,这两种方式实现。第一种方法将在 spin box delegate 例子中实现。
     Pixelato例子显示如何专门为TableView创建定制的Delegate。
 
Using an existing delegate
     Qt提供的标准视图都是通过QItemDelegate实例来实现编辑功能。默认的Delegate接口实现,以通常的样式渲染每个标准视图:QListView,QTreeView,QTableView。
     所有的standard roles都由标准视图使用的默认Delegate来处理。视图使用的Delegate由itemDelegate()函数返回。setItemDelegate()函数允许您为标准视图安装自定义Delegate。 
 
Delegate举例: 我们显示一个4x2的表格,表格中每个单元都是spinbox。
 
思考:
1.我们使用QTableView显示这个表格。
2.使用QStandardItemModel,初始化时指定4行2列。
3.渲染每个表格为spinbox,这里就需要QItemDelegate。
4.QItemDeleate实现了paint()和sizeHint()函数。我们不需要重新这2个函数。
5.每个表格变成spinbox,需要实现createEditor()函数。
6.表格中数据改变,需要告诉Model,实现setModelData()函数
7.表格中数据改变,显示也要跟着改变,实现setEditorData()函数。
8.最后数据改变,单元格的空间也可能改变,实现updataEditorGeometry()函数。
 
代码如下:
spinboxdelegate.h
#ifndef SPINBOXDELEGATE_H
#define SPINBOXDELEGATE_H #include <QItemDelegate> class SpinBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit SpinBoxDelegate(QObject *parent = );
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;
signals: public slots: }; #endif // SPINBOXDELEGATE_H

sinboxdelegate.cpp

#include "spinboxdelegate.h"

#include <QSpinBox>

SpinBoxDelegate::SpinBoxDelegate(QObject *parent) :
QItemDelegate(parent)
{
} QWidget* SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMaximum();
editor->setMinimum(); return editor;
}
void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QSpinBox *spinBox = static_cast<QSpinBox *>(editor); spinBox->interpretText();
int num = index.data(Qt::DisplayRole).toInt();
spinBox->setValue(num);
} void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QSpinBox *spinBox = static_cast<QSpinBox *>(editor);
int num = spinBox->value(); model->setData(index, QVariant(num), Qt::EditRole);
} void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}

main.cpp

#include <QTableView>
#include <QStandardItemModel>
#include <QModelIndex>
#include <QApplication> #include <spinboxdelegate.h> int main(int argc, char *argv[])
{
QApplication a(argc, argv); QStandardItemModel *standardModel = new QStandardItemModel(,);
QTableView *tableView = new QTableView; tableView->setModel(standardModel);
for(int row = ; row < ; row++) {
for(int col = ; col < ; col++) {
QModelIndex index = standardModel->index(row, col, QModelIndex());
standardModel->setData(index, QVariant((row+)*(col+)));
}
} SpinBoxDelegate *delegate = new SpinBoxDelegate;
tableView->setItemDelegate(delegate); tableView->show(); return a.exec();
}

程序效果如下:

      

 
 
 
 
 

9.Delegate类的更多相关文章

  1. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  2. iOS多播Delegate类——GCDMulticastDelegate用法小结

    iOS中通常的delegate模式只能有一个被委托的对象,这样当需要有多个被委托的对象时,实现起来就略为麻烦,在开源库XMPPFramework中提供了一个GCDMulticastDelegate类, ...

  3. 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)

    并发编程概述   前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...

  4. [C#] C# 知识回顾 - 委托 delegate

    C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...

  5. [转载]C#委托和事件(Delegate、Event、EventHandler、EventArgs)

    原文链接:http://blog.csdn.net/zwj7612356/article/details/8272520 14.1.委托 当要把方法作为实参传送给其他方法的形参时,形参需要使用委托.委 ...

  6. 【C#】委托-Delegate

    C# 委托(Delegate) C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针.委托(Delegate) 是存有对某个方法的引用的一种引用类型变量.引用可在运行时被改变. 委托 ...

  7. CLR via C#(12)-委托Delegate

    本来按照进度应该学习事件了,可总觉得应该委托在前,事件在后,才好理解. 委托是一个类,它提供了回调函数机制,而且是类型安全的.使用委托可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数 ...

  8. 转iOS中delegate、protocol的关系

    iOS中delegate.protocol的关系 分类: iOS Development2014-02-12 10:47 277人阅读 评论(0) 收藏 举报 delegateiosprocotolc ...

  9. c# 关键字delegate、event(委托与事件)[MSDN原文摘录][1]

    A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. ...

随机推荐

  1. uva10815(set的应用)

    紫书例题,这道题的例程让我长了知识.以前没有用过cctype和stringstream相关的东西.很实用,值得学习. #include <cctype>的函数 c++中应该是#includ ...

  2. Xcode9 修改工程名(含cocopods)

    由于需要现在要更改包名,但是在网上找了N多资料都比较老,16年的资料却是残缺不全,尤其 ios10 出了 .entitlement  的机制 ,很多琐碎的小细节 很容易忘记.所以我自己总结了一篇, 环 ...

  3. 普通平衡树 - Treap

    怕被学弟怼 : "你的博客上没有Treap模板啊?" #include <cstdio> #include <cstring> #include <a ...

  4. 使用virtualenv搭建python3的环境

    转:http://blog.csdn.net/liuchunming033/article/details/46008301 转:http://www.jb51.net/article/85527.h ...

  5. Azure上Linux VM误配防火墙的恢复方法

    在实际运维中,防火墙把自己挡在机器外面的情况会时有发生.如何快速的恢复对运维人员是很重要的. 本文将介绍如何用Azure Extension实现不通过ssh对VM进行操作的方法. 之前写过一遍Blog ...

  6. CPU 和 Linux 进程

    进程与线程 进程应该是Linux中最重要的一个概念.进程运行在CPU上,是所有硬件资源分配的对象.Linux中用一个task_struct的结构来描述进程,描述了进程的各种信息.属性.资源. Linu ...

  7. SpringMVC的环境搭建

    MyBatis框架-->持久层框架-->Object[对象]Relation[关系型数据库]Mapping[在MyBatis的体现是哪个映射文件中国的<resultMap>标签 ...

  8. Mybatis-Spring包学习

    MyBatis-Spring包用来将MyBatis无缝整合到Spring中.使用这个类库中的类, Spring将会加载必要的MyBatis工厂类和Session类. 这个类库也提供一个简单的方式来注入 ...

  9. (转)C#特性详解

    本文转载自:http://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类 ...

  10. Java-API-Package:javax.annotation

    ylbtech-Java-API-Package:javax.annotation 1.返回顶部 1. Package javax.annotation Enum Summary Resource.A ...