alexltr的个人主页

http://www.qtcn.org/bbs/u/107032  [收藏] [复制]

alexltr

业余编程学习者

  • 24

    关注

  • 48

    粉丝

  • 145

    访客

  • 等级:骑士
  • 身份:论坛版主
  • 总积分:533
  • 男,1976-01-01
  • 广州

最后登录:2020-10-26

更多资料

日志

Model/View Programming 模型与视图编程 -- <3>

2011-11-26 21:07
Model classes
模型类


Before examining how selections are handled, you may find it useful to examine the concepts used in the model/view framework.
在检验如何处理选择项之前,我们先来了解一下模型/视图框架里用到的概念。

Basic concepts
基本概念

In the model/view architecture, the model provides a standard interface that views and delegates use to access data. In Qt, the standard interface is defined by the QAbstractItemModel class. No matter how the items of data are stored in any underlying data structure, all subclasses of QAbstractItemModelrepresent the data as a hierarchical structure containing tables of items. Views use this convention to access items of data in the model, but they are not restricted in the way that they present this information to the user.
在模型/视图架构中,模型提供一个视图和委托用于存取数据的标准接口。在Qt中,这个标准接口由QAbstractItemModel类定义。无论数据项以任何底层数据结构储存,所有QAbstractItemModel的子类都将以包含项表格的层次结构来呈现这些数据。视图使用这个约定来存取模型中数据的项,但是他们向用户显示信息的方法不会受到限制。

Models also notify any attached views about changes to data through the signals and slots mechanism.
This section describes some basic concepts that are central to the way item of data are accessed by other components via a model class. More advanced concepts are discussed in later sections.
同时模型通过信号和槽机制将数据的变动通知附属的视图。
这一章节讲解了一些关于其它部件通过使用模型类存取数据项的核心基本概念。更多的高级概念将在后面的章节讨论。

Model indexes
模型索引

To ensure that the representation of the data is kept separate from the way it is accessed, the concept of a model index is introduced. Each piece of information that can be obtained via a model is represented by a model index. Views and delegates use these indexes to request items of data to display.
As a result, only the model needs to know how to obtain data, and the type of data managed by the model can be defined fairly generally. Model indexes contain a pointer to the model that created them, and this prevents confusion when working with more than one model.
为了确保数据的显示和存取之间的分离, 引入了模型索引这个概念。每一条通过模型获取的信息都是用一个模型索引来表示的。视图和委托通过这些索引来请求数据项的显示。
这样,只有模型需要知道怎样去获取数据,同时模型处理的数据类型可以被定义得相当广泛。模型索引包含一个指向创建它们的模型的指针,这样可以避免使用多个模型时引起混淆。
  1. QAbstractItemModel *model = index.model();


Model indexes provide temporary references to pieces of information, and can be used to retrieve or modify data via the model. Since models may reorganize their internal structures from time to time, model indexes may become invalid, and should not be stored. If a long-term reference to a piece of information is required, a persistent model index must be created. This provides a reference to the information that the model keeps up-to-date. Temporary model indexes are provided by the QModelIndex class, and persistent model indexes are provided by the QPersistentModelIndex class.
To obtain a model index that corresponds to an item of data, three properties must be specified to the model: a row number, a column number, and the model index of a parent item. The following sections describe and explain these properties in detail.
模型索引为信息块提供临时参照,可以用来提取或修改模型里的数据。由于模型可能要经常重组内部的结构,使得模型索引变成无效的,从而不能储存。如果需要长时间得参照一个数据块,就必须创建一个固定的模型索引。这个索引为模型不断更新的数据提供一个参照。临时模型索引由QModelIndex类提供,固定模型索引由QPersistentModelIndex类提供。
要获得一个数据项对应的模型索引,必须为模型指定3个属性:行号,列号,父项的索引号。下面的章节将详细描述和解释这些属性。

Rows and columns
行和列

In its most basic form, a model can be accessed as a simple table in which items are located by their row and column numbers. This does not mean that the underlying pieces of data are stored in an array structure; the use of row and column numbers is only a convention to allow components to communicate with each other. We can retrieve information about any given item by specifying its row and column numbers to the model, and we receive an index that represents the item:
在模型的最基本的形式中,它可以以一个简单的表格进行存取,里面的项以行号和列号定位。这并不意味底层数据以数组结构储存。使用行号和列号只是让部件之间进行相互通讯的一个约定。通过指定模型中的行号和列号我们就可以提取任何项的信息,同时得到一个代表这个项的索引:
  1. QModelIndex index = model->index(row, column, ...);


Models that provide interfaces to simple, single level data structures like lists and tables do not need any other information to be provided but, as the above code indicates, we need to supply more information when obtaining a model index.
为简单且单一层次数据结构如列表和表格提供接口的模型不需要提供任何其他的信息,但是,就像上面的例子所标明的一样,当要获得一个模型索引时我们要提供更多的信息。

Rows and columns
行和列

The diagram shows a representation of a basic table model in which each item is located by a pair of row and column numbers. We obtain a model index that refers to an item of data by passing the relevant row and column numbers to the model.
这个图示显示了一个基本表格模型的表示法,里面的项以一对行号和列号来定位。通过向模型传递对应的行号和列号,我们就可以获得一个指向一个数据项的模型索引。
  1. QModelIndex indexA = model->index(0, 0, QModelIndex());
    QModelIndex indexB = model->index(1, 1, QModelIndex());
    QModelIndex indexC = model->index(2, 1, QModelIndex());



Top level items in a model are always referenced by specifying QModelIndex() as their parent item. This is discussed in the next section.
模型的顶层项总是通过指定QModelIndex()函数来作为他们的父项参照。这就是下一节要讨论的内容。

Parents of items
父项

The table-like interface to item data provided by models is ideal when using data in a table or list view; the row and column number system maps exactly to the way the views display items. However, structures such as tree views require the model to expose a more flexible interface to the items within. As a result, each item can also be the parent of another table of items, in much the same way that a top-level item in a tree view can contain another list of items.
When requesting an index for a model item, we must provide some information about the item's parent. Outside the model, the only way to refer to an item is through a model index, so a parent model index must also be given:
当在一个表格或列表视图中使用数据时,模型提供的表格型数据接口是较为理想的。行列号系统恰好影射视图显示项的方法。然而,诸如树型视图的结构要求模型对它其中的项要有更灵活的接口。这样,每一个项可以是另外一个表的父项,同样地,一个树型视图的顶级项也可以包含另一个项列表。
当为一个模型项请求一个索引时,我们必须提供一些关于这个项的父项的一些信息。在模型之外,引用一个项的唯一方法就是通过模型索引,所以一个父模型索引也必须要提供:
  1. QModelIndex index = model->index(row, column, parent);


Parents, rows, and columns
父项,行和列

The diagram shows a representation of a tree model in which each item is referred to by a parent, a row number, and a column number.
Items "A" and "C" are represented as top-level siblings in the model:
这个图显示了一个树模型的表示法,里面的项由父项,行和列号定位。项”A”和项”C”代表这个模型中的顶层成员
QModelIndex indexA = model->index(0, 0, QModelIndex()); QModelIndex indexC = model->index(2, 1, QModelIndex());
Item "A" has a number of children. A model index for item "B" is obtained with the following code:
项“A”有一个子成员,项“B”的模型索引可以用以下的代码获得:
  1. QModelIndex indexB = model->index(1, 0, indexA);



Item roles
项角色

Items in a model can perform various roles for other components, allowing different kinds of data to be supplied for different situations. For example,Qt::DisplayRole is used to access a string that can be displayed as text in a view. Typically, items contain data for a number of different roles, and the standard roles are defined by Qt::ItemDataRole.
We can ask the model for the item's data by passing it the model index corresponding to the item, and by specifying a role to obtain the type of data we want:
模型里的项可以为其他部件扮演不同的角色,允许为不同的情形提供各种不同的数据。例如,Qt::DisplayRole用于存取一个可以在视图中以文字显示的字符串。通常情况下,项包含各种不同的角色的数据,标准的角色由Qt::ItemDataRole定义。
通过传递对应项的模型索引给模型,并指定一个角色以取得我们想要的数据的类型,我们就可以从模型中取得项的数据:
  1. QVariant value = model->data(index, role);


Item roles
项角色

The role indicates to the model which type of data is being referred to. Views can display the roles in different ways, so it is important to supply appropriate information for each role.
角色为模型指明哪一种数据被引用。视图可以用不同的方式显示角色,所以为每一种角色提供合适的信息是很重要的

The Creating New Models section covers some specific uses of roles in more detail.
在“创建新模型”这一节中将详细介绍角色的一些具体的用法

Most common uses for item data are covered by the standard roles defined in Qt::ItemDataRole. By supplying appropriate item data for each role, models can provide hints to views and delegates about how items should be presented to the user. Different kinds of views have the freedom to interpret or ignore this information as required. It is also possible to define additional roles for application-specific purposes.
Qt::ItemDataRole里定义的标准角色涵盖了项数据最常用的用途。通过为每个角色提供合适的项数据,模型就可以为视图和委托提供关于怎样向用户显示项的提示。各种不同的视图都有根据需要中断或忽略这些信息的自由。同时也可以为特定的程序要求定义另外的角色。

Summary
概括

Model indexes give views and delegates information about the location of items provided by models in a way that is independent of any underlying data structures.
模型索引以一种独立于任何底层数据结构之外的方法,为视图和委托提供关于模型中项的定位信息

Items are referred to by their row and column numbers, and by the model index of their parent items.
项以他们的行号和列号,以及他们的父项的模型索引作为参照

Model indexes are constructed by models at the request of other components, such as views and delegates.
模型索引应其他部件如视图和委托的要求由模型来构造。

If a valid model index is specified for the parent item when an index is requested using index(), the index returned refers to an item beneath that parent item in the model. The index obtained refers to a child of that item.
当使用index()函数请求一个索引时,如果指定一个有效的模型索引作为父项索引,则返回的索引指向模型里这个父项下面的一个项。这个索引获得该项的子项的一个参照。

If an invalid model index is specified for the parent item when an index is requested using index(), the index returned refers to a top-level item in the model.
当使用index()函数请求一个索引时,如果指定一个无效的模型索引为父项索引,则返回的索引指向模型里的一个顶级项。

The role distinguishes between the different kinds of data associated with an item.
角色识别一个项中的各种不同类型的相关数据。

Using model indexes
使用模型索引

To demonstrate how data can be retrieved from a model, using model indexes, we set up a QFileSystemModel without a view and display the names of files and directories in a widget. Although this does not show a normal way of using a model, it demonstrates the conventions used by models when dealing with model indexes.We construct a file system model in the following way:
为了说明如何使用模型索引从模型中获得数据,我们建立一个没有视图的 QFileSystemModel,同时把文件名和目录名显示在一个部件中。虽然这个例子并没有显示使用模型的常用方法,但是说明了使用模型索引时模型所使用的约定。我们用下面的方法构造一个文件系统模型:
  1.      QFileSystemModel *model = new QFileSystemModel;
         QModelIndex parentIndex = model->index(QDir::currentPath());
         int numRows = model->rowCount(parentIndex);

In this case, we set up a default QFileSystemModel, obtain a parent index using a specific implementation of index() provided by that model, and we count the number of rows in the model using the rowCount() function.
For simplicity, we are only interested in the items in the first column of the model. We examine each row in turn, obtaining a model index for the first item in each row, and read the data stored for that item in the model.
在这个例子中,我们建立一个默认的QFileSystemModel,,使用这个模型提供的一个专门的实现index()函数获得一个父索引,同时使用rowCount()函数计算出这个模型的行数。
为简单起见,我们只关注模型第一列的数据。我们按顺序逐行检查,获得每行第一个项的模型索引,然后读取存储在模型项里的数据。
  1.      for (int row = 0; row < numRows; ++row) {
             QModelIndex index = model->index(row, 0, parentIndex);


To obtain a model index, we specify the row number, column number (zero for the first column), and the appropriate model index for the parent of all the items that we want. The text stored in each item is retrieved using the model's data() function. We specify the model index and the DisplayRole to obtain data for the item in the form of a string.为获得一个模型索引,我们指定行号,列号(第一列为0),以及我们想要的所有数据项的父项模型索引。储存于每个项中的文本可以用模型的data() 函数获得。我们指定一个模型索引以及DisplayRole角色来取得一个字符串形式的项数据。

  1.          QString text = model->data(index, Qt::DisplayRole).toString();
             // Display the text in a widget.     }

The above example demonstrates the basic principles used to retrieve data from a model:
以上例子说明了用于从模型中提取数据的一些基本原则:

The dimensions of a model can be found using rowCount() and columnCount(). These functions generally require a parent model index to be specified.
模型的大小可以用rowCount() 和 columnCount()得到。这两个函数通常要指定一个父模型索引。

Model indexes are used to access items in the model. The row, column, and parent model index are needed to specify the item.
模型索引用于存取模型里的项。指定项必须要有行号,列号以及父模型索引。

To access top-level items in a model, specify a null model index as the parent index with QModelIndex().
要存取模型的顶级项,就用QModelIndex()函数指定一个空的模型索引作为父模型索引。l   Items contain data for different roles. To obtain the data for a particular role, both the model index and the role must be supplied to the model.
项包含不同角色的数据。要获得一个特定角色的数据,必须要为模型提供模型索引和角色。

Further reading
进一步阅读

New models can be created by implementing the standard interface provided by QAbstractItemModel. In the Creating New Models section, we demonstrate this by creating a convenient ready-to-use model for holding lists of strings.
通过实现 QAbstractItemModel提供的标准接口可以创建新的模型。在“创建新模型”这一节中,我们通过创建一个存放字符列表的方便现成的模型来说明这一内容。
分类:默认分类|回复:0|浏览:1707|全站可见|转载
 

Powered by phpwind v8.7 Certificate Copyright Time now is:10-31 04:01
©2005-2016 QTCN开发网 版权所有 Gzip disabled