9.Delegate类
#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类的更多相关文章
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- iOS多播Delegate类——GCDMulticastDelegate用法小结
iOS中通常的delegate模式只能有一个被委托的对象,这样当需要有多个被委托的对象时,实现起来就略为麻烦,在开源库XMPPFramework中提供了一个GCDMulticastDelegate类, ...
- 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
并发编程概述 前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...
- [C#] C# 知识回顾 - 委托 delegate
C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...
- [转载]C#委托和事件(Delegate、Event、EventHandler、EventArgs)
原文链接:http://blog.csdn.net/zwj7612356/article/details/8272520 14.1.委托 当要把方法作为实参传送给其他方法的形参时,形参需要使用委托.委 ...
- 【C#】委托-Delegate
C# 委托(Delegate) C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针.委托(Delegate) 是存有对某个方法的引用的一种引用类型变量.引用可在运行时被改变. 委托 ...
- CLR via C#(12)-委托Delegate
本来按照进度应该学习事件了,可总觉得应该委托在前,事件在后,才好理解. 委托是一个类,它提供了回调函数机制,而且是类型安全的.使用委托可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数 ...
- 转iOS中delegate、protocol的关系
iOS中delegate.protocol的关系 分类: iOS Development2014-02-12 10:47 277人阅读 评论(0) 收藏 举报 delegateiosprocotolc ...
- c# 关键字delegate、event(委托与事件)[MSDN原文摘录][1]
A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. ...
随机推荐
- 不能解决,复选框在request对象获取的信息后显示在用户信息里面为中文的选项名
因为方框里面value 不用中文?.? 假如用中文呢? 完全可以!!已经试验 如果不用中文,那么中文可以用对象的参数来获得,即在login.jsp中就要用javabean类属性
- nyoj-130-相同的雪花(hash)
题目链接 /* Name:NYOJ-130-相同的雪花 Copyright: Author: Date: 2018/4/14 15:13:39 Description: 将雪花各个分支上的值加起来,h ...
- pthread_getspecific()--读线程私有数据|pthread_setspecific()--写线程私有数据
原型: #include <pthread.h> void *pthread_getspecific(pthread_key_t key); int pthread_setspecific ...
- RTP协议全解(H264码流和PS流)
写在前面:RTP的解析,网上找了很多资料,但是都不全,所以我力图整理出一个比较全面的解析, 其中借鉴了很多文章,我都列在了文章最后,在此表示感谢. 互联网的发展离不开大家的无私奉献,我决定从我做起,希 ...
- Busybox shell脚本修改密码
/****************************************************************************** * Busybox shell脚本修改密 ...
- Effective Python之编写高质量Python代码的59个有效方法
这个周末断断续续的阅读完了<Effective Python之编写高质量Python代码 ...
- LeetCode Maximum Average Subarray I
原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/ 题目: Given an array con ...
- linux find -regex 使用正则表达式
find之强大毋庸置疑,此处只是带领大家一窥find门径,更详细的说明见man find和 info find.整篇文章循序渐进,从最常用的文件名测试项开始步步深入,到第六节基本讲完find处理文件 ...
- Pager分页
分页组件: /// <summary> /// 分页组件 /// </summary> public class PagerHelper { /// <summary&g ...
- 怎么让eclipse调试的时候不进入 class文件中去
Eclipse -> Window ->Preferences ->Java ->Debug "Suspend execution on uncaught excep ...