QAbstractItemModel


细节描述

QAbstractItemModel类定义了M/V模式中能与其他组件(components)交互(interoperate)的数据模型(item model)所必须使用的标准接口(interfaces). 它不能够被直接实例化, 相反, 你应该继承(subclass)它, 创建一个新的模型.
QAbstractItemModel是一个M/V类, 也是M/V框架下的一部分. 它可以被用作数据试图的底层数据模型.
如果你需要一个用在QListView或QTableView等其他数据视图中的模型, 你应该考虑继承QAbstractListModel或QAbstractTableModel, 而不是这个类.
底层数据模型作为一个表的层次结构暴露给视图和委托(The underlying data model is exposed to views and delegates as a hierarchy of tables). 如果你不使用层次结构, 那么这个模型就是一个简单的由行列组成的简单表格. 每个数据有一个独一无二的下标QModelIndex.

每个可以通过模型获取的数据元素(item data)都有一个关联的模型下标. index()函数可以获取这个模型下标. 每个下标可能有一个sibling()下标; 子元素有一个parent()下标.
每一个元素有一系列与之相关联的数据元素, 并且可以通过在模型的data函数中指定一个角色(role)来获取这些数据元素. itemData()函数可以获取同一时间下所有可用角色的数据.
用Qt::ItemDataRole可以指定每个角色的数据. Data for individual roles are set individually with setData(), or they can be set for all roles with setItemData().
通过flags()函数可以查看元素能否被选取, 拖拽或进行其他操作.
If an item has child objects, hasChildren() returns true for the corresponding index.
The model has a rowCount() and a columnCount() for each level of the hierarchy. Rows and columns can be inserted and removed with insertRows(), insertColumns(), removeRows(), and removeColumns().
模型通过发送(emit)信号(singal)来表明改变.
The items available through the model can be searched for particular data using the match() function.

继承(Subclassing)

继承QAbstractItemModel后, 你至少要实现index(), parent(), rowCount(), columnCount(), 和data()函数. 所有的只读(read-only)模型都会使用这些函数, 并且是可编辑模型(editable)的基础函数.
你也可以重新实现hasChildren()函数,来为那些rowCount()函数实现成本很高的模型提供特定的行为. 这使得模型可以限制视图请求的数据量, 并且可以用作实现模型数据的惰性填充的方式(and can be used as a way to implement lazy population of model data).
如果要生成可编辑模型, 你必须实现setData()函数, 并且重新实现flags()函数, 并确保返回中包含ItemIsEditable. You can also reimplement headerData() and setHeaderData() to control the way the headers for your model are presented.
The dataChanged() and headerDataChanged() signals must be emitted explicitly when reimplementing the setData() and setHeaderData() functions, respectively.
自定义模型必须创建其他组件可用的模型下标. 调用createIndex()函数, 并传入适当的row, column, 以及一个标识符(identifier), 标识符可以是指针或整型值. 每个元素的这些值的组合必须是独一无二的. 自定义模型通常在其他重新实现的函数中使用这些唯一标识符来检索项目数据并访问有关该项目的父项和子项的信息.
没有必要支持Qt::ItemDataRole中定义的所有角色. 根据模型中所包含的数据类型的不同, 可能实现data()函数并返回一个更通用类型的有效信息会更有用. 大多数模型至少为Qt::DisplayRole提供项目数据的文本表示, 更好的模型也应为Qt::ToolTipRole和Qt::WhatsThisRole提供有效信息. 这些角色使得模型可以在标准Qt视图中使用. 对于那些高度特质化的数据, 仅为用户定义的角色提供数据才是合理的.
模型的数据如果想要可以调整size, 就要实现insertRows(), removeRows(), insertColumns(), 和removeColumns(). 实现这些函数时, 最重要的是, 在事件发生前后, 通知所有连接的视图, 数据维度改变了.

  • An insertRows() implementation must call beginInsertRows() before inserting new rows into the data structure, and endInsertRows() immediately afterwards.
  • An insertColumns() implementation must call beginInsertColumns() before inserting new columns into the data structure, and endInsertColumns() immediately afterwards.
  • A removeRows() implementation must call beginRemoveRows() before the rows are removed from the data structure, and endRemoveRows() immediately afterwards.
  • A removeColumns() implementation must call beginRemoveColumns() before the columns are removed from the data structure, and endRemoveColumns() immediately afterwards.

这些函数发出的专用信号使连接的组件有机会在任何数据变得不可用之前采取措. 使用这些开始和结束功能对插入和删除操作进行封装也使模型能够正确管理持久性模型索引. 如果要正确处理选择, 则必须确保调用这些函数. 如果您插入或删除带有子项的项目, 则无需为子项调用这些函数. 换句话说, 父项管理其子项.
To create models that populate incrementally, you can reimplement fetchMore() and canFetchMore(). If the reimplementation of fetchMore() adds rows to the model, beginInsertRows() and endInsertRows() must be called.

重要函数

未完待续...


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM