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. 不能解决,复选框在request对象获取的信息后显示在用户信息里面为中文的选项名

    因为方框里面value 不用中文?.? 假如用中文呢?  完全可以!!已经试验 如果不用中文,那么中文可以用对象的参数来获得,即在login.jsp中就要用javabean类属性

  2. nyoj-130-相同的雪花(hash)

    题目链接 /* Name:NYOJ-130-相同的雪花 Copyright: Author: Date: 2018/4/14 15:13:39 Description: 将雪花各个分支上的值加起来,h ...

  3. pthread_getspecific()--读线程私有数据|pthread_setspecific()--写线程私有数据

    原型: #include <pthread.h> void *pthread_getspecific(pthread_key_t key); int pthread_setspecific ...

  4. RTP协议全解(H264码流和PS流)

    写在前面:RTP的解析,网上找了很多资料,但是都不全,所以我力图整理出一个比较全面的解析, 其中借鉴了很多文章,我都列在了文章最后,在此表示感谢. 互联网的发展离不开大家的无私奉献,我决定从我做起,希 ...

  5. Busybox shell脚本修改密码

    /****************************************************************************** * Busybox shell脚本修改密 ...

  6. Effective Python之编写高质量Python代码的59个有效方法

                                                         这个周末断断续续的阅读完了<Effective Python之编写高质量Python代码 ...

  7. LeetCode Maximum Average Subarray I

    原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/ 题目: Given an array con ...

  8. linux find -regex 使用正则表达式

    find之强大毋庸置疑,此处只是带领大家一窥find门径,更详细的说明见man  find和 info find.整篇文章循序渐进,从最常用的文件名测试项开始步步深入,到第六节基本讲完find处理文件 ...

  9. Pager分页

    分页组件: /// <summary> /// 分页组件 /// </summary> public class PagerHelper { /// <summary&g ...

  10. 怎么让eclipse调试的时候不进入 class文件中去

    Eclipse -> Window ->Preferences ->Java ->Debug "Suspend execution on uncaught excep ...