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. 剑指offer--5.变态跳台阶

    WA了一次,错误数据4,输出8,怎么真么熟悉呢?改个return过了,OMG ------------------------------------------------------------- ...

  2. LeetCode Target Sum

    原题链接在这里:https://leetcode.com/problems/target-sum/description/ 题目: You are given a list of non-negati ...

  3. LeetCode Minimum Absolute Difference in BST

    原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...

  4. 洛谷 P1854 花店橱窗布置

    题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V是花瓶的数目.花束可以移动,并且每束花用1到F的整数标识 ...

  5. nodepad++的python环境变量设置

    转:http://blog.csdn.net/memray/article/details/42041975

  6. SQL Server 学习系列之四(SQL 内幕)

    SQL Server 学习系列之四(SQL 内幕) SQL Server 学习系列之一(薪酬方案+基础) SQL Server 学习系列之二(日期格式问题) SQL Server 学习系列之三(SQL ...

  7. [python] itertools库学习

    最近做 cyber-dojo上的题,好几道都要用到排列组合.一开始我还老老实实自己写算法.后来一想,不对呀!python有那么多的库,为啥不用呢? 于是搜了下,发现这个:itertools 使用 he ...

  8. 2015.9.28 不能将多个项传入“Microsoft.Build.Framework.ITaskItem”类型的参数 问题解决

    方法是:项目->属性->安全性->启用ClickOnce安全设置, 把这个复选框前面的勾去掉就可以了.

  9. SetConsoleCtrlHandler演示

    #include "stdafx.h"#include <Windows.h> static BOOL WINAPI Handler(DWORD cntrlEvent) ...

  10. paramiko分开执行多条命令 不像之前一样使用\n

    #!/usr/bin/env python#-*- encoding -*- import paramiko transport = paramiko.Transport(('192.168.11.1 ...