~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/model-view-model.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/model-view-programming.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Model Classes</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
    <link rel="prev" href="model-view-using.html" />
 
15
    <link rel="contents" href="model-view-programming.html" />
 
16
    <link rel="next" href="model-view-creating-models.html" />
 
17
</head>
 
18
<body>
 
19
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
20
<tr>
 
21
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
22
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
23
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><p>
 
24
[Previous: <a href="model-view-using.html">Using Models and Views</a>]
 
25
[<a href="model-view-programming.html">Contents</a>]
 
26
[Next: <a href="model-view-creating-models.html">Creating New Models</a>]
 
27
</p>
 
28
<h1 align="center">Model Classes</h1>
 
29
<ul><li><a href="#basic-concepts">Basic Concepts</a></li>
 
30
<ul><li><a href="#model-indexes">Model Indexes</a></li>
 
31
<li><a href="#rows-and-columns">Rows and Columns</a></li>
 
32
<li><a href="#parents-of-items">Parents of Items</a></li>
 
33
<li><a href="#item-roles">Item Roles</a></li>
 
34
<li><a href="#summary-of-concepts">Summary of Concepts</a></li>
 
35
<li><a href="#using-model-indexes">Using Model Indexes</a></li>
 
36
</ul>
 
37
<li><a href="#further-reading">Further Reading</a></li>
 
38
</ul>
 
39
<a name="basic-concepts"></a>
 
40
<h2>Basic Concepts</h2>
 
41
<p>In the model/view architecture, the model provides a standard interface that views and delegates use to access data. In Qt, the standard interface is defined by the <a href="qabstractitemmodel.html">QAbstractItemModel</a> class. No matter how the items of data are stored in any underlying data structure, all subclasses of <a href="qabstractitemmodel.html">QAbstractItemModel</a> represent the data as a hierarchical structure containing tables of items. Views use this <i>convention</i> to access items of data in the model, but they are not restricted in the way that they present this information to the user.</p>
 
42
<center><img src="images/modelview-models.png" /></center><p>Models also notify any attached views about changes to data through the signals and slots mechanism.</p>
 
43
<p>This chapter describes some basic concepts that are central to the way item of data are accessed by other components via a model class. More advanced concepts are discussed in later chapters.</p>
 
44
<a name="model-indexes"></a>
 
45
<h3>Model Indexes</h3>
 
46
<p>To ensure that the representation of the data is kept separate from the way it is accessed, the concept of a <i>model index</i> is introduced. Each piece of information that can be obtained via a model is represented by a model index. Views and delegates use these indexes to request items of data to display.</p>
 
47
<p>As a result, only the model needs to know how to obtain data, and the type of data managed by the model can be defined fairly generally. Model indexes contain a pointer to the model that created them, and this prevents confusion when working with more than one model.</p>
 
48
<pre>&nbsp;   QAbstractItemModel *model = index.model();</pre>
 
49
<p>Model indexes provide <i>temporary</i> references to pieces of information, and can be used to retrieve or modify data via the model. Since models may reorganize their internal structures from time to time, model indexes may become invalid, and <i>should not be stored</i>. If a long-term reference to a piece of information is required, a <i>persistent model index</i> must be created. This provides a reference to the information that the model keeps up-to-date. Temporary model indexes are provided by the <a href="qmodelindex.html">QModelIndex</a> class, and persistent model indexes are provided by the <a href="qpersistentmodelindex.html">QPersistentModelIndex</a> class.</p>
 
50
<p>To obtain a model index that corresponds to an item of data, three properties must be specified to the model: a row number, a column number, and the model index of a parent item. The following sections describe and explain these properties in detail.</p>
 
51
<a name="rows-and-columns"></a>
 
52
<h3>Rows and Columns</h3>
 
53
<p>In its most basic form, a model can be accessed as a simple table in which items are located by their row and column numbers. <i>This does not mean that the underlying pieces of data are stored in an array structure</i>; the use of row and column numbers is only a convention to allow components to communicate with each other. We can retrieve information about any given item by specifying its row and column numbers to the model, and we receive an index that represents the item:</p>
 
54
<pre>&nbsp;   QModelIndex index = model-&gt;index(row, column, ...);</pre>
 
55
<p>Models that provide interfaces to simple, single level data structures like lists and tables do not need any other information to be provided but, as the above code indicates, we need to supply more information when obtaining a model index.</p>
 
56
<table align="center" cellpadding="2" cellspacing="1" border="0">
 
57
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/modelview-tablemodel.png" /></td><td><b>Rows and columns</b><p>The diagram shows a representation of a basic table model in which each item is located by a pair of row and column numbers. By passing the relevant row and column numbers to the model we obtain a model index that refers to an item of data.</p>
 
58
<pre>&nbsp;   QModelIndex indexA = model-&gt;index(0, 0, QModelIndex());
 
59
    QModelIndex indexB = model-&gt;index(1, 1, QModelIndex());
 
60
    QModelIndex indexC = model-&gt;index(2, 1, QModelIndex());</pre>
 
61
<p>Top level items in a model are always referenced by specifying <tt>QModelIndex()</tt> as their parent item. This is discussed in the next section.</p>
 
62
</td></tr>
 
63
</table>
 
64
<a name="parents-of-items"></a>
 
65
<h3>Parents of Items</h3>
 
66
<p>The table-like interface to item data provided by models is ideal when using data in a table or list view; the row and column number system maps exactly to the way the views display items. However, structures such as tree views require the model to expose a more flexible interface to the items within. As a result, each item can also be the parent of another table of items, in much the same way that a top-level item in a tree view can contain another list of items.</p>
 
67
<p>When requesting an index for a model item, we must provide some information about the item's parent. Outside the model, the only way to refer to an item is through a model index, so a parent model index must also be given:</p>
 
68
<pre>&nbsp;   QModelIndex index = model-&gt;index(row, column, parent);</pre>
 
69
<table align="center" cellpadding="2" cellspacing="1" border="0">
 
70
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/modelview-treemodel.png" /></td><td><b>Parents, rows, and columns</b><p>The diagram shows a representation of a tree model in which each item is referred to by a parent, a row number, and a column number.</p>
 
71
<p>Items &quot;A&quot; and &quot;C&quot; are represented as top-level siblings in the model:</p>
 
72
<pre>&nbsp;   QModelIndex indexA = model-&gt;index(0, 0, QModelIndex());
 
73
    QModelIndex indexC = model-&gt;index(2, 1, QModelIndex());</pre>
 
74
<p>Item &quot;A&quot; has a number of children. A model index for item &quot;B&quot; is obtained with the following code:</p>
 
75
<pre>&nbsp;   QModelIndex indexB = model-&gt;index(1, 0, indexA);</pre>
 
76
</td></tr>
 
77
</table>
 
78
<a name="item-roles"></a>
 
79
<h3>Item Roles</h3>
 
80
<p>Items in a model can perform various <i>roles</i> for other components, allowing different kinds of data to be supplied for different situations. For example, the <a href="qt.html#ItemDataRole-enum">DisplayRole</a> role is used to access a string that can be displayed as text in a view. Typically, items contain data for a number of different roles.</p>
 
81
<p>We can ask the model for the item's data by passing it the model index corresponding to the item, and by specifying a role to obtain the type of data we want:</p>
 
82
<pre>&nbsp;   QVariant value = model-&gt;data(index, role);</pre>
 
83
<table align="center" cellpadding="2" cellspacing="1" border="0">
 
84
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/modelview-roles.png" /></td><td><b>Item roles</b><p>The role indicates to the model which type of data is being referred to. Views can display the roles in different ways, so it is important to supply appropriate information for each role.</p>
 
85
<p>We will examine roles in more detail when we <a href="model-view-creating-models.html">create an example model</a>.</p>
 
86
</td></tr>
 
87
</table>
 
88
<p>Many of the most common uses for items of data are represented by standard roles. However, it is also possible to define additional roles for your own purposes.</p>
 
89
<a name="summary-of-concepts"></a>
 
90
<h3>Summary of Concepts</h3>
 
91
<ul>
 
92
<li>Model indexes give views and delegates information about the location of items provided by models in a way that is independent of any underlying data structures.</li>
 
93
<li>Items are referred to by their row and column numbers, and by the model index of their parent items.</li>
 
94
<li>Model indexes are constructed by models at the request of other components, such as views and delegates.</li>
 
95
<li>If a valid model index is specified when an index is requested (using <a href="qabstractitemmodel.html#index">index()</a>, the model understands that it refers to a parent item, and that it must return an index that refers to a child of that item.</li>
 
96
<li>If an invalid model index is specified when an index is requested (using <a href="qabstractitemmodel.html#index">index()</a>, the model understands that it must return a model index that refers to a top-level item in the model.</li>
 
97
<li>The <a href="qt.html#ItemDataRole-enum">role</a> distinguishes between the different kinds of data associated with an item.</li>
 
98
</ul>
 
99
<a name="using-model-indexes"></a>
 
100
<h3>Using Model Indexes</h3>
 
101
<p>To demonstrate how data can be retrieved from a model, using model indexes, we set up a <a href="qdirmodel.html">QDirModel</a> without a view and display the names of files and directories in a widget. Although this does not show a normal way of using a model, it demonstrates the conventions used by models when dealing with model indexes.</p>
 
102
<p>We construct a directory model in the following way:</p>
 
103
<pre>&nbsp;       QDirModel *model = new QDirModel;
 
104
        QModelIndex parentIndex = model-&gt;index(QDir::currentPath());
 
105
        int numRows = model-&gt;rowCount(parentIndex);</pre>
 
106
<p>In this case, we set up a default <a href="qdirmodel.html">QDirModel</a>, obtain a parent index using a specific implementation of <a href="qdirmodel.html#index">index()</a> provided by that model, and we count the number of rows in the model using the <a href="qdirmodel.html#rowCount">rowCount()</a> function.</p>
 
107
<p>For simplicity, we are only interested in the items in the first column of the model. We examine each row in turn, obtaining a model index for the first item in each row, and read the data stored for that item in the model.</p>
 
108
<pre>&nbsp;       for (int row = 0; row &lt; numRows; ++row) {
 
109
            QModelIndex index = model-&gt;index(row, 0, parentIndex);</pre>
 
110
<p>To obtain a model index, we specify the row number, column number (zero for the first column), and the appropriate model index for the parent of all the items that we want. The text stored in each item is retrieved using the model's <a href="qdirmodel.html#data">data()</a> function. We specify the model index and the <a href="qt.html#ItemDataRole-enum">DisplayRole</a> to obtain data for the item in the form of a string.</p>
 
111
<pre>&nbsp;           QString text = model-&gt;data(index, Qt::DisplayRole).toString();
 
112
            // Display the text in a widget.
 
113
 
 
114
        }</pre>
 
115
<p>The above example demonstrates the basic principles used to retrieve data from a model:</p>
 
116
<ul>
 
117
<li>The dimensions of a model can be found using <a href="qabstractitemmodel.html#rowCount">rowCount()</a> and <a href="qabstractitemmodel.html#columnCount">columnCount()</a>. These functions generally require a parent model index to be specified.</li>
 
118
<li>Model indexes are used to access items in the model. The row, column, and parent model index are needed to specify the item.</li>
 
119
<li>To access top-level items in a model, specify a null model index as the parent index with <tt>QModelIndex()</tt>.</li>
 
120
<li>Items contain data for different roles. To obtain the data for a particular role, both the model index and the role must be supplied to the model.</li>
 
121
</ul>
 
122
<a name="further-reading"></a>
 
123
<h2>Further Reading</h2>
 
124
<p>New models can be created by implementing the standard interface provided by <a href="qabstractitemmodel.html">QAbstractItemModel</a>. In the <a href="model-view-creating-models.html">next chapter</a>, we will demonstrate this by creating a convenient ready-to-use model for holding lists of strings.</p>
 
125
<p>
 
126
[Previous: <a href="model-view-using.html">Using Models and Views</a>]
 
127
[<a href="model-view-programming.html">Contents</a>]
 
128
[Next: <a href="model-view-creating-models.html">Creating New Models</a>]
 
129
</p>
 
130
<p /><address><hr /><div align="center">
 
131
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
132
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
133
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
134
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
135
</tr></table></div></address></body>
 
136
</html>