简体   繁体   English

我不明白QAbstractItemModel的工作原理

[英]I don't understand how QAbstractItemModel works

I've been using this class somewhat successfully thus far but I feel like I don't have a good conceptual understanding of what I'm doing. 到目前为止,我已经成功地使用了该类,但是我觉得我对自己的工作没有很好的概念性理解。 I'm missing something basic about the Qt model/view architecture which prevents me from making full use of it. 我缺少有关Qt模型/视图架构的基本知识,这使我无法充分利用它。 Qt doc doesn't seem to be addressing my questions. Qt doc似乎没有解决我的问题。 Much of my lack of understanding seems to be centered around the data() method. 我缺乏了解的很多地方似乎都围绕data()方法。

For example: 1) The doc doesn't properly explain (or I missed it) what the data() method is and how to properly use it. 例如:1)文档没有正确解释(或我错过了)什么是data()方法以及如何正确使用它。 I just have this vague idea that it is some kind of callback or slot function for when there are any changes to my model. 我只是有一个模糊的想法,当我的模型有任何更改时,它是某种回调或插槽函数。 But architecturally speaking what is its role? 但是从架构上来讲,它的作用是什么? Why is it needed and what problem is it solving? 为什么需要它,它要解决什么问题?

2) Speaking of roles, what is the point of the role parameter in the data method. 2)说到角色,数据方法中角色参数的意义是什么? Again, why did the designers choose to implement the data() method this way? 同样,为什么设计人员选择以这种方式实现data()方法?

I hope my questions aren't too abstract. 我希望我的问题不要太抽象。 If they are it might be because I don't fully understand what I don't understand about the model/view architecture. 如果是这样,可能是因为我不完全了解关于模型/视图架构的不了解。 Therefore not quite sure how to formulate my question. 因此不太确定如何提出我的问题。

Anyway, anyone who has some decent understanding of these concepts is welcome to chime in. Also if anybody knows other places that explains this better than the Qt doc that would be great as well. 无论如何,欢迎对这些概念有一定了解的任何人来参加。此外,如果有人比Qt Doc更好地解释了这一点,那么其他人也可以对此进行解释。

Thanks much for any feedback. 非常感谢您的任何反馈。

As the function name hints the `QAbstractItemModel::data()' is for providing the information that should be visualized by the view. 正如函数名称所暗示的,“ QAbstractItemModel :: data()”用于提供应由视图可视化的信息。 View doesn't need to cache all the data (in some cases thousands of elements) at once, but incrementally queries it from this function as soon as it should be shown on the screen. View不需要一次缓存所有数据(在某些情况下为数千个元素),但应在屏幕上显示后立即从此函数进行增量查询。

When I talk about the data, I assume that it could be item's text, text color, background color, icon etc. All these types of data represented by Qt::ItemDataRole enum and the data itself represented by QVariant . 当谈论数据时,我假设它可以是项目的文本,文本颜色,背景颜色,图标等。所有这些类型的数据都由Qt::ItemDataRole枚举表示,而数据本身由QVariant表示。

For example, if you have a tree view and corresponding model. 例如,如果您具有树视图和相应的模型。 When you scroll the tree down, it should paint, say 20 nodes. 向下滚动树时,它应该绘制,例如20个节点。 Tree view calls model's data() function for each of these 20 nodes to paint their texts, icons, backgrounds etc. 树视图为这20个节点中的每一个调用模型的data()函数以绘制其文本,图标,背景等。

And finally, for better understanding roles. 最后,为了更好地理解角色。 Instead of having textData() , colorData() , iconData() function in the model, Qt provides one single function data() with ability to conditionally chose which data to return depending on the role. Qt iconData()模型中具有textData()colorData()iconData()函数, iconData()提供了一个单一的函数data() ,可以根据角色有条件地选择要返回的数据。 This is much convenient design wise. 这是非常方便的设计明智的选择。

Did you read about Model/View Programming? 您是否了解过模型/视图编程?

http://doc.qt.io/qt-4.8/model-view-programming.html http://doc.qt.io/qt-4.8/model-view-programming.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM