Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

[Next: Model Classes]

# Model/View Programming

- Introduction
- The Model/View Architecture
- The Model/View Components
- References

## Introduction

Qt 4 introduces 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 and access to more features than was previously possible. 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

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.

If the view and the controller objects are combined, the result is the model/view architecture. This still separates the data in the model from its presentation to the user, but provides a simpler framework based on the same principles. This separation makes it possible to display data from the same model in several different views, and to implement new types of views, without changing the underlying model. To allow flexible handling of user input, we introduce the concept of the delegate. The advantage of using a delegate is that it allows input methods to be defined in addition to those provided by the view, but does not introduce additional complexity in the common case.

Generally, the model/view classes can be separated into the three groups described above: models, views, and delegates. Each of these components are 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. For example, all item models are based on the QAbstractItemModel class. This class can be used to provide models for views that represent data in the form of tables, lists, and trees. However, the QAbstractListModel and QAbstractTableModel classes are better starting points for new list and table models 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.

Complete implementations are provided for different kinds of view widgets based on the QAbstractItemView class. QListView provides a complete implementation of a simple list widget, QTableView displays items from a model in a tabular form, and QTreeView shows model items in a hierarchical list. Although these classes are ready-to-use implementations, they can also be subclassed to provide customized views.

The abstract base class for delegates is the QAbstractItemDelegate class. A default delegate implementation is provided by the QItemDelegate class which is used as the default delegate by standard view implementations.

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, and QTableWidget; these offer compatibility with the `QListBox`, `QListView`, and `QTable` classes from Qt 3.

## The Model/View Components

The following sections describe the way in which the model/view pattern is used in Qt. Each section provides an example of use, and is followed by a section showing how you can create new components.

- Model Classes
- Creating New Models
- View Classes
- Handling Selections
- Delegate Classes
- Convenience Classes

## References

Design Patterns - Elements of Reusable Object-Oriented Software, by Gamma, Helm, Johnson, and Vlissides, ISBN 0-201-63361-2, provides more information on the MVC paradigm, explaining MVC and its sub-patterns in detail.

See also the list of model/view classes.

[Next: Model Classes]