Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > a6711891ce757817bba854bf3f25205a > files > 1794

qtjambi-doc-4.3.3-3mdv2008.1.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /home/gvatteka/dev/qt-4.3/doc/src/model-view-programming.qdoc -->
<head>
  <title>Model/View Programming</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Model/View Programming<br /><small></small></h1>
<ul><li><a href="#introduction">Introduction</a></li>
<li><a href="#the-model-view-architecture">The Model/View Architecture</a></li>
<ul><li><a href="#models">Models</a></li>
<li><a href="#views">Views</a></li>
<li><a href="#delegates">Delegates</a></li>
<li><a href="#sorting">Sorting</a></li>
<li><a href="#convenience-classes">Convenience Classes</a></li>
</ul>
<li><a href="#the-model-view-components">The Model/View Components</a></li>
<li><a href="#related-examples">Related Examples</a></li>
</ul>
<a name="introduction"></a>
<h3>Introduction</h3>
<p>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 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.</p>
<a name="the-model-view-architecture"></a>
<h3>The Model/View Architecture</h3>
<p>Model-View-Controller (MVC) is a design pattern originating from Smalltalk that is often used when building user interfaces. In <a href="guibooks.html#design-patterns">Design Patterns</tt></a>, Gamma et al. write:</p>
<blockquote><p>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.</p>
</blockquote>
<p>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 <i>delegate</i>. 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.</p>
<p><table align="center" cellpadding="2" cellspacing="1" border="0">
<tr valign="top" class="odd"><td><img src="images/modelview-overview.png" /></td><td><b>The model/view architecture</b><p>The model communicates with a source of data, providing an <i>interface</i> 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.</p>
<p>The view obtains <i>model indexes</i> 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.</p>
<p>In standard views, a <i>delegate</i> renders the items of data. When an item is edited, the delegate communicates with the model directly using model indexes.</p>
</td></tr>
</table></p>
<p>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 <i>abstract</i> 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.</p>
<p>Models, views, and delegates communicate with each other using <i>signals and slots</i>:</p>
<ul>
<li>Signals from the model inform the view about changes to the data held by the data source.</li>
<li>Signals from the view provide information about the user's interaction with the items being displayed.</li>
<li>Signals from the delegate are used during editing to tell the model and view about the state of the editor.</li>
</ul>
<a name="models"></a>
<h4>Models</h4>
<p>All item models are based on the <a href="core/QAbstractItemModel.html"><tt>QAbstractItemModel</tt></a> 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.</p>
<p>The basic concepts surrounding models are presented in the section on <a href="model-view-model.html">Model Classes</tt></a>.</p>
<p><a href="core/QAbstractItemModel.html"><tt>QAbstractItemModel</tt></a> 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 <a href="core/QAbstractListModel.html"><tt>QAbstractListModel</tt></a> and <a href="gui/QAbstractTableModel.html"><tt>QAbstractTableModel</tt></a> 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.</p>
<p>The process of subclassing models is discussed in the section on <a href="model-view-creating-models.html">Creating New Models</tt></a>.</p>
<p>Qt provides some ready-made models that can be used to handle items of data:</p>
<ul>
<li><a href="gui/QStringListModel.html"><tt>QStringListModel</tt></a> is used to store a simple list of <a href="porting4.html#qstring"><tt>QString</tt></a> items.</li>
<li><a href="gui/QStandardItemModel.html"><tt>QStandardItemModel</tt></a> manages more complex tree structures of items, each of which can contain arbitrary data.</li>
<li><a href="gui/QDirModel.html"><tt>QDirModel</tt></a> provides information about files and directories in the local filing system.</li>
<li><a href="sql/QSqlQueryModel.html"><tt>QSqlQueryModel</tt></a>, <a href="sql/QSqlTableModel.html"><tt>QSqlTableModel</tt></a>, and <a href="sql/QSqlRelationalTableModel.html"><tt>QSqlRelationalTableModel</tt></a> are used to access databases using model/view conventions.</li>
</ul>
<p>If these standard models do not meet your requirements, you can subclass <a href="core/QAbstractItemModel.html"><tt>QAbstractItemModel</tt></a>, <a href="core/QAbstractListModel.html"><tt>QAbstractListModel</tt></a>, or <a href="gui/QAbstractTableModel.html"><tt>QAbstractTableModel</tt></a> to create your own custom models.</p>
<a name="views"></a>
<h4>Views</h4>
<p>Complete implementations are provided for different kinds of views: <a href="gui/QListView.html"><tt>QListView</tt></a> displays a list of items, <a href="gui/QTableView.html"><tt>QTableView</tt></a> displays data from a model in a table, and <a href="gui/QTreeView.html"><tt>QTreeView</tt></a> shows model items of data in a hierarchical list. Each of these classes is based on the <a href="gui/QAbstractItemView.html"><tt>QAbstractItemView</tt></a> abstract base class. Although these classes are ready-to-use implementations, they can also be subclassed to provide customized views.</p>
<p>The available views are examined in the section on <a href="model-view-view.html">View Classes</tt></a>.</p>
<a name="delegates"></a>
<h4>Delegates</h4>
<p><a href="gui/QAbstractItemDelegate.html"><tt>QAbstractItemDelegate</tt></a> is the abstract base class for delegates in the model/view framework. A default delegate implementation is provided by the <a href="gui/QItemDelegate.html"><tt>QItemDelegate</tt></a> class, and this is used as the default delegate by Qt's standard views.</p>
<p>Delegates are described in the section on <a href="model-view-delegate.html">Delegate Classes</tt></a>.</p>
<a name="sorting"></a>
<h4>Sorting</h4>
<p>There are two ways of approaching sorting in the model/view architecture; which approach to choose depends on your underlying model.</p>
<p>If your model is sortable, i.e, if it reimplements the QAbstractItemModel::sort() function, both <a href="gui/QTableView.html"><tt>QTableView</tt></a> and <a href="gui/QTreeView.html"><tt>QTreeView</tt></a> provide an API that allows you to sort your model data programmatically. In addition, you can enable interactive sorting (i.e&#x2e; 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.</p>
<p>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 <a href="model-view-proxy-models.html">Proxy Models</tt></a>.</p>
<a name="convenience-classes"></a>
<h4>Convenience Classes</h4>
<p>A number of <i>convenience</i> 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 <a href="gui/QListWidget.html"><tt>QListWidget</tt></a>, <a href="gui/QTreeWidget.html"><tt>QTreeWidget</tt></a>, and <a href="gui/QTableWidget.html"><tt>QTableWidget</tt></a>; these provide similar behavior to the <tt>QListBox</tt>, <tt>QListView</tt>, and <tt>QTable</tt> classes in Qt 3.</p>
<p>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.</p>
<p>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 <a href="gui/QListView.html"><tt>QListView</tt></a>, <a href="gui/QTableView.html"><tt>QTableView</tt></a>, and <a href="gui/QTreeView.html"><tt>QTreeView</tt></a> with <a href="gui/QStandardItemModel.html"><tt>QStandardItemModel</tt></a>.</p>
<a name="the-model-view-components"></a>
<h3>The Model/View Components</h3>
<p>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.</p>
<ul>
<li><a href="model-view-using.html">Using Models and Views</tt></a> <ul><li><a href="model-view-using.html#introduction">Introduction</a></li>
<li><a href="model-view-using.html#using-views-with-an-existing-model">Using Views with an Existing Model</a></li>
</ul>
</li>
<li><a href="model-view-model.html">Model Classes</tt></a> <ul><li><a href="model-view-model.html#basic-concepts">Basic Concepts</a></li>
<ul><li><a href="model-view-model.html#model-indexes">Model Indexes</a></li>
<li><a href="model-view-model.html#rows-and-columns">Rows and Columns</a></li>
<li><a href="model-view-model.html#parents-of-items">Parents of Items</a></li>
<li><a href="model-view-model.html#item-roles">Item Roles</a></li>
<li><a href="model-view-model.html#summary-of-concepts">Summary of Concepts</a></li>
<li><a href="model-view-model.html#using-model-indexes">Using Model Indexes</a></li>
</ul>
<li><a href="model-view-model.html#further-reading">Further Reading</a></li>
</ul>
</li>
<li><a href="model-view-creating-models.html">Creating New Models</tt></a> <ul><li><a href="model-view-creating-models.html#introduction">Introduction</a></li>
<li><a href="model-view-creating-models.html#designing-a-model">Designing a Model</a></li>
<li><a href="model-view-creating-models.html#a-read-only-example-model">A Read-Only Example Model</a></li>
<ul><li><a href="model-view-creating-models.html#dimensions-of-the-model">Dimensions of The Model</a></li>
<li><a href="model-view-creating-models.html#model-headers-and-data">Model Headers and Data</a></li>
</ul>
<li><a href="model-view-creating-models.html#an-editable-model">An Editable Model</a></li>
<ul><li><a href="model-view-creating-models.html#making-the-model-editable">Making the Model Editable</a></li>
<li><a href="model-view-creating-models.html#inserting-and-removing-rows">Inserting and Removing Rows</a></li>
</ul>
<li><a href="model-view-creating-models.html#next-steps">Next Steps</a></li>
</ul>
</li>
<li><a href="model-view-view.html">View Classes</tt></a> <ul><li><a href="model-view-view.html#concepts">Concepts</a></li>
<li><a href="model-view-view.html#using-an-existing-view">Using an Existing View</a></li>
<ul><li><a href="model-view-view.html#using-a-model">Using a Model</a></li>
<li><a href="model-view-view.html#using-multiple-views-onto-the-same-model">Using Multiple Views onto the Same Model</a></li>
</ul>
<li><a href="model-view-view.html#handling-selections-of-items">Handling Selections of Items</a></li>
<ul><li><a href="model-view-view.html#sharing-selections-between-views">Sharing Selections Between Views</a></li>
</ul>
</ul>
</li>
<li><a href="model-view-selection.html">Handling Selections in Item Views</tt></a> <ul><li><a href="model-view-selection.html#concepts">Concepts</a></li>
<li><a href="model-view-selection.html#using-a-selection-model">Using a Selection Model</a></li>
<ul><li><a href="model-view-selection.html#selecting-items">Selecting Items</a></li>
<li><a href="model-view-selection.html#reading-the-selection-state">Reading the Selection State</a></li>
<li><a href="model-view-selection.html#updating-a-selection">Updating a Selection</a></li>
<li><a href="model-view-selection.html#selecting-all-items-in-a-model">Selecting All Items in a Model</a></li>
</ul>
</ul>
</li>
<li><a href="model-view-delegate.html">Delegate Classes</tt></a> <ul><li><a href="model-view-delegate.html#concepts">Concepts</a></li>
<li><a href="model-view-delegate.html#using-an-existing-delegate">Using an Existing Delegate</a></li>
<li><a href="model-view-delegate.html#a-simple-delegate">A Simple Delegate</a></li>
<ul><li><a href="model-view-delegate.html#providing-an-editor">Providing an Editor</a></li>
<li><a href="model-view-delegate.html#submitting-data-to-the-model">Submitting Data to the Model</a></li>
<li><a href="model-view-delegate.html#updating-the-editor-s-geometry">Updating the Editor's Geometry</a></li>
<li><a href="model-view-delegate.html#editing-hints">Editing Hints</a></li>
</ul>
</ul>
</li>
<li><a href="model-view-convenience.html">Item View Convenience Classes</tt></a> <ul><li><a href="model-view-convenience.html#overview">Overview</a></li>
<li><a href="model-view-convenience.html#list-widgets">List Widgets</a></li>
<li><a href="model-view-convenience.html#tree-widgets">Tree Widgets</a></li>
<li><a href="model-view-convenience.html#table-widgets">Table Widgets</a></li>
<li><a href="model-view-convenience.html#common-features">Common Features</a></li>
<ul><li><a href="model-view-convenience.html#hidden-items">Hidden Items</a></li>
<li><a href="model-view-convenience.html#selections">Selections</a></li>
<li><a href="model-view-convenience.html#searching">Searching</a></li>
</ul>
</ul>
</li>
<li><a href="model-view-dnd.html">Using Drag and Drop with Item Views</tt></a> <ul><li><a href="model-view-dnd.html#overview">Overview</a></li>
<li><a href="model-view-dnd.html#using-convenience-views">Using Convenience Views</a></li>
<li><a href="model-view-dnd.html#using-model-view-classes">Using Model/View Classes</a></li>
<ul><li><a href="model-view-dnd.html#enabling-drag-and-drop-for-items">Enabling Drag and Drop for Items</a></li>
<li><a href="model-view-dnd.html#encoding-exported-data">Encoding Exported Data</a></li>
<li><a href="model-view-dnd.html#inserting-dropped-data-into-a-model">Inserting Dropped Data into a Model</a></li>
<li><a href="model-view-dnd.html#decoding-imported-data">Decoding Imported Data</a></li>
</ul>
</ul>
</li>
<li><a href="model-view-proxy-models.html">Proxy Models</tt></a> <ul><li><a href="model-view-proxy-models.html#overview">Overview</a></li>
<li><a href="model-view-proxy-models.html#using-proxy-models">Using Proxy Models</a></li>
<li><a href="model-view-proxy-models.html#customizing-proxy-models">Customizing Proxy Models</a></li>
<ul><li><a href="model-view-proxy-models.html#custom-filtering-models">Custom Filtering Models</a></li>
<li><a href="model-view-proxy-models.html#custom-sorting-models">Custom Sorting Models</a></li>
</ul>
</ul>
</li>
<li><a href="model-view-model-subclassing.html">Model Subclassing Reference</tt></a> <ul><li><a href="model-view-model-subclassing.html#introduction">Introduction</a></li>
<li><a href="model-view-model-subclassing.html#item-data-handling">Item Data Handling</a></li>
<ul><li><a href="model-view-model-subclassing.html#read-only-access">Read-Only Access</a></li>
<li><a href="model-view-model-subclassing.html#editable-items">Editable Items</a></li>
<li><a href="model-view-model-subclassing.html#resizable-models">Resizable Models</a></li>
<li><a href="model-view-model-subclassing.html#lazy-population-of-model-data">Lazy Population of Model Data</a></li>
</ul>
<li><a href="model-view-model-subclassing.html#navigation-and-model-index-creation">Navigation and Model Index Creation</a></li>
<ul><li><a href="model-view-model-subclassing.html#parents-and-children">Parents and Children</a></li>
</ul>
<li><a href="model-view-model-subclassing.html#drag-and-drop-support-and-mime-type-handling">Drag and Drop Support and MIME Type Handling</a></li>
<ul><li><a href="model-view-model-subclassing.html#mime-data">MIME Data</a></li>
<li><a href="model-view-model-subclassing.html#accepting-dropped-data">Accepting Dropped Data</a></li>
<li><a href="model-view-model-subclassing.html#convenience-views">Convenience Views</a></li>
</ul>
<li><a href="model-view-model-subclassing.html#performance-optimization-for-large-amounts-of-data">Performance Optimization for Large Amounts of Data</a></li>
</ul>
</li>
</ul>
<p>See also the list of <a href="qt4-interview.html#model-view-classes">Model/View Classes</tt></a>.</p>
<a name="related-examples"></a>
<h3>Related Examples</h3>
<ul>
<li>Dir View</tt></li>
<li>Spin Box Delegate</tt></li>
<li>Pixelator</tt></li>
<li>Simple Tree Model</tt></li>
<li>Chart</tt></li>
</ul>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2007 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Jambi </div></td>
</tr></table></div></address></body>
</html>