QAbstractItemModel 如何表示树? [英] How does QAbstractItemModel Represent Tree?

查看:41
本文介绍了QAbstractItemModel 如何表示树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然很难理解 QAbstractItemModel 对项目的表示.有两种返回 QModelIndex 项的方法对我来说没有任何意义.

I'm still having a great deal of difficulty understanding QAbstractItemModel's representation of items. There are two methods that return QModelIndex items that just aren't making any sense to me.

QModelIndex QAbstractItemModel::index(int row, int column, const QModelIndex& index)

是第一个.传递给这个函数的视图是什么?我索引特定的树项以创建索引吗?如果是这样,这个函数的意义是什么?为什么不只返回索引?行和列代表什么?index 实际上是一个父节点,函数根据该父节点下的行数返回特定索引吗?列在这里只是一个空操作吗?

is the first. What is the view passing to this function? I index the specific tree item to have an index created for? If so, what's the point of the function? Why not just return index? What do row and column represent? Is index actually a parent node, with the function returning the specific index based on the number of rows beneath that parent? Is column just a no-op here?

当使用传递的 row 参数时,如果有的话,第 0 行是指索引/父节点本身,还是它下面的第一项?

When the passed row parameter is used, if ever, does row 0 refer to the index/parent node itself, or the first item below it?

第二,

QModelIndex QAbstractItemModel::parent(const QModelIndex& index) const

这个方法似乎会返回传递索引的直接父级.我正在使用一个数据结构,它本质上是树状的,但存储在一个平面数组中,数组元素包含有关树深度的信息,因此直接父级的父级总是深度小于它自己的深度 1.但是在这种情况下,什么会被提供给 createIndex 呢?内部 QModelIndex 行、列和 internalPointer 指的是什么?鉴于我使用的基于数组的结构,array[0] 的父级应该是什么?

It seems like this method would return the direct parent of the passed index. I'm working with a data structure which is inherently tree-like, but stored in a flat array, with array elements containing information on tree depth, so a direct parent would always have a parent with depth 1 less than it's own depth. But what gets fed to createIndex in this case? What do the internal QModelIndex row, column, and internalPointer refer to? Given the array-based structure I'm using, what should the parent of array[0] be?

我已经阅读了有关这些主题的 Qt 示例和文档,但似乎无法在理解这些类的工作原理方面取得任何进展.

I've read over the Qt examples and documentation on these topics already, and can't seem to make any progress understanding how these classes work.

推荐答案

QAbstractItemModel 被称为抽象"是有原因的.它不定义或强制执行任何存储模型项的特定方式,完全取决于您,开发人员对 QAbstractItemModel 进行子类化并在您的子类中实现所需的接口.QAbstractItemModel 强制执行的是视图用来与模型通信的接口.它还强制执行的是标准视图如何呈现数据的心理模型".

QAbstractItemModel is called "abstract" for a reason. It does not define or enforce any particular way of storing the model items, it's completely up to you, the developer subclassing QAbstractItemModel and implementing the required interfaces in your subclass. What QAbstractItemModel does enforce is the interfaces which are used by views to communicate with the model. What it also kinda enforces is a "mental model" of how the data can be presented by the standard views.

您可以将 QAbstractItemModel 视为出生在树和桌子家庭的孩子.想象一棵树:你有一堆物品,每个物品都可以有自己的子物品,它们也可以有自己的子物品等等.现在想象一个表:你有一堆项目,它们被排列成一个二维数组,可以按行和列进行索引.现在假设您有一个树,其中所有项目都可以有多个列,因此看起来像表格的行.所以,如果你,比如说,展开一些已知有子项的树项,你会看到一个表格 - 几个子项排列成几行 + 每个子项都有几列.我希望这个心智模型可以帮助理解 QAbstractItemModel 和看似奇怪的 QModelIndex 类的其他令人困惑的接口.

You can think of QAbstractItemModel as a child born in the family of a tree and a table. Imagine a tree: you have a bunch of items, each item can have its own child items, they can have their child items as well and so on. Now imagine a table: you have a bunch of items which are arranged into a 2D-array which can be indexed by row and column. Now imagine that you have a tree all items in which can have multiple columns and thus look like rows of the table. So if you, say, expand some tree item known to have child items, you'd see a table - several child items arranged into several rows + each of them having several columns. I hope this mental model can help to make sense of otherwise confusing interfaces of QAbstractItemModel and of seemingly strange QModelIndex class.

QModelIndex,作为官方文档 states,用于在模型中定位项目.在第一个近似值中,索引具有一行、一列和父项来唯一标识此类行树模型中的项目应该足够了.实际上 QModelIndex 允许你做更多的事情:你可以放一些 指针 或一些内部id如果这样可以让您自己的代码更轻松地识别 QAbstractItemModel 子类中使用的数据结构中的模型项.

QModelIndex, as the official documentation states, is used to locate items within the model. In the first approximation, it should be enough for the index to have a row, a column and parent to uniquely identify the item within such a tree-of-rows model. In reality QModelIndex allows you to do even more: you can put some pointer or some internal id into the index if that makes it easier for your own code to identify the model item within your own data structures used in your QAbstractItemModel subclass.

因此,您询问的方法大致如下:

Thus, the methods you asked about do roughly the following:

  1. QAbstractItemModel::index 接受行、列和 parent 假定模型项并返回与其对应的 QModelIndex.稍后视图可能会使用返回的 QModelIndex 对象来调用,例如 data 方法以获取要显示的一些实际数据.您可能会问为什么视图不只按行、列和父级查询数据?好吧,理论上它可以,但是通过 QModelIndex 这样做允许您,特定模型子类的开发人员,使用诸如内部指针或内部 id 之类的技巧.
  2. QAbstractItemModel::parent 应返回父项的QModelIndex 用于由传入的 QModelIndex 表示的项目.或者只是无效的 QModelIndex 如果该项目没有父项.您现在可能想知道,如果 QModelIndexrel="nofollow noreferrer">QAbstractItemModel::createIndex 需要行、列和指针或 id 而不是父 QModelIndex?答案很简单:QAbstractItemModel::createIndex 返回的QModelIndex 包含一个指向创建它的模型对象的链接;因此,当视图询问其父级的 QModelIndex 时,该调用将传播到创建该 QModelIndex 的模型,即 QAbstractItemModel::parent 被调用.现在是您的模型应该使用特殊索引或内部指针或内部 id 或一些疯狂的魔法来识别传入的 QModelIndex 所指向的项的父项.
  1. QAbstractItemModel::index takes row, column and parent of the supposed model item and returns QModelIndex corresponding to it. Later on the view might use that returned QModelIndex object to call, for example, the data method of your model in order to get some actual data to display. You might ask why won't the view just query the data by row, column and parent? Well, in theory it could but doing so via QModelIndex allows you, the developer of a particular model subclass, to employ such tricks as internal pointer or internal id.
  2. QAbstractItemModel::parent should return the parent item's QModelIndex for the item represented by the passed in QModelIndex. Or just invalid QModelIndex if that item doesn't have a parent. You might wonder now how on Earth can you create a QModelIndex which would know its parent if QAbstractItemModel::createIndex takes row, column and a pointer or id but not the parent QModelIndex? The answer is simple: QModelIndex returned by QAbstractItemModel::createIndex contains a link to the model object that has created it; so when the view asks QModelIndex of its parent, that call is propagated to the model that created that QModelIndex i.e. QAbstractItemModel::parent gets called. And it is your model now which should use either special indexing or internal pointer or internal id or maybe some crazy magic to identify the parent of the item pointed to by the passed in QModelIndex.

这篇关于QAbstractItemModel 如何表示树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆