Sophie

Sophie

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

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>Proxy Models</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Proxy Models<br /><small></small></h1>
<ul><li><a href="#overview">Overview</a></li>
<li><a href="#using-proxy-models">Using Proxy Models</a></li>
<li><a href="#customizing-proxy-models">Customizing Proxy Models</a></li>
<ul><li><a href="#custom-filtering-models">Custom Filtering Models</a></li>
<li><a href="#custom-sorting-models">Custom Sorting Models</a></li>
</ul>
</ul>
<a name="overview"></a>
<h2>Overview</h2>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<a name="using-proxy-models"></a>
<h2>Using Proxy Models</h2>
<p>Proxy models can be inserted between an existing model and any number of views. Qt is supplied with a standard proxy model, <a href="gui/QSortFilterProxyModel.html"><tt>QSortFilterProxyModel</tt></a>, that is usually instantiated and used directly, but can also be subclassed to provide custom filtering and sorting behavior. The <a href="gui/QSortFilterProxyModel.html"><tt>QSortFilterProxyModel</tt></a> class can be used in the following way:</p>
<pre>        QSortFilterProxyModel *filterModel = new QSortFilterProxyModel(parent);
        filterModel-&gt;setSourceModel(stringListModel);

        QListView *filteredView = new QListView;
        filteredView-&gt;setModel(filterModel);</pre>
<p>Since proxy models are inherit from <a href="core/QAbstractItemModel.html"><tt>QAbstractItemModel</tt></a>, they can be connected to any kind of view, and can be shared between views. They can be also be used to process the information obtained from other proxy models in a pipeline arrangement.</p>
<p>The <a href="gui/QSortFilterProxyModel.html"><tt>QSortFilterProxyModel</tt></a> 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.</p>
<a name="customizing-proxy-models"></a>
<h2>Customizing Proxy Models</h2>
<p>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 <i>filtering</i> 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.</p>
<p><a href="gui/QSortFilterProxyModel.html"><tt>QSortFilterProxyModel</tt></a> 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.</p>
<a name="custom-filtering-models"></a>
<h3>Custom Filtering Models</h3>
<p>The <a href="gui/QSortFilterProxyModel.html"><tt>QSortFilterProxyModel</tt></a> class provides a filtering model that is fairly versatile, and which can be used in a variety of common situations. For advanced users, <a href="gui/QSortFilterProxyModel.html"><tt>QSortFilterProxyModel</tt></a> can be subclassed, providing a mechanism that enables custom filters to be implemented.</p>
<p>Subclasses of <a href="gui/QSortFilterProxyModel.html"><tt>QSortFilterProxyModel</tt></a> can reimplement two virtual functions that are called whenever a model index from the proxy model is requested or used:</p>
<ul>
<li>filterAcceptsColumn() is used to filter specific columns from part of the source model.</li>
<li>filterAcceptsRow() is used to filter specific rows from part of the source model.</li>
</ul>
<p>The default implementations of the above functions in <a href="gui/QSortFilterProxyModel.html"><tt>QSortFilterProxyModel</tt></a> 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.</p>
<a name="custom-sorting-models"></a>
<h3>Custom Sorting Models</h3>
<p><a href="gui/QSortFilterProxyModel.html"><tt>QSortFilterProxyModel</tt></a> 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.</p>
<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>