alexltr的个人主页

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

alexltr

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

  • 26

    关注

  • 60

    粉丝

  • 150

    访客

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

最后登录:2024-04-20

更多资料

日志

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

2012-05-06 20:16


Proxy models
代理模型


In the model/view framework, items of data supplied by a single model can be shared by any number of views, and each of these can possibly represent the same information in completely different ways. Custom views and delegates are effective ways to provide radically different representations of the same data. However, applications often need to provide conventional views onto processed versions of the same data, such as differently-sorted views onto a list of items.
在模型/视图框架中,单个模型提供的数据项可以共享于任意多个视图中,并且每个视图都可能以完全不同的方式显示相同的信息。自定义视图和委托是为相同数据提供根本不同的呈现方式的有效方法。然而,应用程序经常要提供常规的视图用到相同数据的不同处理版本上,如一个列表项的不同排序视图。


Although it seems appropriate to perform sorting and filtering operations as internal functions of views, this approach does not allow multiple views to share the results of such potentially costly operations. The alternative approach, involving sorting within the model itself, leads to the similar problem where each view has to display items of data that are organized according to the most recent processing operation.
虽然它看上去好像适合作为视图的内部参数来执行排序和筛选操作,但是这种方法不允许多个视图共用这种潜在的代价高昂的操作所得的结果。另外一种方法,涉及在模型本身内部的排序,同样会导致相似的问题:每一个视图都必须显示根据最新的处理操作所组织的数据项。


To solve this problem, the model/view framework uses proxy models to manage the information supplied between individual models and views. Proxy models are components that behave like ordinary models from the perspective of a view, and access data from source models on behalf of that view. The signals and slots used by the model/view framework ensure that each view is updated appropriately no matter how many proxy models are placed between itself and the source model.
为了解决这个问题,模型/视图框架使用代理模型来管理单独模型和视图之间的信息。从视图角度看,代理模型是跟普通模型表现一样的组件,可以存取代表该视图的源模型的数据。不管有多少个代理模型放在视图和源模型之间,模型/视图框架使用的信号和槽会确保每一个视图都能及时的更新。


Using proxy models
使用代理模型


Proxy models can be inserted between an existing model and any number of views. Qt is supplied with a standard proxy model, QSortFilterProxyModel, that is usually instantiated and used directly, but can also be subclassed to provide custom filtering and sorting behavior. The QSortFilterProxyModel class can be used in the following way:
代理模型可以安插在一个现有的模型和任意数量的视图之间。Qt提供了一个标准的代理模型,QSortFilterProxyModel,,它通常被实例化就可直接使用,但它也可以子类化以提供自定义的筛选和排序行为。QSortFilterProxyModel可以以下面的方式使用:


  1.      QSortFilterProxyModel *filterModel = new QSortFilterProxyModel(parent);
         filterModel->setSourceModel(stringListModel);


         QListView *filteredView = new QListView;
         filteredView->setModel(filterModel);





Since proxy models are inherit from QAbstractItemModel, they can be connected to any kind of view, and can be shared between views. They can also be used to process the information obtained from other proxy models in a pipeline arrangement.
The QSortFilterProxyModel class is designed to be instantiated and used directly in applications. More specialized proxy models can be created by subclassing this classes and implementing the required comparison operations.
因为代理模型是从QAbstractItemModel继承而来的,所以它可以连接到各种视图,并共用于视图间。他们也可以通过一个传递途径的安排,用来处理从其它代理模型得到的信息。
QSortFilterProxyModel类被设计为可实例化并直接在程序中使用。子类化这个类并实现所要求的比较操作,可以创建更多专门的代理模型。


Customizing proxy models
自定义代理模型


Generally, the type of processing used in a proxy model involves mapping each item of data from its original location in the source model to either a different location in the proxy model. In some models, some items may have no corresponding location in the proxy model; these models are filtering proxy models. Views access items using model indexes provided by the proxy model, and these contain no information about the source model or the locations of the original items in that model.
通常情况下,代理模型所使用的处理形式涉及将每个项的数据从他的源模型原始位置映射到代理模型中的任意一个不同的位置。在有些模型中,一些项可能在代理模型中没有对应的位置;这些模型就是筛选代理模型。视图使用代理模型提供的模型索引存取项,以及那些在这个模型中没有包含源模型信息或源项位置的项。


QSortFilterProxyModel enables data from a source model to be filtered before being supplied to views, and also allows the contents of a source model to be supplied to views as pre-sorted data.
QSortFilterProxyModel 使得源模型的数据在提供给视图之前可以被筛选,同时允许源模型的数据以排好序的数据提供给视图。


Custom filtering models  
自定义筛选模型


The QSortFilterProxyModel class provides a filtering model that is fairly versatile, and which can be used in a variety of common situations. For advanced users, QSortFilterProxyModel can be subclassed, providing a mechanism that enables custom filters to be implemented.
QSortFilterProxyModel类提供了一个相当通用多变的筛选模型,它可以用于各种常见得情况。对于高级使用者,可以子类化QSortFilterProxyModel来提供一个能够执行自定义筛选的机制。


Subclasses of QSortFilterProxyModel can reimplement two virtual functions that are called whenever a model index from the proxy model is requested or used:
子类化QSortFilterProxyModel可以重新实现两个虚函数,当请求或使用代理模型的模型索引时要调用这两个函数:


  • filterAcceptsColumn() is used to filter specific columns from part of the source model.
  • filterAcceptsColumn()函数用于筛选源模型中指定的列
  • filterAcceptsRow() is used to filter specific rows from part of the source model.
  • filterAcceptsRow()函数用于筛选源模型中指定的行

The default implementations of the above functions in QSortFilterProxyModel return true to ensure that all items are passed through to views; reimplementations of these functions should return false to filter out individual rows and columns.
以上两个QSortFilterProxyModel函数的默认实现返回true,以确保所有的项都可以传递给视图;重新实现这些函数应该返回false以筛选出单独的行和列。


Custom sorting models
自定义排序模型
QSortFilterProxyModel instances use Qt's built-in qStableSort() function to set up mappings between items in the source model and those in the proxy model, allowing a sorted hierarchy of items to be exposed to views without modifying the structure of the source model. To provide custom sorting behavior, reimplement the lessThan() function to perform custom comparisons.
QSortFilterProxyModel 实例使用Qt内建的qStableSort()函数来建立源模型项和代理模型项之间的映射,在不改变源模型结构的情况下将一个排序后的项显示到视图上。为了提供自定义的排序行为,就要重新实现 lessThan()函数以执行自定义的比较。

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

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