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

« back to all changes in this revision

Viewing changes to doc/html/sql-presenting.html

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

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
<!-- sql-programming.qdoc -->
 
6
<head>
 
7
  <title>Qt 4.6: Presenting Data in a Table View</title>
 
8
  <link rel="prev" href="sql-model.html" />
 
9
  <link rel="contents" href="sql-programming.html" />
 
10
  <link rel="next" href="sql-forms.html" />
 
11
  <link href="classic.css" rel="stylesheet" type="text/css" />
 
12
</head>
 
13
<body>
 
14
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
15
<tr>
 
16
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
 
17
<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="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td><td class="searchBar" align="right" valign="center"><form action="http://www.google.com/cse" id="cse-search-box"><div><input type="hidden" name="cx" value="000136343326384750312:dhbxnqlakyu" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" /></div></form></td>
 
18
</tr></table><p>
 
19
[Previous: <a href="sql-model.html">Using the SQL Model Classes</a>]
 
20
[<a href="sql-programming.html">SQL Programming</a>]
 
21
[Next: <a href="sql-forms.html">Creating Data-Aware Forms</a>]
 
22
</p>
 
23
<h1 class="title">Presenting Data in a Table View<br /><span class="subtitle"></span>
 
24
</h1>
 
25
<p>The <a href="qsqlquerymodel.html">QSqlQueryModel</a>, <a href="qsqltablemodel.html">QSqlTableModel</a>, and <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> classes can be used as a data source for Qt's view classes such as <a href="qlistview.html">QListView</a>, <a href="qtableview.html">QTableView</a>, and <a href="qtreeview.html">QTreeView</a>. In practice, <a href="qtableview.html">QTableView</a> is by far the most common choice, because an SQL result set is essentially a two-dimensional data structure.</p>
 
26
<p align="center"><img src="images/relationaltable.png" alt="A table view displaying a QSqlTableModel" /></p><p>The following example creates a view based on an SQL data model:</p>
 
27
<pre>     QTableView *view = new QTableView;
 
28
     view-&gt;setModel(model);
 
29
     view-&gt;show();</pre>
 
30
<p>If the model is a read-write model (e.g&#x2e;, <a href="qsqltablemodel.html">QSqlTableModel</a>), the view lets the user edit the fields. You can disable this by calling</p>
 
31
<pre>     view-&gt;setEditTriggers(QAbstractItemView::NoEditTriggers);</pre>
 
32
<p>You can use the same model as a data source for multiple views. If the user edits the model through one of the views, the other views will reflect the changes immediately. The <a href="sql-tablemodel.html">Table Model</a> example shows how it works.</p>
 
33
<p>View classes display a header at the top to label the columns. To change the header texts, call <a href="qabstractitemmodel.html#setHeaderData">setHeaderData()</a> on the model. The header's labels default to the table's field names. For example:</p>
 
34
<pre>     model-&gt;setHeaderData(0, Qt::Horizontal, QObject::tr(&quot;ID&quot;));
 
35
     model-&gt;setHeaderData(1, Qt::Horizontal, QObject::tr(&quot;Name&quot;));
 
36
     model-&gt;setHeaderData(2, Qt::Horizontal, QObject::tr(&quot;City&quot;));
 
37
     model-&gt;setHeaderData(3, Qt::Horizontal, QObject::tr(&quot;Country&quot;));</pre>
 
38
<p><a href="qtableview.html">QTableView</a> also has a vertical header on the left with numbers identifying the rows. If you insert rows programmatically using <a href="qsqltablemodel.html#insertRows">QSqlTableModel::insertRows</a>(), the new rows will be marked with an asterisk (*) until they are submitted using <a href="qsqltablemodel.html#submitAll">submitAll()</a> or automatically when the user moves to another record (assuming the <a href="qsqltablemodel.html#EditStrategy-enum">edit strategy</a> is <a href="qsqltablemodel.html#EditStrategy-enum">QSqlTableModel::OnRowChange</a>).</p>
 
39
<p align="center"><img src="images/insertrowinmodelview.png" alt="Inserting a row in a model" /></p><p>Likewise, if you remove rows using <a href="qsqltablemodel.html#removeRows">removeRows()</a>, the rows will be marked with an exclamation mark (!) until the change is submitted.</p>
 
40
<p>The items in the view are rendered using a delegate. The default delegate, <a href="qitemdelegate.html">QItemDelegate</a>, handles the most common data types (<tt>int</tt>, <a href="qstring.html">QString</a>, <a href="qimage.html">QImage</a>, etc.)&#x2e; The delegate is also responsible for providing editor widgets (e.g&#x2e;, a combobox) when the user starts editing an item in the view. You can create your own delegates by subclassing <a href="qabstractitemdelegate.html">QAbstractItemDelegate</a> or <a href="qitemdelegate.html">QItemDelegate</a>. See <a href="model-view-programming.html">Model/View Programming</a> for more information.</p>
 
41
<p><a href="qsqltablemodel.html">QSqlTableModel</a> is optimized to operate on a single table at a time. If you need a read-write model that operates on an arbitrary result set, you can subclass <a href="qsqlquerymodel.html">QSqlQueryModel</a> and reimplement <a href="qabstractitemmodel.html#flags">flags()</a> and <a href="qabstractitemmodel.html#setData">setData()</a> to make it read-write. The following two functions make fields 1 and 2 of a query model editable:</p>
 
42
<pre> Qt::ItemFlags EditableSqlModel::flags(
 
43
         const QModelIndex &amp;index) const
 
44
 {
 
45
     Qt::ItemFlags flags = QSqlQueryModel::flags(index);
 
46
     if (index.column() == 1 || index.column() == 2)
 
47
         flags |= Qt::ItemIsEditable;
 
48
     return flags;
 
49
 }
 
50
 
 
51
 bool EditableSqlModel::setData(const QModelIndex &amp;index, const QVariant &amp;value, int <span class="comment">/* role */</span>)
 
52
 {
 
53
     if (index.column() &lt; 1 || index.column() &gt; 2)
 
54
         return false;
 
55
 
 
56
     QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
 
57
     int id = data(primaryKeyIndex).toInt();
 
58
 
 
59
     clear();
 
60
 
 
61
     bool ok;
 
62
     if (index.column() == 1) {
 
63
         ok = setFirstName(id, value.toString());
 
64
     } else {
 
65
         ok = setLastName(id, value.toString());
 
66
     }
 
67
     refresh();
 
68
     return ok;
 
69
 }</pre>
 
70
<p>The setFirstName() helper function is defined as follows:</p>
 
71
<pre> bool EditableSqlModel::setFirstName(int personId, const QString &amp;firstName)
 
72
 {
 
73
     QSqlQuery query;
 
74
     query.prepare(&quot;update person set firstname = ? where id = ?&quot;);
 
75
     query.addBindValue(firstName);
 
76
     query.addBindValue(personId);
 
77
     return query.exec();
 
78
 }</pre>
 
79
<p>The setLastName() function is similar. See the <a href="sql-querymodel.html">Query Model</a> example for the complete source code.</p>
 
80
<p>Subclassing a model makes it possible to customize it in many ways: You can provide tooltips for the items, change the background color, provide calculated values, provide different values for viewing and editing, handle null values specially, and more. See <a href="model-view-programming.html">Model/View Programming</a> as well as the <a href="qabstractitemview.html">QAbstractItemView</a> reference documentation for details.</p>
 
81
<p>If all you need is to resolve a foreign key to a more human-friendly string, you can use <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a>. For best results, you should also use <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a>, a delegate that provides combobox editors for editing foreign keys.</p>
 
82
<p align="center"><img src="images/relationaltable.png" alt="Editing a foreign key in a relational table" /></p><p>The <a href="sql-relationaltablemodel.html">Relational Table Model</a> example illustrates how to use <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> in conjunction with <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a> to provide tables with foreign key support.</p>
 
83
<p>
 
84
[Previous: <a href="sql-model.html">Using the SQL Model Classes</a>]
 
85
[<a href="sql-programming.html">SQL Programming</a>]
 
86
[Next: <a href="sql-forms.html">Creating Data-Aware Forms</a>]
 
87
</p>
 
88
<p /><address><hr /><div align="center">
 
89
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
90
<td width="40%" align="left">Copyright &copy; 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
 
91
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
92
<td width="40%" align="right"><div align="right">Qt 4.6.0</div></td>
 
93
<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script><script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script></tr></table></div></address></body>
 
94
</html>