在qt中,用到最多就是model/view的结构来表示数据层及表示层的关系。model用于给view提供数据。那如何来实现一个简单的树形model呢。

实现一个自己的model需要重载以下的方法:

  1. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  2. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
  3. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  4. int columnCount(const QModelIndex &parent = QModelIndex()) const<pre name="code" class="cpp">QVariant headerData(int section, Qt::Orientation orientation,
  5. int role = Qt::DisplayRole) const;
  6. bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
  7. int role = Qt::EditRole);
  8. QModelIndex index(int row, int column,
  9. const QModelIndex &parent = QModelIndex()) const;
  10. QModelIndex parent(const QModelIndex &child) const;
  11. virtual Qt::ItemFlags flags(const QModelIndex &index) const;

下面通过一个小例子来说明每个方法的作用。想实现下面一个树形结构。

既然实现一个model里面肯定得构造自己的数据结构

  1. class RowNode
  2. {
  3. public:
  4. RowNode() { m_pNode = NULL; }
  5. ~RowNode() {}
  6. public:
  7. inline void setParent(RowNode *rowNode) { m_pNode = rowNode; }
  8. inline RowNode *parent() { return m_pNode; }
  9. private:
  10. RowNode *m_pNode;
  11. };

model构造里

  1. m_child1 = new RowNode();
  2. m_child2 = new RowNode();
  3. m_child2->setParent(m_child1);

1)data方法:这个方法用于提供model所需要的各种数据。根据不同的index跟role返回不同的值。

  1. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  2. {
  3. <pre name="code" class="cpp">if (Qt::DisplayRole == role)
  4. {
  5. RowNode *pRowNode = static_cast<RowNode *>(index.internalPointer());
  6. if (m_child1 == pRowNode)
  7. {
  8. if (0 == index.column())
  9. {
  10. return "Test";
  11. }
  12. else if (1 == index.column())
  13. {
  14. return "Icon";
  15. }
  16. else if (2 == index.column())
  17. {
  18. return "comboBox";
  19. }
  20. }
  21. else
  22. {
  23. if (0 == index.column())
  24. {
  25. return "col0";
  26. }
  27. else if (1 == index.column())
  28. {
  29. return "col1";
  30. }
  31. else
  32. {
  33. return "col2";
  34. }
  35. }
  36. }
  37. else
  38. return QVariant();
  39. }

2)setData:这个方法用于设置model的各种数据,根据不同的index跟role的值.原理跟data一样,这里就不直接写了,直接返回True.

  1. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
  2. {
  3. return true;
  4. }

3)rowCount:用于设置行数。需要注意的是返回是指parent下面有几个子,而不是指整个TableView有多少行。因为显示树形的话,返回的RowCount只是指那个父结点有几个子。

  1. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  2. {
  3. <pre name="code" class="cpp">if (! parent.isValid())
  4. {
  5. return 1;
  6. }
  7. else
  8. {
  9. RowNode *pNode = static_cast<RowNode *>(parent.internalPointer());
  10. if (pNode == m_child1)
  11. {
  12. return 1;
  13. }
  14. else
  15. {
  16. return 0;
  17. }
  18. }

4)columnCount:用于设置列数,这个是整个TableView的列数。

  1. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  2. {
  3. return 4;
  4. }

5)headerData及setHeadData

  1. QVariant headerData(int section, Qt::Orientation orientation,
  2. int role = Qt::DisplayRole) const;
  3. {
  4. <pre name="code" class="cpp">    if (orientation == Qt::Vertical)
  5. {
  6. if (Qt::DisplayRole == role)
  7. {
  8. return "2";
  9. }
  10. }
  11. if (Qt::DisplayRole == role)
  12. {
  13. return "1";
  14. }
  15. else
  16. return QVariant();

}
 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole);
{
return true;
}


同data跟setData一样,但是是用于设置header的值,就是标题行及固定列的值。

6)index:用于设置返回的index的值。

  1. QModelIndex index(int row, int column,
  2. const QModelIndex &parent = QModelIndex()) const;
  3. {
  4. <pre name="code" class="cpp">    if (! parent.isValid())
  5. {
  6. return createIndex(row, column, m_child1);
  7. }
  8. else
  9. {
  10. return createIndex(row, column, m_child2);
  11. }

}


7)parent:用于设置父index。

  1. QModelIndex parent(const QModelIndex &child) const;
  2. {
  1. RowNode *pRowNode = static_cast<RowNode *>(child.internalPointer());
  2. if (pRowNode == m_child2)
  3. {
  4. return createIndex(0, 0, m_child1);
  5. }
  6. return QModelIndex();
  7. }

8)flags:用于返回model一些flags,如是否可以编辑的话,会加上Qt::itemIsEditable.

  1. virtual Qt::ItemFlags flags(const QModelIndex &index) const;
  2. {
  3. <pre name="code" class="cpp">    Qt::ItemFlags oFlags = QAbstractItemModel::flags(index);
  4. return oFlags | Qt::ItemIsEditable;
  5. }

这样一个简单的model就实现了。

http://blog.csdn.net/hpjx1987/article/details/28005011

Qt中如何写一个model的更多相关文章

  1. Qt中如何写一个model(自定义一个RowNode,我没有碰到过)

    在qt中,用到最多就是model/view的结构来表示数据层及表示层的关系.model用于给view提供数据.那如何来实现一个简单的树形model呢. 实现一个自己的model需要重载以下的方法: Q ...

  2. mvc中动态给一个Model类的属性设置验证

    原文:mvc中动态给一个Model类的属性设置验证 在mvc中有自带的验证机制,比如如果某个字段的类型是数字或者日期,那么用户在输入汉字或者英文字符时,那么编译器会自动验证并提示用户格式不正确,不过这 ...

  3. C语言中如何写一个简单可移植而又足够随机的随机数生成器

    在C语言中标准库中的随机数产生函数的返回可能不是最优的,因为有些随机数生成器的低位并不随机,而另一些返回随机数的函数实现上又太复杂鸟.所以rand()%N并不是一个好方法,牛人给出的建议是使用: ra ...

  4. QT 中如何实现一个简单的动画

    QT可以实现一下简单的动画,比如 运动的时钟 闪烁的按钮. 动画的实现: (1)创建一个定时器 (2)调用QWidget::update()通知界面重绘 实现一个按钮闪烁的例子: circlewidg ...

  5. (转载)QT中PRO文件写法的详细介绍,很有用,很重要!

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在QT中,有一个工具qmake可以生成一个makefile文件,它是由.pro文件生成而来的,.pro文件的写法如下: 1. 注释从“#”开始,到 ...

  6. QT中PRO文件写法的详细介绍

    学习Qt时,发现有些知识看了不经常用就忘了,以下是书本上写的一些关于qmake的相关知识,自己看后,打算把一些经常用到的记下来,整理整理. Qt程序一般使用Qt提供的qmake工具来编译. qmake ...

  7. 【SSH网上商城项目实战24】Struts2中如何处理多个Model请求

       转自: https://blog.csdn.net/eson_15/article/details/51465067 1. 问题的提出 Struts2中如果实现了ModelDriven<m ...

  8. Qt中的多线程与线程池浅析+实例

    1. Qt中的多线程与线程池 今天学习了Qt中的多线程和线程池,特写这篇博客来记录一下 2. 多线程 2.1 线程类 QThread Qt 中提供了一个线程类,通过这个类就可以创建子线程了,Qt 中一 ...

  9. 从零开始写一个武侠冒险游戏-8-用GPU提升性能(3)

    从零开始写一个武侠冒险游戏-8-用GPU提升性能(3) ----解决因绘制雷达图导致的帧速下降问题 作者:FreeBlues 修订记录 2016.06.23 初稿完成. 2016.08.07 增加对 ...

随机推荐

  1. ORACLE AWR 和 ASH

    一.关于ASH 我们都知道,用户在 ORACLE 数据库中执行操作时,必然要创建相应的连接和会话, 其中,所有当前的会话信息都保存在动态性能视图 V$SESSION 中,通过该视图,DBA 可 以查看 ...

  2. 【转】你需要知道的Python用法

    在使用Python多年以后,我偶然发现了一些我们过去不知道的功能和特性.一些可以说是非常有用,但却没有充分利用.考虑到这一点,我编辑了一些的你应该了解的Pyghon功能特色. 带任意数量参数的函数 你 ...

  3. Ubuntu下配置samba服务器实现文件共享

    安装Samba 安装samba sudo apt-get install samba Kubuntu 安装系统设置的共享模块 sudo apt-get install kdenetwork-files ...

  4. CPU 时间片 分时 轮转调度

    时间片即CPU分配给各个程序的时间,每个线程被分配一个时间段,称作它的时间片,即该进程允许运行的时间,使各个程序从表面上看是同时进行的.如果在时间片结束时进程还在运行,则CPU将被剥夺并分配给另一个进 ...

  5. MAC 升级到10.10(OS X Yosemite)下apache+php的配置问题

    MAC升级到最新系统后 本地测试的站点不能正常运行,其原因是mac系统升级后 他的apache的版本也随之升级了版本 Server version: Apache/2.4.9 (Unix) Serve ...

  6. 《自学C语言》第一章:C语言简介

    这一章主要介绍什么是C语言,以及它的产生.发展历史.如其中所说的计算机术语有不懂的请百度,例如什么是强类型,什么是高级语言. 1.C语言是什么? 答:C语言是一门通用计算机编程语言,应用广泛.C语言的 ...

  7. 配置 RAILS FOR JRUBY1.7.4

    1. 去JRUBY官方网站下载JRUBY http://www.jruby.org/ 目前最新版本是1.7.4,解压到C:\目录下 2. 设置环境变量 JRUBY_HOME = C:\jruby-1. ...

  8. mysql存储过程 OUT or INOUT argument 3 for routine

    mysql存储过程出现: OUT or INOUT argument 3 for routine gotask.UserLogin is not a variable or NEW pseudo-va ...

  9. Winform控件学习-TreeView - ContextMenuStrip

    首先,要向窗体添加一个TreeView控件: 然后再添加一个ContextMenuStrip控件: 接下就要给TreeView添加一个MouseDown事件,代码如下: Example 1 priva ...

  10. SET FOREIGN_KEY_CHECKS=0;在Mysql中取消外键约束。

    SET FOREIGN_KEY_CHECKS=0;在Mysql中取消外键约束.