在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(自定义一个RowNode,我没有碰到过)的更多相关文章

  1. Qt中如何写一个model

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

  2. C++在使用Qt中SLOT宏须要注意的一个小细节

    大家都知道C++虚函数的机制,对于基类定义为虚函数的地方,子类假设覆写,在基类指针或者引用来指向子类的时候会实现动态绑定. 但假设指针去调用非虚函数,这个时候会调用C++的静态绑定,去推断当前的指针是 ...

  3. C++在使用Qt中SLOT宏需要注意的一个小细节

    大家都知道C++虚函数的机制,对于基类定义为虚函数的地方,子类如果覆写,在基类指针或者引用来指向子类的时候会实现动态绑定. 但如果指针去调用非虚函数,这个时候会调用C++的静态绑定,去判断当前的指针是 ...

  4. 6.关于QT中的内存管理,动态的制作,动态库的调用,静态库的制作

     一  QT的内存管理 1  QT中的内存管理是QObject来管理的 2  QT中的内存管理没有cocos2dx中的引用计数 3  组件能够指定父对象 QTimer *timer = QTime ...

  5. QT中QWidget、QDialog QMainWindow

    继承关系:在Qt中所有的类都有一个共同的基类QObject ,QWidget直接继承与QPaintDevice类,QDialog.QMainWindow.QFrame直接继承QWidget 类. QW ...

  6. Qt中的内存回收机制

    Qt中的内存回收机制 在Qt中创建对象的时候会提供一个 Parent对象指针(可以查看类的构造函数),下面来解释这个parent到底是干什么的. QObject是以对象树的形式组织起来的.当你创建一个 ...

  7. CI中的控制器中要用model中的方法,是统一写在构造器方法中,还是在每一个方法中分别写

    Q: CI中的控制器中要用model中的方法,是统一写在构造器方法中,还是在每一个方法中分别写 A: 建议统一写,CI框架会自动识别已经加载过的类,所以不用担心重复加载的问题 class C_User ...

  8. Qt中,当QDockWidget的父窗口是一个不可以拖动的QTabWidget的时候实现拖动的方法

    之前在做有关QDockWidget的内容时候遇到了瓶颈,那就是窗口弹出来之后拖动不了,也不可以放大和缩小,若是弹出来之后设置成了window的flags,也不可以拖动,而且也不是需要的效果. 1.弹出 ...

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

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

随机推荐

  1. ResourceManager高可用配置

    ResourceManager高可用配置 1. yarn-site.xml配置 <property> <name>yarn.resourcemanager.cluster-id ...

  2. Yii框架学习笔记(二)将html前端模板整合到框架中

    选择Yii 2.0版本框架的7个理由 http://blog.chedushi.com/archives/8988 刚接触Yii谈一下对Yii框架的看法和感受 http://bbs.csdn.net/ ...

  3. 利用微软类库 Visual Studio International Pack 汉字转拼音

    首先,从微软官网下载安装包:http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=44CAC7F0-633B-477D-AED2 ...

  4. 读取tomcat下的文件夹路径

    背景:测试的为了每次部署时清缓存,将temp文件夹也删了,导致系统中有些excel导出功能用不了. 解决:新建一个监听文件,在系统启动时,判断temp文件夹是否存在,不存在就新建. temp文件夹的作 ...

  5. C++异常处理(Exception Handling)

    在C++中引入了三种操作符来处理程序的出错情况,分别是:try  , throw  ,  catch 1.基本的用法如下: try{ //code to be tried throw exceptio ...

  6. 查找并绘制轮廓[OpenCV 笔记XX]

    好久没有更新了,原谅自己放了个假最近又在赶进度,所以...更新的内容是很靠后的第八章,因为最近工作要用就先跳了,后面会更新笔记编号...加油加油! 在二值图像中寻找轮廓 void cv::findCo ...

  7. [leetcode] 406. Queue Reconstruction by Height

    https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...

  8. Poj 1001 / OpenJudge 2951 Exponentiation

    1.链接地址: http://poj.org/problem?id=1001 http://bailian.openjudge.cn/practice/2951 2.题目: Exponentiatio ...

  9. Linux – RedHat7 / CentOS 7 忘记root密码修改

    1.(a) 开机出现grub boot loader 开机选项菜单时,立即点击键盘任意鍵,boot loader 会暂停. (b) 按下’e’,编辑选项菜单(c) 移动上下鍵至linux16 核心命令 ...

  10. HTML5的离线储存

    在用户没有与因特网连接时,可以正常访问站点或应用,在用户与因特网连接时,更新用户机器上的缓存文件.        原理:HTML5的离线存储是基于一个新建的.appcache文件的缓存机制(不是存储技 ...