1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- sql-programming.qdoc -->
<title>Qt 4.8: Presenting Data in a Table View</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li>Presenting Data in a Table View</li>
</ul>
</div>
</div>
<div class="content mainContent">
<link rel="prev" href="sql-model.html" />
<link rel="next" href="sql-forms.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="sql-model.html">Using the SQL Model Classes</a>
<a class="nextPage" href="sql-forms.html">Creating Data-Aware Forms</a>
</p><p/>
<h1 class="title">Presenting Data in a Table View</h1>
<span class="subtitle"></span>
<!-- $$$sql-presenting.html-description -->
<div class="descr"> <a name="details"></a>
<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>
<p class="centerAlign"><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>
<pre class="cpp"> <span class="type"><a href="qtableview.html">QTableView</a></span> <span class="operator">*</span>view <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtableview.html">QTableView</a></span>;
view<span class="operator">-</span><span class="operator">></span>setModel(model);
view<span class="operator">-</span><span class="operator">></span>show();</pre>
<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>
<pre class="cpp"> view<span class="operator">-</span><span class="operator">></span>setEditTriggers(<span class="type"><a href="qabstractitemview.html">QAbstractItemView</a></span><span class="operator">::</span>NoEditTriggers);</pre>
<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>
<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>
<pre class="cpp"> model<span class="operator">-</span><span class="operator">></span>setHeaderData(<span class="number">0</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Horizontal<span class="operator">,</span> <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>tr(<span class="string">"ID"</span>));
model<span class="operator">-</span><span class="operator">></span>setHeaderData(<span class="number">1</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Horizontal<span class="operator">,</span> <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>tr(<span class="string">"Name"</span>));
model<span class="operator">-</span><span class="operator">></span>setHeaderData(<span class="number">2</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Horizontal<span class="operator">,</span> <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>tr(<span class="string">"City"</span>));
model<span class="operator">-</span><span class="operator">></span>setHeaderData(<span class="number">3</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Horizontal<span class="operator">,</span> <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>tr(<span class="string">"Country"</span>));</pre>
<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>
<p class="centerAlign"><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>
<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>
<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>
<pre class="cpp"> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>ItemFlags EditableSqlModel<span class="operator">::</span>flags(
<span class="keyword">const</span> <span class="type"><a href="qmodelindex.html">QModelIndex</a></span> <span class="operator">&</span>index) <span class="keyword">const</span>
{
<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>ItemFlags flags <span class="operator">=</span> <span class="type"><a href="qsqlquerymodel.html">QSqlQueryModel</a></span><span class="operator">::</span>flags(index);
<span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span> <span class="operator">|</span><span class="operator">|</span> index<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span>)
flags <span class="operator">|</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>ItemIsEditable;
<span class="keyword">return</span> flags;
}
<span class="type">bool</span> EditableSqlModel<span class="operator">::</span>setData(<span class="keyword">const</span> <span class="type"><a href="qmodelindex.html">QModelIndex</a></span> <span class="operator">&</span>index<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qvariant.html">QVariant</a></span> <span class="operator">&</span>value<span class="operator">,</span> <span class="type">int</span> <span class="comment">/* role */</span>)
{
<span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator"><</span> <span class="number">1</span> <span class="operator">|</span><span class="operator">|</span> index<span class="operator">.</span>column() <span class="operator">></span> <span class="number">2</span>)
<span class="keyword">return</span> <span class="keyword">false</span>;
<span class="type"><a href="qmodelindex.html">QModelIndex</a></span> primaryKeyIndex <span class="operator">=</span> <span class="type"><a href="qsqlquerymodel.html">QSqlQueryModel</a></span><span class="operator">::</span>index(index<span class="operator">.</span>row()<span class="operator">,</span> <span class="number">0</span>);
<span class="type">int</span> id <span class="operator">=</span> data(primaryKeyIndex)<span class="operator">.</span>toInt();
clear();
<span class="type">bool</span> ok;
<span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>) {
ok <span class="operator">=</span> setFirstName(id<span class="operator">,</span> value<span class="operator">.</span>toString());
} <span class="keyword">else</span> {
ok <span class="operator">=</span> setLastName(id<span class="operator">,</span> value<span class="operator">.</span>toString());
}
refresh();
<span class="keyword">return</span> ok;
}</pre>
<p>The setFirstName() helper function is defined as follows:</p>
<pre class="cpp"> <span class="type">bool</span> EditableSqlModel<span class="operator">::</span>setFirstName(<span class="type">int</span> personId<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>firstName)
{
<span class="type"><a href="qsqlquery.html">QSqlQuery</a></span> query;
query<span class="operator">.</span>prepare(<span class="string">"update person set firstname = ? where id = ?"</span>);
query<span class="operator">.</span>addBindValue(firstName);
query<span class="operator">.</span>addBindValue(personId);
<span class="keyword">return</span> query<span class="operator">.</span>exec();
}</pre>
<p>The setLastName() function is similar. See the <a href="sql-querymodel.html">Query Model</a> example for the complete source code.</p>
<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>
<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>
<p class="centerAlign"><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>
</div>
<!-- @@@sql-presenting.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="sql-model.html">Using the SQL Model Classes</a>
<a class="nextPage" href="sql-forms.html">Creating Data-Aware Forms</a>
</p>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|