Quick控件--10.treeview 树控件

1 效果介绍

2 介绍

2.1 MVC介绍

  • Model-View-Controller (MVC) is a design pattern frequently used in the development of all types of software. Sometimes, it is termed as architectural pattern (Wikipedia), a wider connotation of the design pattern that takes into account various software design principles not addressed by a simple design pattern. MVC began as a framework in SmallTalk, which became so popular and widely used that the character exhibits semblance to many software engineering principles as it evolved.
  • The structure of MVC, as the name suggests, consists of three decoupled term, called model, view, and controller. And decouple it is—as you apply the idea, you segment your code into three sections based on the principle that one segment must not be dependent on another to function. That means the model section should be independent of view and controller and so on… But, there is a catch there; in practice, it is found that the main stress is in decoupling the model from the view. Such strict rules cannot be applied to the controller. It often dwindles between the model and the view, or sometimes it is inseparable from the view. It is not always possible to separate the controller from either two although our intention should be to make them as distinct as much as possible. But, one thing for sure is that the view and the model should be so decoupled that if any changes are made to the view that must not trigger a ripple effect on the model code.
    在这里插入图片描述

2.2 MVC in the Qt Framework

Thus, according to the classic definition, the model keeps the data and the view renders it to display units. The controller helps in mapping the two layers. When the client wants to change a certain record, the controller holds the key mechanism to initiate the change. So far, so good.

However, the Qt Framework has a slightly different perspective. Instead of having controller classes, the view handles data updating via delegates. It has two primary objectives: one, to help the view render each value; two, help the view when the user wants to make some changes. So, in a way, the controller has merged with the view and the view does some of the controller’s job through delegates. So, the idea becomes this.
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.3 重要成员函数

索引index

index()
sibling() 
parent()

可以通过模型访问的每一项数据都有一个关联的模型索引。可以使用index()函数获得该模型索引。每个索引可以具有一个sibling() 索引;子项具有parent()索引。

数据data

roleNames()
data()
itemData()

每个项目都有许多与之关联的数据元素,可以检索它们,通过在data()函数中指定一个角色(参见Qt :: ItemDataRole)来检索它们。使用itemData()函数可以同时获取所有可用角色的数据。
使用特定的Qt :: ItemDataRole可以设置当前Item的每个角色(role)的数据。可以使用setData()设置Item的每个role的数据,也可以使用setItemData()设置Item的所有角色的数据。

标志

flags()

可以使用flags() (参阅Qt::ItemFlag) 来查询项目,以查看是否可以通过其他方式选择,拖动或操纵它们。

子对象

hasChildren()

如果项目具有子对象,则hasChildren()返回true相应的索引。

模型改变信号

dataChanged()
headerDataChanged()
layoutChanged()

模型发出信号以指示变化。只要模型可用的数据项发生更改,就会发出dataChanged()。对模型提供的标头的更改将导致发出headerDataChanged()。如果基础数据的结构发生了变化,则模型可以发出layoutChanged()来向任何附加的视图指示它们应该重新显示所显示的任何项目,并考虑到新的结构。

层次结构

insertRows()
insertColumns()
removeRows()
removeColumns()

该模型在层次结构的每个级别都有一个rowCount()和columnCount()。可以使用insertRows(),insertColumns(),removeRows()和removeColumns()插入和删除行和列。
一个insertRow()实现必须调用beginInsertRows()之前插入新行到数据结构,endInsertRow()之后立即。
一个insertColumns()实现必须调用beginInsertColumns()之前将新列插入的数据结构,并endInsertColumns()之后立即。
一个removeRows()实现必须调用beginRemoveRows()前行,从数据结构中删除,并endRemoveRows()之后立即。
一个removeColumns()实现必须调用beginRemoveColumns()之前的列从数据结构中删除,并endRemoveColumns()之后立即。

2.4 数据访问机制

  • enum中定义role
  • QHash中关联role和name
  • data()中根据role返回对应的数值Value
  • QML中,直接通过name获取数据
    在这里插入图片描述
	// 构造父节点下子节点的索引
    virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    // 通过子节点索引获取父节点索引
    virtual QModelIndex parent(const QModelIndex &child) const override;
    // 获取父节点下子节点的行数
    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    // 获取父节点下子节点列数
    virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    // 获取节点数据:包括DisplayRole|TextAlignmentRole等
    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

3 控件代码

参考

1、Qt qml treeview 树控件
2、QML中树形控件TreeView的最基本、最简单粗暴的、一看秒懂的实现方法,之一
3、Qt读写JSON,以及使用QTreeView展示和编辑JSON数据
4、树结构(有id和pid字段)数组,生成多层嵌套的json对象
5、QT-EditableTreeModel中,TreeItem和TreeModel的理解
6、bootstrap-treeview多级树形菜单,后台JSON格式如何组织?
7、示例:QML_MVC_Demo
8、使用C++实现QML的TreeView Model (一)
9、使用C++实现QML的TreeView Model (二)
10、QML自定义一个TreeView,使用ListView递归
11、QML自定义树控件(TreeView 的style加上节点可拖动)
12、QAbstractItemModel使用样例与解析
13、如何使用QAbstractItemModel定制自己想要的数据模型(使用TreeView模型讲解)
14、QT qml TreeView展示数据结构于界面
15、Mark–Qt树形控件QTreeView使用1——节点的添加删除操作
16、Mark–QT开发(三十八)——Model/View框架编程
17、Implementing an MVC Model with the Qt C++ Framework
18、QTreeVirew 与 QTreeWidget 简单使用
19、【QT】模型与视图,QStandardItemModel and QTreeView
20、Qt Model/View/Delegate浅谈 - roleNames()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

worthsen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值