alexltr的个人主页

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

alexltr

我不是IT,只是喜欢Qt。 我不是程序员,只是与程序有缘。

  • 26

    关注

  • 60

    粉丝

  • 150

    访客

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

最后登录:2024-04-20

更多资料

日志

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

2011-11-19 19:16
Introduction to Model/View Programming
模型与视图简单介绍


Qt 4 introduced a new set of item view classes that use a model/view architecture to manage the relationship between data and the way it is presented to the user. The separation of functionality introduced by this architecture gives developers greater flexibility to customize the presentation of items, and provides a standard model interface to allow a wide range of data sources to be used with existing item views. In this document, we give a brief introduction to the model/view paradigm, outline the concepts involved, and describe the architecture of the item view system. Each of the components in the architecture is explained, and examples are given that show how to use the classes provided.
Qt4引入了一套新的项视图类,这些类使用模型/视图架构来处理数据及其显示方法之间的关系。模型/视图架构所引入的功能上的分离为开发者提供了更大的灵活性来自定义视图项的呈现方式,同时提供一个标准的模型接口,它允许广泛的数据源可以使用现有的视图项。在这里,我们将对模型/视图范例做一个简单的介绍,对相关的概念做一个概述,并且对项视图系统的架构进行描述。这个架构所涉及的每一个组件都将一一作解释,同时也将用例子来说明如何使用这些提供的类。

The model/view architecture
模型/视图架构

Model-View-Controller (MVC) is a design pattern originating from Smalltalk that is often used when building user interfaces. In Design Patterns, Gamma et al. write:
MVC consists of three kinds of objects. The Model is the application object, the View is its screen presentation, and the Controller defines the way the user interface reacts to user input. Before MVC, user interface designs tended to lump these objects together. MVC decouples them to increase flexibility and reuse.
模型-视图-控制器(MVC)是一个源自SmallTalk的设计模式,当建立用户接口时会经常用到它(SmallTalk)。在设计模型(Design Patterns)中 Gamma et al. 是这样描述的:
MVC由3种对象组成:模型是应用程序对象,视图是它的屏幕显示方式,控制器定义用户接口对用户输入反应的方式。在MVC之前,用户界面设计倾向于将这3种对象统统合并在一起处理。MVC将他们分离以增加灵活性和可重用性。

If the view and the controller objects are combined, the result is the model/view architecture. This still separates the way that data is stored from the way that it is presented to the user, but provides a simpler framework based on the same principles. This separation makes it possible to display the same data in several different views, and to implement new types of views, without changing the underlying data structures. To allow flexible handling of user input, we introduce the concept of the delegate. The advantage of having a delegate in this framework is that it allows the way items of data are rendered and edited to be customized.
如果将视图和控制器对象合并,那结果就是模型/视图架构。这样,数据的存储及其向用户显示的方法仍然是分离的,但在相同原理的基础上提供了一个更为简单的框架。这种分离使得在几个不同的视图上显示相同的数据成为可能,同时在不改变底层数据结构的情况下实现新的视图种类。为了灵活处理用户输入,我们引入了委托的概念。在这个框架中,使用委托的优势在于它可以自定义视图项数据传递及编辑的方法。

The model/view architecture
模型/视图架构

The model communicates with a source of data, providing an interface for the other components in the architecture. The nature of the communication depends on the type of data source, and the way the model is implemented.
模型负责与数据源进行通讯,为这个架构中的其它组件提供接口。通讯的实质依数据源的类型及实现模型的方法而定。

The view obtains model indexes from the model; these are references to items of data. By supplying model indexes to the model, the view can retrieve items of data from the data source.
视图从模型中获取视图索引;他们是数据项的引用;通过为模型提供模型索引,视图可以从数据源提取数据项。

In standard views, a delegate renders the items of data. When an item is edited, the delegate communicates with the model directly using model indexes.
在标准视图中,委托传递数据项。当项被编辑时,委托通过模型索引直接与模型进行通讯。

Generally, the model/view classes can be separated into the three groups described above: models, views, and delegates. Each of these components is defined by abstract classes that provide common interfaces and, in some cases, default implementations of features. Abstract classes are meant to be subclassed in order to provide the full set of functionality expected by other components; this also allows specialized components to be written.
通常情况下,模型/视图 类可以如以上所描述的一样分为3组:模型,视图和委托。其中的每一个组件都是由抽象基类定义的, 这些抽象基类提供通用的接口,在某些情况下,还提供一些特性的默认实现。抽象基类的目的是用来子类化,以便为其他组件提供一整套的功能,同时也可以用来写专门的组件。

Models, views, and delegates communicate with each other using signals and slots:
l   Signals from the model inform the view about changes to the data held by the data source.
l   Signals from the view provide information about the user's interaction with the items being displayed.
l   Signals from the delegate are used during editing to tell the model and view about the state of the editor.

模型,视图,委托通过使用信号和槽进行相互间的通讯。
l   模型的信号通知视图关于数据源所发生的数据改变。
l   视图信号提供关于用户与它所显示的项之间的互动信息
l   委托的信号用于在编辑时通知模型和视图有关编辑器的状态。

Models
模型

All item models are based on the QAbstractItemModel class. This class defines an interface that is used by views and delegates to access data. The data itself does not have to be stored in the model; it can be held in a data structure or repository provided by a separate class, a file, a database, or some other application component.
The basic concepts surrounding models are presented in the section on Model Classes.
所有的项模型都是基于QAbstractItemModel类的。这个类定义了一个视图和委托用于存取数据的接口。数据本身并不需要储存在模型中;它可以放在一个数据结构或单独的类提供的容器,文件,数据库,或一些其他的程序组件中。
围绕模型的一些基本概念放在模型类Model Classes.这一章节中。

QAbstractItemModel provides an interface to data that is flexible enough to handle views that represent data in the form of tables, lists, and trees. However, when implementing new models for list and table-like data structures, theQAbstractListModel and QAbstractTableModel classes are better starting points because they provide appropriate default implementations of common functions. Each of these classes can be subclassed to provide models that support specialized kinds of lists and tables.
The process of subclassing models is discussed in the section on Creating New Models.
QAbstractItemModel 为数据提供了足够灵活处理用来显示表格/列表/树形结构数据的视图的接口。然而,当为列表或表格型的数据结构实现新的模型时,QAbstractListModel类和 QAbstractTableModel类是较好的起点,因为他们提供了通用函数的默认实现。这两个类都可以子类化来实现支持专门的列表和表格类型的模型。
子类化模型的步骤放在“创建新模型Creating New Models”这一章节中进行讨论。

Qt provides some ready-made models that can be used to handle items of data:
l   QStringListModel is used to store a simple list of QString items.
l   QStandardItemModel manages more complex tree structures of items, each of which can contain arbitrary data.
l   QFileSystemModel provides information about files and directories in the local filing system.
l   QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel are used to access databases using model/view conventions.
Qt提供了几种可以用来处理数据项的预定义模型:
l   QStringListModel 用于存储一个 QString项的简单链表.
l   QStandardItemModel 处理较复杂的树结构项,其中的每个项可以包含任意的数据
l   QFileSystemModel 提供有关当前文件系统的文件及目录信息
l   QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel 使用模型/视图协定存取数据库。

If these standard models do not meet your requirements, you can subclass QAbstractItemModel, QAbstractListModel, or QAbstractTableModel to create your own custom models.
如果以上这些模型不能满足你的要求,你可以子类化QAbstractItemModel,QAbstractListModel, 或者 QAbstractTableModel去创建你自己定义的模型。

Views
视图

Complete implementations are provided for different kinds of views: QListView displays a list of items, QTableViewdisplays data from a model in a table, and QTreeView shows model items of data in a hierarchical list. Each of these classes is based on the QAbstractItemView abstract base class. Although these classes are ready-to-use implementations, they can also be subclassed to provide customized views.
The available views are examined in the section on View Classes.
各种不同类型的视图都提供了完整的实现:QListView显示项的一个列表,QTableView在一个表格中显示模型的数据,而QTreeView在一个层次结构的列表中显示模型的数据项。所有这些类都是基于QAbstractItemView这个抽象基类的。虽然这些类都是现成的实现,但是他们也可以子类化以提供自定义的视图。
在“视图类View Classes”这一章节中将研究这些可以的视图。

Delegate
委托

QAbstractItemDelegate is the abstract base class for delegates in the model/view framework. Since Qt 4.4, the default delegate implementation is provided by QStyledItemDelegate, and this is used as the default delegate by Qt's standard views. However, QStyledItemDelegate and QItemDelegate are independent alternatives to painting and providing editors for items in views. The difference between them is that QStyledItemDelegate uses the current style to paint its items. We therefore recommend using QStyledItemDelegate as the base class when implementing custom delegates or when working with Qt style sheets.
Delegates are described in the section on Delegate Classes.
QAbstractItemDelegate是模型/视图架构中委托的抽象基类,自Qt4.4开始,默认的委托实现由QStyledItemDelegate提供,它被作为Qt标准视图的默认委托来使用,然而 QStyledItemDelegate 和 QItemDelegate是为视图项提供绘制和编辑器的两个独立的委托。他们之间的不同在于,QStyledItemDelegate使用当前的样式来绘制它的项。因此在实现自定义委托或使用Qt样式表时,我们建议使用QStyledItemDelegate作为基类。
委托将在“委托类Delegate Classes”这一章节做介绍

Sorting
排序

There are two ways of approaching sorting in the model/view architecture; which approach to choose depends on your underlying model.
If your model is sortable, i.e, if it reimplements the QAbstractItemModel::sort() function, both QTableView andQTreeView provide an API that allows you to sort your model data programmatically. In addition, you can enable interactive sorting (i.e. allowing the users to sort the data by clicking the view's headers), by connecting theQHeaderView::sortIndicatorChanged() signal to the QTableView::sortByColumn() slot or theQTreeView::sortByColumn() slot, respectively.
在模型/视图架构中有两种方法可以实现排序,选择那种方法要看你的底层模型。比如,如果你的模型是可以排序的,如果它重新实现QAbstractItemModel::sort()函数,QTableView和QTreeView都提供一个API使你可以通过编程的方式对你的模型数据进行排序。另外,你还可以通过把QHeaderView::sortIndicatorChanged()信号与QTableView::sortByColumn()槽或QTreeView::sortByColumn()槽的对应连接进行互动式的排序(比如,允许用户单击表头来对数据进行排序)

The alternative approach, if your model do not have the required interface or if you want to use a list view to present your data, is to use a proxy model to transform the structure of your model before presenting the data in the view. This is covered in detail in the section on Proxy Models.
如果你的模型没有所要求的接口,或你想用列表视图Listview来显示你的数据,那另一个方法是就在视图显示你的数据之前使用代理模型(PROXY MODEL)来转换你的模型的结构。

Convenience classes
简便视图类

A number of convenience classes are derived from the standard view classes for the benefit of applications that rely on Qt's item-based item view and table classes. They are not intended to be subclassed, but simply exist to provide a familiar interface to the equivalent classes in Qt 3. Examples of such classes include QListWidget, QTreeWidget, andQTableWidget; these provide similar behavior to the QListBox, QListView, and QTable classes in Qt 3.
为了使应用程序可以使用qt基于项的(item-based)视图和表格类,从标准的视图类中衍生出了一些项视图的简便类。他们的目的不是用于子类化的,他们的存在只是为了给Qt3中对等的类提供一个类似的接口。这些类包括QListWidget, QTreeWidget, 和QTableWidget,他们分别跟Qt3中的QListBox, QListView, and QTable提供相似的行为。

These classes are less flexible than the view classes, and cannot be used with arbitrary models. We recommend that you use a model/view approach to handling data in item views unless you strongly need an item-based set of classes.
If you wish to take advantage of the features provided by the model/view approach while still using an item-based interface, consider using view classes, such as QListView, QTableView, and QTreeView with QStandardItemModel.
这些类没有视图类那么灵活,也不能用于各种模型。除非你强烈需要一套基于项的简便类,否则我们还是建议你在处理项视图数据时使用模型/视图的方法。
如果你希望利用模型/视图方法所提供的特性,同时又想使用一个基于项的接口,那就考虑把QStandardItemModel类与视图类如QListView, QTableView, 和 QTreeView等搭配使用。

分类:默认分类|回复:0|浏览:2774|全站可见|转载
 

Powered by phpwind v8.7 Certificate Copyright Time now is:05-09 14:30
©2005-2016 QTCN开发网 版权所有 Gzip disabled