Qt中如何写一个model
在qt中,用到最多就是model/view的结构来表示数据层及表示层的关系。model用于给view提供数据。那如何来实现一个简单的树形model呢。
实现一个自己的model需要重载以下的方法:
- QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
- bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
- int rowCount(const QModelIndex &parent = QModelIndex()) const;
- int columnCount(const QModelIndex &parent = QModelIndex()) const<pre name="code" class="cpp">QVariant headerData(int section, Qt::Orientation orientation,
- int role = Qt::DisplayRole) const;
- bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
- int role = Qt::EditRole);
- QModelIndex index(int row, int column,
- const QModelIndex &parent = QModelIndex()) const;
- QModelIndex parent(const QModelIndex &child) const;
- virtual Qt::ItemFlags flags(const QModelIndex &index) const;
下面通过一个小例子来说明每个方法的作用。想实现下面一个树形结构。
既然实现一个model里面肯定得构造自己的数据结构。
- class RowNode
- {
- public:
- RowNode() { m_pNode = NULL; }
- ~RowNode() {}
- public:
- inline void setParent(RowNode *rowNode) { m_pNode = rowNode; }
- inline RowNode *parent() { return m_pNode; }
- private:
- RowNode *m_pNode;
- };
model构造里
- m_child1 = new RowNode();
- m_child2 = new RowNode();
- m_child2->setParent(m_child1);
1)data方法:这个方法用于提供model所需要的各种数据。根据不同的index跟role返回不同的值。
- QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
- {
- <pre name="code" class="cpp">if (Qt::DisplayRole == role)
- {
- RowNode *pRowNode = static_cast<RowNode *>(index.internalPointer());
- if (m_child1 == pRowNode)
- {
- if (0 == index.column())
- {
- return "Test";
- }
- else if (1 == index.column())
- {
- return "Icon";
- }
- else if (2 == index.column())
- {
- return "comboBox";
- }
- }
- else
- {
- if (0 == index.column())
- {
- return "col0";
- }
- else if (1 == index.column())
- {
- return "col1";
- }
- else
- {
- return "col2";
- }
- }
- }
- else
- return QVariant();
- }
2)setData:这个方法用于设置model的各种数据,根据不同的index跟role的值.原理跟data一样,这里就不直接写了,直接返回True.
- bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
- {
- return true;
- }
3)rowCount:用于设置行数。需要注意的是返回是指parent下面有几个子,而不是指整个TableView有多少行。因为显示树形的话,返回的RowCount只是指那个父结点有几个子。
- int rowCount(const QModelIndex &parent = QModelIndex()) const;
- {
- <pre name="code" class="cpp">if (! parent.isValid())
- {
- return 1;
- }
- else
- {
- RowNode *pNode = static_cast<RowNode *>(parent.internalPointer());
- if (pNode == m_child1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
4)columnCount:用于设置列数,这个是整个TableView的列数。
- int columnCount(const QModelIndex &parent = QModelIndex()) const;
- {
- return 4;
- }
5)headerData及setHeadData
- QVariant headerData(int section, Qt::Orientation orientation,
- int role = Qt::DisplayRole) const;
- {
- <pre name="code" class="cpp"> if (orientation == Qt::Vertical)
- {
- if (Qt::DisplayRole == role)
- {
- return "2";
- }
- }
- if (Qt::DisplayRole == role)
- {
- return "1";
- }
- else
- return QVariant();
}
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole);
{
return true;
}
同data跟setData一样,但是是用于设置header的值,就是标题行及固定列的值。
6)index:用于设置返回的index的值。
- QModelIndex index(int row, int column,
- const QModelIndex &parent = QModelIndex()) const;
- {
- <pre name="code" class="cpp"> if (! parent.isValid())
- {
- return createIndex(row, column, m_child1);
- }
- else
- {
- return createIndex(row, column, m_child2);
- }
}
7)parent:用于设置父index。
- QModelIndex parent(const QModelIndex &child) const;
- {
- RowNode *pRowNode = static_cast<RowNode *>(child.internalPointer());
- if (pRowNode == m_child2)
- {
- return createIndex(0, 0, m_child1);
- }
- return QModelIndex();
- }
8)flags:用于返回model一些flags,如是否可以编辑的话,会加上Qt::itemIsEditable.
- virtual Qt::ItemFlags flags(const QModelIndex &index) const;
- {
- <pre name="code" class="cpp"> Qt::ItemFlags oFlags = QAbstractItemModel::flags(index);
- return oFlags | Qt::ItemIsEditable;
- }
这样一个简单的model就实现了。
http://blog.csdn.net/hpjx1987/article/details/28005011
Qt中如何写一个model的更多相关文章
- Qt中如何写一个model(自定义一个RowNode,我没有碰到过)
在qt中,用到最多就是model/view的结构来表示数据层及表示层的关系.model用于给view提供数据.那如何来实现一个简单的树形model呢. 实现一个自己的model需要重载以下的方法: Q ...
- mvc中动态给一个Model类的属性设置验证
原文:mvc中动态给一个Model类的属性设置验证 在mvc中有自带的验证机制,比如如果某个字段的类型是数字或者日期,那么用户在输入汉字或者英文字符时,那么编译器会自动验证并提示用户格式不正确,不过这 ...
- C语言中如何写一个简单可移植而又足够随机的随机数生成器
在C语言中标准库中的随机数产生函数的返回可能不是最优的,因为有些随机数生成器的低位并不随机,而另一些返回随机数的函数实现上又太复杂鸟.所以rand()%N并不是一个好方法,牛人给出的建议是使用: ra ...
- QT 中如何实现一个简单的动画
QT可以实现一下简单的动画,比如 运动的时钟 闪烁的按钮. 动画的实现: (1)创建一个定时器 (2)调用QWidget::update()通知界面重绘 实现一个按钮闪烁的例子: circlewidg ...
- (转载)QT中PRO文件写法的详细介绍,很有用,很重要!
版权声明:本文为博主原创文章,未经博主允许不得转载. 在QT中,有一个工具qmake可以生成一个makefile文件,它是由.pro文件生成而来的,.pro文件的写法如下: 1. 注释从“#”开始,到 ...
- QT中PRO文件写法的详细介绍
学习Qt时,发现有些知识看了不经常用就忘了,以下是书本上写的一些关于qmake的相关知识,自己看后,打算把一些经常用到的记下来,整理整理. Qt程序一般使用Qt提供的qmake工具来编译. qmake ...
- 【SSH网上商城项目实战24】Struts2中如何处理多个Model请求
转自: https://blog.csdn.net/eson_15/article/details/51465067 1. 问题的提出 Struts2中如果实现了ModelDriven<m ...
- Qt中的多线程与线程池浅析+实例
1. Qt中的多线程与线程池 今天学习了Qt中的多线程和线程池,特写这篇博客来记录一下 2. 多线程 2.1 线程类 QThread Qt 中提供了一个线程类,通过这个类就可以创建子线程了,Qt 中一 ...
- 从零开始写一个武侠冒险游戏-8-用GPU提升性能(3)
从零开始写一个武侠冒险游戏-8-用GPU提升性能(3) ----解决因绘制雷达图导致的帧速下降问题 作者:FreeBlues 修订记录 2016.06.23 初稿完成. 2016.08.07 增加对 ...
随机推荐
- 修改Win7远程桌面端口
Win7与XP不同,在开启远程桌面修改端口后是无法直接访问的,原因是还未修改远程桌面在防火墙入站规则中的端口号. 修改远程桌面端口: [HKEY_LOCAL_MACHINE/SYSTEM/Curren ...
- ckeditor增加上传图片的功能
1.配置config.js开启图片上传选项卡. CKEDITOR.editorConfig = function( config ) { config.filebrowserImageUploadUr ...
- IIFE-js中(function(){…})()立即执行函数写法理解
介绍IIFE IIFE的性能 使用IIFE的好处 IIFE最佳实践 jQuery优化 在Bootstrap源码(具体请看<Bootstrap源码解析>)和其他jQuery插件经常看到如下的 ...
- ios特性访问器方法(setter和getter)
Employee.h @interface Employee:NSObject { int _employeeNumber; NSString *_name; Employee*_supervisit ...
- 开发流程习惯的养成—TFS简单使用
才开始用,所以是个很基础的介绍,欢迎大家一起交流学习 一.追本溯源 讲到开发流程,还要从敏捷开始,因为敏捷才有了开发流程的重视,整个流程也是按照敏捷的思想进行的,这里不再叙述敏捷的定义 敏捷的流程(个 ...
- 流水线(pipe-line)简介
1.什么是流水线设计技术? 答:所谓流水线设计实际上是把规模较大.层次较多的组合逻辑电路分为几个级,在每一级插入寄存器组并暂存数据. K级就是有K个寄存器组,从上到下没有反馈电路. 2.流水线设计的深 ...
- SVN 安装配置
1,软件下载 到官方网站的下载二进制安装文件,来到二进制包下载部分,找到 Windows NT, 2000, XP and 2003部分,然后选择Apache 2.2 或者 Apache 2.4,这样 ...
- MVC3 ModelBinder
1.Model Binder从哪些地方获取数据(找到数据后会停止继续寻找) MVC 框架内置默认的 Model Binder 是 DefaultModelBinder 类.当 Action Invok ...
- Java多线程——<四>让线程有返回值
一.概述 到目前为止,我们已经能够声明并使一个线程任务运行起来了.但是遇到一个问题:现在定义的任务都没有任何返回值,那么加入我们希望一个任务运行结束后告诉我一个结果,该结果表名任务执行成功或失败,此时 ...
- 【转载】CCombobox使用大全
一.如何添加/删除Combo Box内容 1. 在Combo Box控件属性的Data标签里面添加,一行表示Combo Box下拉列表中的一行.换行用ctrl+回车. 2. 在程序初始化时动态添加 如 ...