1 Introduction to Model/View Programming
<u>The model/view architecture</u>
1.jpg一般的model/view classes可以分成三部分:models, views, and delegates ,每一部分都有抽象基类提供通用接口,这意味着你可以子类化他们来实现契约中的功能,也可以实现一些你所定制的功能。
Models, views, and delegates使用信号槽机制通信:
- Signals from the model inform the view about changes to the data held by the data source.
- Signals from the view provide information about the user's interaction with the items being displayed.
- Signals from the delegate are used during editing to tell the model and view about the state of the editor.
Models
QAbstractItemModel是所有model的基类,它提供了足够多的,灵活度高的接口来使view显示数据。
- QStringListModel is used to store a simple list of QString items.
- QStandardItemModel manages more complex tree structures of items, each of which can contain arbitrary data.
- QFileSystemModel provides information about files and directories in the local filing system.
- QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel are used to access databases using model/view conventions.
Views
QAbstractItemView是三种views的基类:
- QListView displays a list of items
- QTableView displays data from a model in a table
- QTreeView shows model items of data in a hierarchical list
Delegates
基类:QAbstractItemDelegate
默认使用:QStyledItemDelegate
Sorting
0...0
Convenience classes
- QListWidget, QTreeWidget, and QTableWidget 这些类方便使用,但不是为了子类化而设计的,所以他们没有views类灵活性高,不可以使用model,还有重要的是他们是 item-based
- 如果你想在model/view的基础上继续使用item-based模型,可以使用下面类: QListView, QTableView, and QTreeView with QStandardItemModel
2 Using models and views
下面介绍怎样使用model/view模式
<u>Two models included in Qt</u>
- QStandardItemModel
多用途model,可以提供list、table、tree的数据结构,这个类中可以存储数据。 - QFileSystemModel
提供文件目录的信息,不存储数据。
3 Model classes
<u>Basic concepts</u>
在model/view架构中,model提供标准接口以便于 views and delegates可以访问数据。在QT中,标准接口由QAbstractItemModel规定,无论数据项是怎么样保存的,所有QAbstractItemModel的子类都是以一个层次结构来表示数据。
1.jpgModel内部改变时,会通过信号槽机制来通知任何使用它的views
Model indexes
为了确保数据表示和数据的访问无关,引入了model index的概念。model中的每一个数据都可以由一个model index来表示, Views and delegates通过这些index获取显示数据项。
这样的结果就是只有model知道怎样获取数据,通过模型管理数据类型。索引包含一个指向创建它们的模型的指针,用多个模型时,可以防止混乱。
QAbstractItemModel *model = index.model();
模型索引提供了不同信息块的一个临时引用,可用于检索或修改数据。模型有时会重组其内部结构,索引会失效,所以不要保存模型索引。如果需要一个长期有效的数据引用,一定要创建一个永久模型索引。
临时索引: QModelIndex
永久索引:QPersistentModelIndex
如果要获取一个数据项的index,必须具备三要素:行数,列数,父索引,下面会详细介绍这三个属性。
Rows and columns
在一个最基本的形式中,一个model可以以一个简单表格的方式,通过行列值来访问。这并不是说底层数据存放在一个数组中。利用行和列的数目只是一个约定,以允许组件相互通信。我们可以通过指定的行数和列数的模型检索任何特定项目,并且得到代表数据项的index。
QModelIndex index = model->index(row, column, ...);
单一数据结构的模型例如表格、列表不需要其他信息。但就像上面代码表明,为了获取索引我们需要提供更多的信息。
1.jpg
Parents of items
由模型提供的类似表格项的数据接口对于表格或列表中的数据时理想的;行列数目系统是一种准确映射数据的方式。然而,像树形的数据结构需要模型更灵活的接口。所以每个数据项又可以做为其他表的父项。
当我们请求一个模型项的索引时,我们必须提供父项的信息。在模型外,唯一的查询项目的方法就是通过索引,所以一个父项的索引是必须的:
QModelIndex index = model->index(row, column, parent);
1.jpg
Item roles
模型项可以为了其他的组件扮演不同的角色,允许不同类型数据在不用的情况下被提供。例如: Qt::DisplayRole用于访问一个字符串,这个字符串可以以一个文本的形式显示在view中,通常,一个数据项包含多种不同的角色,标准的角色由Qt::ItemDataRole定义。
QVariant value = model->data(index, role);
1.jpg
对数据项最常用的用途都在 Qt::ItemDataRole中定义了。通过为每个角色提供相应的数据,模型可以提供hints,并代表有关项目应如何呈现给用户。不同的views,可以自由地解释或忽略此信息。另外,也可以为应用程序特定的用途role完成附加的作用。
Summary
- 模型index为views和delegates提供了数据项的位置,这样做可以使他们远离任何model内在的数据结构
- 数据项通过他们的行列数,以及父项的索引值被查询到
- 模型索引是应其他组件的要求由model创建的,例如views和delegates
- role区分与一个数据项相关的不同的数据类型
<u>Using model indexes</u>
为了展示我们怎样在模型中获取数据,使用模型索引,我们建立一个QFileSystemModel,不用views在窗口中显示。
1.jpg
2.jpg