Qt中如何写一个model(自定义一个RowNode,我没有碰到过)
在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(自定义一个RowNode,我没有碰到过)的更多相关文章
- Qt中如何写一个model
在qt中,用到最多就是model/view的结构来表示数据层及表示层的关系.model用于给view提供数据.那如何来实现一个简单的树形model呢. 实现一个自己的model需要重载以下的方法: Q ...
- C++在使用Qt中SLOT宏须要注意的一个小细节
大家都知道C++虚函数的机制,对于基类定义为虚函数的地方,子类假设覆写,在基类指针或者引用来指向子类的时候会实现动态绑定. 但假设指针去调用非虚函数,这个时候会调用C++的静态绑定,去推断当前的指针是 ...
- C++在使用Qt中SLOT宏需要注意的一个小细节
大家都知道C++虚函数的机制,对于基类定义为虚函数的地方,子类如果覆写,在基类指针或者引用来指向子类的时候会实现动态绑定. 但如果指针去调用非虚函数,这个时候会调用C++的静态绑定,去判断当前的指针是 ...
- 6.关于QT中的内存管理,动态的制作,动态库的调用,静态库的制作
一 QT的内存管理 1 QT中的内存管理是QObject来管理的 2 QT中的内存管理没有cocos2dx中的引用计数 3 组件能够指定父对象 QTimer *timer = QTime ...
- QT中QWidget、QDialog QMainWindow
继承关系:在Qt中所有的类都有一个共同的基类QObject ,QWidget直接继承与QPaintDevice类,QDialog.QMainWindow.QFrame直接继承QWidget 类. QW ...
- Qt中的内存回收机制
Qt中的内存回收机制 在Qt中创建对象的时候会提供一个 Parent对象指针(可以查看类的构造函数),下面来解释这个parent到底是干什么的. QObject是以对象树的形式组织起来的.当你创建一个 ...
- CI中的控制器中要用model中的方法,是统一写在构造器方法中,还是在每一个方法中分别写
Q: CI中的控制器中要用model中的方法,是统一写在构造器方法中,还是在每一个方法中分别写 A: 建议统一写,CI框架会自动识别已经加载过的类,所以不用担心重复加载的问题 class C_User ...
- Qt中,当QDockWidget的父窗口是一个不可以拖动的QTabWidget的时候实现拖动的方法
之前在做有关QDockWidget的内容时候遇到了瓶颈,那就是窗口弹出来之后拖动不了,也不可以放大和缩小,若是弹出来之后设置成了window的flags,也不可以拖动,而且也不是需要的效果. 1.弹出 ...
- mvc中动态给一个Model类的属性设置验证
原文:mvc中动态给一个Model类的属性设置验证 在mvc中有自带的验证机制,比如如果某个字段的类型是数字或者日期,那么用户在输入汉字或者英文字符时,那么编译器会自动验证并提示用户格式不正确,不过这 ...
随机推荐
- OC5_构造方法与self指针
// // Dog.h // OC5_构造方法与self指针 // // Created by zhangxueming on 15/6/9. // Copyright (c) 2015年 zhang ...
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
- 3.MySQL之创建/删除用户
登录mysql服务器后可使用grant命令来创建用户并赋予相关权限. mysql> use mysql; Reading table information for completion of ...
- 实习笔记-3:ef实体操作错误篇
学习笔记 1.json序列化ef实体是报错:“序列化类型为“System.Data.Entity.DynamicProxies.XXXX.... 对象时检测到循环引用.” 公司里用ef来生成实体.但是 ...
- 建立IP6隧道
某站点又开始全站Free了,是否还在为在家上不了IPv6站点而苦恼呢?本教程适用于路由后的windows设备,即ip地址为内网地址通过本教程设置,可实现windows设备获得ipv6地址,以访问IPv ...
- 【HeadFirst设计模式】9.迭代器与组合模式
迭代器: 定义: 提供一种方法,顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示.(不让你知道我内部是如何聚合的) 把游走的任务放在迭代器上,而不是聚合上.这样简化了聚合的接口和实现,也让责任 ...
- CentOS6.4 安装JDK
1.下载JDK,这里用的是jdk-7u65-linux-x64.tar.gz,请到官网上下载. 2.清除默认的JDK,yum remove java 3.解压文件 tar -xzf jdk-7u65- ...
- Linux CentOS下shell显示-bash-4.1$ 不显示用户名和主机名的解决方法
CentOS下新增加一个用户,登录进去会发现shell脚本信息没有显示用户名和主机名,反而显示的是-bash-4.1$,如图所示: 而不是我们经常看到的username@hostname$的组合,看起 ...
- LogBack入数据库重写
项目需要:将info以及error的日志信息写入到数据库中:同时所有的日志都要写入到日志文件中. 可以封装一下,在基类的logError/logInfo中调用了log.error()以及log.inf ...
- C++引用计数
简介 引用计数就是对一个对象记录其被引用的次数,其的引用计数可加可减,那什么时候加什么时候减呢?所以引用计数的重点在于在哪里加,在哪里减: 加: 减: 实现 // // Ref.hpp // Ref ...