基于QItemDelegate的例子1 SpinBoxDelegate
SpinBoxDelegate例子是Qt Assistant中提供的一个非常优秀的例子,虽然讲的是继承于QItemDelegate的例子。但对于我们理解Delegate-委托这个概念,非常有帮助。
它重载了必须的几个函数:
(1) QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const;
(2) void setEditorData(QWidget *editor, const QModelIndex &index) const;
(3) void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const;
(4) void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const;
下面把源码附上,并加上部分注释。附件有源码可以下载。
Main.cpp
#include <QApplication>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QStandardItemModel>
#include <QTableView>#include "delegate.h"int main(int argc, char *argv[])
{QApplication app(argc, argv);//构建一个4行,2列的项模型QStandardItemModel model(4, 2);//声明一个TableViewQTableView tableView;//绑定模型tableView.setModel(&model);//声明一个委托SpinBoxDelegate delegate;//设定视图的委托tableView.setItemDelegate(&delegate);//ensuring that the view does not waste any of the space assigned to it for its header//最后一列全部填充ViewtableView.horizontalHeader()->setStretchLastSection(true);//初始化Modelfor (int row = 0; row < 4; ++row) {for (int column = 0; column < 2; ++column) {QModelIndex index = model.index(row, column, QModelIndex());model.setData(index, QVariant((row+1) * (column+1)));}}tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));tableView.show();return app.exec();
}
delegate.h
#ifndef DELEGATE_H
#define DELEGATE_H#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QSpinBox>class SpinBoxDelegate : public QItemDelegate
{Q_OBJECTpublic:SpinBoxDelegate(QObject *parent = 0);//返回一个编辑控件,用来编辑指定项的数据QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const;//将Model中数据赋值到控件上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;
};#endif
delegate.cpp
#include <QtGui>#include "delegate.h"SpinBoxDelegate::SpinBoxDelegate(QObject *parent): QItemDelegate(parent)
{
}//返回一个编辑控件,用来编辑指定项的数据
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/* option */,const QModelIndex &/* index */) const
{//返回该QSpinBox控件QSpinBox *editor = new QSpinBox(parent);editor->setMinimum(0);editor->setMaximum(100);return editor;
}
//将Model中数据赋值到控件上
void SpinBoxDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{//返回该索引的模型,继而返回该模型中此索引的编辑角色数据int value = index.model()->data(index, Qt::EditRole).toInt();//给控件赋值QSpinBox *spinBox = static_cast<QSpinBox*>(editor);spinBox->setValue(value);
}
//设定模型数据,根据指定项中对应编辑控件的数据
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const
{QSpinBox *spinBox = static_cast<QSpinBox*>(editor);spinBox->interpretText();int value = spinBox->value();//设置模型的数据model->setData(index, value, Qt::EditRole);
}
//更新编辑框几何形状
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{//根据option,设置编辑框位置editor->setGeometry(option.rect);
}
基于QItemDelegate的例子1 SpinBoxDelegate相关推荐
- 基于QItemDelegate的例子2 trackeEditorDelegate
trackeEditorDelegate例子是< C++ GUI Programming with Qt 4>中自定义委托的标准例子. 和上一个SpinBox例子相比更完整:它多了自定义E ...
- 最简单的基于libVLC的例子:最简单的基于libVLC的视频播放器
最简单的基于libVLC的例子文章列表: 最简单的基于libVLC的例子:最简单的基于libVLC的视频播放器 最简单的基于libVLC的例子:最简单的基于libVLC的视频播放器(图形界面版) 最简 ...
- 基于QStyledItemDelegate的例子 Star Delegate Example
这节需要事件.绘图等基础知识,比较复杂...先收藏,后续学习. Star Delegate Example http://doc.qt.nokia.com/4.7-snapshot/itemviews ...
- GJM: Unity3D基于Socket通讯例子 [转载]
首先创建一个C# 控制台应用程序, 直接服务器端代码丢进去,然后再到Unity 里面建立一个工程,把客户端代码挂到相机上,运行服务端,再运行客户端. 高手勿喷!~! 完全源码已经奉上,大家开始研究吧! ...
- Qt自定义委托在QTableView中绘制控件、图片、文字
自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数, 1.实现在QTableView中绘制 格式字符串 2.实现在QTableView中绘制进度条 3.实现在QTab ...
- 基于DDD的.NET开发框架 - ABP领域服务
返回ABP系列 ABP是"ASP.NET Boilerplate Project (ASP.NET样板项目)"的简称. ASP.NET Boilerplate是一个用最佳实践和流行 ...
- 深入理解面向对象 -- 基于 JavaScript 实现
我们在学习编程时,避免不了会接触一个概念,叫:面向对象编程(Object-oriented programming,缩写:oop) (不是搞对象那个对象哈),其实我们的编程方式,不止有面向对象,还有 ...
- Mule 官方例子研究
Mule 官方例子研究 一.编译导入Mule自带的例子 1.准备 安装Mule.这里就不介绍mule的安装了,请参考<Mule安装部署手册>. 2. 编译Mule自带例子中的Hello例子 ...
- 推荐系统--基于图的推荐算法
基于图的模型(graph−basedmodel )是推荐系统中的重要内容.在研究基于图的模型之前,首先需要将用户行为数据表示成图的形式.这里我们将用户行为数据用二分图表示,例如用户数据是由一系列的二元 ...
最新文章
- EBS 抓trace 文件
- 一个平行四边形可以分成四个_【八年级下】数学 平行四边形(3)菱形
- postman调用webservice接口_【分享】关于接口对前后端和测试的意义
- MySQL ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
- FLASH开发[00]
- 《统计学》学习笔记之参数估计
- CSS 中的定位:relative,absolute
- PHP操作图片简单案例
- Java 中的array数组总结之一
- android编译的错误日志,android编译遇到错误
- MySql学习笔记(六):扫描范围
- 360手机助手关于签名校验的分析
- 9款超级好用的在线PDF工具!
- 无线网络通信技术完全介绍
- 优秀程序员的八个好习惯
- 蓦然回首,灯火阑珊——自考总结
- word使用学习总结
- Google正式收购SketchUp
- 文案类网页没有投诉按钮,屏蔽微信投诉按钮
- 《硬核父母的五项修炼》读后感
热门文章
- Django-urls路由系统
- P4838 P哥破解密码
- canvas合成海报所遇问题及解决方案总结
- 豪情-2014年年终总结
- 关于程序员前途的看法和我系列文章的想法
- javascript获取Select下拉框的值
- 发现一个很nice的API调试工具
- pip/pip3 install 报错 “Could not find a version that satisfies the requriement xxx” 的解决方法
- Spring @RequestMapping注解示例
- hadoop中的9000端口代表什么_启动hadoop后,fs.defaultFS中配置的9000端口没有出现在监听中?...