1
<?xml version="1.0" encoding="iso-8859-1"?>
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 -->
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" />
14
<table border="0" cellpadding="0" cellspacing="0" width="100%">
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"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="functions.html"><font color="#004faf">All Functions</font></a> · <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>
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>]
23
<h1 class="title">Presenting Data in a Table View<br /><span class="subtitle"></span>
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->setModel(model);
29
view->show();</pre>
30
<p>If the model is a read-write model (e.g., <a href="qsqltablemodel.html">QSqlTableModel</a>), the view lets the user edit the fields. You can disable this by calling</p>
31
<pre> view->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->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
35
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
36
model->setHeaderData(2, Qt::Horizontal, QObject::tr("City"));
37
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Country"));</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.). The delegate is also responsible for providing editor widgets (e.g., 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 &index) const
45
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
46
if (index.column() == 1 || index.column() == 2)
47
flags |= Qt::ItemIsEditable;
51
bool EditableSqlModel::setData(const QModelIndex &index, const QVariant &value, int <span class="comment">/* role */</span>)
53
if (index.column() < 1 || index.column() > 2)
56
QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
57
int id = data(primaryKeyIndex).toInt();
62
if (index.column() == 1) {
63
ok = setFirstName(id, value.toString());
65
ok = setLastName(id, value.toString());
70
<p>The setFirstName() helper function is defined as follows:</p>
71
<pre> bool EditableSqlModel::setFirstName(int personId, const QString &firstName)
74
query.prepare("update person set firstname = ? where id = ?");
75
query.addBindValue(firstName);
76
query.addBindValue(personId);
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>
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>]
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 © 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>