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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
<?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" />
<!-- qt4-sql.qdoc -->
<title>Qt 4.8: The Qt 4 Database GUI Layer</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>The Qt 4 Database GUI Layer</li>
</ul>
</div>
</div>
<div class="content mainContent">
<link rel="prev" href="qt4-accessibility.html" />
<link rel="next" href="qt4-network.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qt4-accessibility.html">Cross-Platform Accessibility Support in Qt 4</a>
<a class="nextPage" href="qt4-network.html">The Network Module in Qt 4</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#general-overview">General Overview</a></li>
<li class="level1"><a href="#example-code">Example Code</a></li>
<li class="level1"><a href="#comparison-with-qt-3">Comparison with Qt 3</a></li>
</ul>
</div>
<h1 class="title">The Qt 4 Database GUI Layer</h1>
<span class="subtitle"></span>
<!-- $$$qt4-sql.html-description -->
<div class="descr"> <a name="details"></a>
<p>The GUI layer of the SQL module in Qt 4 has been entirely redesigned to work with <a href="qt4-interview.html">Interview</a> (Qt's new model/view classes). It consists of three model classes (<a href="qsqlquerymodel.html">QSqlQueryModel</a>, <a href="qsqltablemodel.html">QSqlTableModel</a>, and <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a>) that can be used with Qt's view classes, notably <a href="qtableview.html">QTableView</a>.</p>
<a name="general-overview"></a>
<h2>General Overview</h2>
<p>The Qt 4 SQL classes are divided into three layers:</p>
<ul>
<li>The database drivers</li>
<li>The core SQL classes</li>
<li>The GUI classes</li>
</ul>
<p>The database drivers and the core SQL classes are mostly the same as in Qt 3. The database item models are new with Qt 4; they inherit from <a href="qabstractitemmodel.html">QAbstractItemModel</a> and make it easy to present data from a database in a view class such as <a href="qlistview.html">QListView</a>, <a href="qtableview.html">QTableView</a>, and <a href="qtreeview.html">QTreeView</a>.</p>
<p>The philosophy behind the Qt 4 SQL module is that it should be possible to use database models for rendering and editing data just like any other item models. By changing the model at run-time, you can decide whether you want to store your data in an SQL database or in, say, an XML file. This generic approach has the additional benefit that you don't need to know anything about SQL to display and edit data.</p>
<p>The Qt 4 SQL module includes three item models:</p>
<ul>
<li><a href="qsqlquerymodel.html">QSqlQueryModel</a> is a read-only model based on an arbitrary SQL query.</li>
<li><a href="qsqltablemodel.html">QSqlTableModel</a> is a read-write model that works on a single table.</li>
<li><a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> is a <a href="qsqltablemodel.html">QSqlTableModel</a> subclass with foreign key support.</li>
</ul>
<p>Combined with Qt's view classes and Qt's default delegate class (<a href="qitemdelegate.html">QItemDelegate</a>), the models offer a very powerful mechanism for accessing databases. For finer control on the rendering of the fields, you can subclass one of the predefined models, or even <a href="qabstractitemdelegate.html">QAbstractItemDelegate</a> or <a href="qitemdelegate.html">QItemDelegate</a> if you need finer control.</p>
<p>You can also perform some customizations without subclassing. For example, you can sort a table using <a href="qsqltablemodel.html#sort">QSqlTableModel::sort</a>(), and you can initialize new rows by connecting to the <a href="qsqltablemodel.html#primeInsert">QSqlTableModel::primeInsert</a>() signal.</p>
<p>One nice feature supported by the read-write models is the possibility to perform changes to the item model without affecting the database until <a href="qsqltablemodel.html#submitAll">QSqlTableModel::submitAll</a>() is called. Changes can be dropped using <a href="qsqltablemodel.html#revertAll">QSqlTableModel::revertAll</a>().</p>
<p>The new classes perform advantageously compared to the SQL module's GUI layer in Qt 3. Speed and memory improvements in the tool classes (especially <a href="qvariant.html">QVariant</a>, <a href="qstring.html">QString</a>, and <a href="qmap.html">QMap</a>) and in the SQL drivers contribute to making Qt 4 database applications more snappy.</p>
<p>See the <a href="qtsql.html">QtSql</a> module overview for a more complete introduction to Qt's SQL classes.</p>
<a name="example-code"></a>
<h2>Example Code</h2>
<p>The simplest way to present data from a database is to simply combine a <a href="qsqlquerymodel.html">QSqlQueryModel</a> with a <a href="qtableview.html">QTableView</a>:</p>
<pre class="cpp"> <span class="type"><a href="qsqlquerymodel.html">QSqlQueryModel</a></span> model;
model<span class="operator">.</span>setQuery(<span class="string">"select * from person"</span>);
<span class="type"><a href="qtableview.html">QTableView</a></span> view;
view<span class="operator">.</span>setModel(<span class="operator">&</span>model);
view<span class="operator">.</span>show();</pre>
<p>To present the contents of a single table, we can use <a href="qsqltablemodel.html">QSqlTableModel</a> instead:</p>
<pre class="cpp"> <span class="type"><a href="qsqltablemodel.html">QSqlTableModel</a></span> model;
model<span class="operator">.</span>setTable(<span class="string">"person"</span>);
model<span class="operator">.</span>select();
<span class="type"><a href="qtableview.html">QTableView</a></span> view;
view<span class="operator">.</span>setModel(<span class="operator">&</span>model);
view<span class="operator">.</span>show();</pre>
<p>In practice, it's common that we need to customize the rendering of a field in the database. In that case, we can create our own model based on <a href="qsqlquerymodel.html">QSqlQueryModel</a>. The next code snippet shows a custom model that prepends '#' to the value in field 0 and converts the value in field 2 to uppercase:</p>
<pre class="cpp"> <span class="keyword">class</span> CustomSqlModel : <span class="keyword">public</span> <span class="type"><a href="qsqlquerymodel.html">QSqlQueryModel</a></span>
{
Q_OBJECT
<span class="keyword">public</span>:
CustomSqlModel(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="type"><a href="qvariant.html">QVariant</a></span> data(<span class="keyword">const</span> <span class="type"><a href="qmodelindex.html">QModelIndex</a></span> <span class="operator">&</span>item<span class="operator">,</span> <span class="type">int</span> role) <span class="keyword">const</span>;
};
<span class="type"><a href="qvariant.html">QVariant</a></span> CustomSqlModel<span class="operator">::</span>data(<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="type">int</span> role) <span class="keyword">const</span>
{
<span class="type"><a href="qvariant.html">QVariant</a></span> value <span class="operator">=</span> <span class="type"><a href="qsqlquerymodel.html">QSqlQueryModel</a></span><span class="operator">::</span>data(index<span class="operator">,</span> role);
<span class="keyword">if</span> (value<span class="operator">.</span>isValid() <span class="operator">&</span><span class="operator">&</span> role <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>DisplayRole) {
<span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>)
<span class="keyword">return</span> value<span class="operator">.</span>toString()<span class="operator">.</span>prepend(<span class="string">"#"</span>);
<span class="keyword">else</span> <span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span>)
<span class="keyword">return</span> value<span class="operator">.</span>toString()<span class="operator">.</span>toUpper();
}
<span class="keyword">if</span> (role <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>TextColorRole <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">1</span>)
<span class="keyword">return</span> <span class="type"><a href="qvariant.html">QVariant</a></span><span class="operator">::</span>fromValue(<span class="type"><a href="qcolor.html">QColor</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>blue));
<span class="keyword">return</span> value;
}</pre>
<p>It is also possible to subclass <a href="qsqlquerymodel.html">QSqlQueryModel</a> to add support for editing. This is done by reimplementing <a href="qabstractitemmodel.html#flags">QAbstractItemModel::flags</a>() to specify which database fields are editable and <a href="qabstractitemmodel.html#setData">QAbstractItemModel::setData</a>() to modify the database. Here's an example of a setData() reimplementation that changes the first or last name of a person:</p>
<pre class="cpp"> <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>It relies on helper functions called <tt>setFirstName()</tt> and <tt>setLastName()</tt>, which execute an <tt>update</tt>. Here's <tt>setFirstName()</tt>:</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>See Qt's <tt>examples/sql</tt> directory for more examples.</p>
<a name="comparison-with-qt-3"></a>
<h2>Comparison with Qt 3</h2>
<p>The core SQL database classes haven't changed so much since Qt 3. Here's a list of the main changes:</p>
<ul>
<li><a href="qsqldatabase.html">QSqlDatabase</a> is now value-based instead of pointer-based.</li>
<li><a href="porting4.html#qsqlfieldinfo">QSqlFieldInfo</a> and <a href="porting4.html#qsqlrecordinfo">QSqlRecordInfo</a> has been merged into <a href="qsqlfield.html">QSqlField</a> and <a href="qsqlrecord.html">QSqlRecord</a>.</li>
<li>The SQL query generation has been moved into the drivers. This makes it possible to use non-standard SQL extensions. It also opens the door to non-SQL databases.</li>
</ul>
<p>The GUI-related database classes have been entirely redesigned. The <a href="porting4.html#qsqlcursor">QSqlCursor</a> abstraction has been replaced with <a href="qsqlquerymodel.html">QSqlQueryModel</a> and <a href="qsqltablemodel.html">QSqlTableModel</a>; <a href="porting4.html#qsqleditorfactory">QSqlEditorFactory</a> is replaced by <a href="qabstractitemdelegate.html">QAbstractItemDelegate</a>; <a href="porting4.html#qdatatable">QDataTable</a> is replaced by <a href="qtableview.html">QTableView</a>. The old classes are part of the <a href="qt3support.html">Qt3Support</a> library to aid porting to Qt 4.</p>
</div>
<!-- @@@qt4-sql.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qt4-accessibility.html">Cross-Platform Accessibility Support in Qt 4</a>
<a class="nextPage" href="qt4-network.html">The Network Module in Qt 4</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>
|