![]() |
Home · Examples |
[Previous: Model/View Programming][Next: Using Models and Views]
Qt Jambi contains a 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 used 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.
![]() | 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. |
Models, views, and delegates communicate with each other using signals and slots:
The basic concepts surrounding models are presented in the section on 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, the QAbstractListModel 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.
Qt provides some ready-made models that can be used to handle items of data:
The available views are examined in the section on View Classes.Delegates
QAbstractItemDelegate is the abstract base class for delegates in the model/view framework. A default delegate implementation is provided by the QItemDelegate class, and this is used as the default delegate by Qt's standard views.
Delegates are described in the section on Delegate Classes. If your model is sortable, i.e, if it reimplements the QAbstractItemModel::sort() function, both QTableView and QTreeView 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 the QHeaderView::sectionClicked() signal to the QTableView::sortByColumn() slot or the QTreeView::sortByColumn() slot, respectively. 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. 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.Sorting
There are two ways of approaching sorting in the model/view architecture; which approach to choose depends on your underlying model. Convenience Classes
A number of convenience classes are derived from the standard view classes for the benefit of applications that rely on Qt Jambi's item-based item view and table classes. Examples of such classes include QListWidget, QTreeWidget, and QTableWidget. The Model/View Components
The following sections describe the way in which the model/view pattern is used in Qt Jambi. Each section provides an example of use, and is followed by a section showing how you can create new components.
Copyright © 2008 Nokia
Trademarks