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

« back to all changes in this revision

Viewing changes to doc/html/qt4-sql.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/qt4-sql.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: The Qt 4 Database GUI Layer</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="qt4-accessibility.html" />
 
15
    <link rel="contents" href="qt4-intro.html" />
 
16
    <link rel="next" href="qt4-network.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="qt4-accessibility.html">Cross-Platform Accessibility Support in Qt 4</a>]
 
25
[<a href="qt4-intro.html">Home</a>]
 
26
[Next: <a href="qt4-network.html">The Network Module in Qt 4</a>]
 
27
</p>
 
28
<h1 align="center">The Qt 4 Database GUI Layer</h1>
 
29
<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>
 
30
<a name="general-overview"></a>
 
31
<h2>General Overview</h2>
 
32
<p>The Qt 4 SQL classes are divided into three layers:</p>
 
33
<ul>
 
34
<li>The database drivers</li>
 
35
<li>The core SQL classes</li>
 
36
<li>The GUI classes</li>
 
37
</ul>
 
38
<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>
 
39
<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>
 
40
<p>The Qt 4 SQL module includes three item models:</p>
 
41
<ul>
 
42
<li><a href="qsqlquerymodel.html">QSqlQueryModel</a> is a read-only model based on an arbitrary SQL query.</li>
 
43
<li><a href="qsqltablemodel.html">QSqlTableModel</a> is a read-write model that works on a single table.</li>
 
44
<li><a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> is a <a href="qsqltablemodel.html">QSqlTableModel</a> subclass with foreign key support.</li>
 
45
</ul>
 
46
<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>
 
47
<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>
 
48
<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>
 
49
<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>
 
50
<p>See the <a href="sql.html">SQL Model</a> for a more complete introduction to Qt's SQL classes.</p>
 
51
<a name="example-code"></a>
 
52
<h2>Example Code</h2>
 
53
<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>
 
54
<pre>&nbsp;   QSqlQueryModel model;
 
55
    model.setQuery(&quot;select * from person&quot;);
 
56
 
 
57
    QTableView view;
 
58
    view.setModel(&amp;model);
 
59
    view.show();</pre>
 
60
<p>To present the contents of a single table, we can use <a href="qsqltablemodel.html">QSqlTableModel</a> instead:</p>
 
61
<pre>&nbsp;   QSqlTableModel model;
 
62
    model.setTable(&quot;person&quot;);
 
63
    model.select();
 
64
 
 
65
    QTableView view;
 
66
    view.setModel(&amp;model);
 
67
    view.show();</pre>
 
68
<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>
 
69
<pre>&nbsp;   class CustomSqlModel : public QSqlQueryModel
 
70
    {
 
71
        Q_OBJECT
 
72
 
 
73
    public:
 
74
        CustomSqlModel(QObject *parent = 0);
 
75
 
 
76
        QVariant data(const QModelIndex &amp;item, int role) const;
 
77
    };
 
78
 
 
79
    QVariant CustomSqlModel::data(const QModelIndex &amp;index, int role) const
 
80
    {
 
81
        QVariant value = QSqlQueryModel::data(index, role);
 
82
        if (value.isValid() &amp;&amp; role == Qt::DisplayRole) {
 
83
            if (index.column() == 0)
 
84
                return value.toString().prepend(&quot;#&quot;);
 
85
            else if (index.column() == 2)
 
86
                return value.toString().toUpper();
 
87
        }
 
88
        if (role == Qt::TextColorRole &amp;&amp; index.column() == 1)
 
89
            return qVariantFromValue(QColor(Qt::blue));
 
90
        return value;
 
91
    }</pre>
 
92
<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>
 
93
<pre>&nbsp;   bool EditableSqlModel::setData(const QModelIndex &amp;index, const QVariant &amp;value, int /* role */)
 
94
    {
 
95
        if (index.column() &lt; 1 || index.column() &gt; 2)
 
96
            return false;
 
97
 
 
98
        QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
 
99
        int id = data(primaryKeyIndex).toInt();
 
100
 
 
101
        clear();
 
102
 
 
103
        bool ok;
 
104
        if (index.column() == 1) {
 
105
            ok = setFirstName(id, value.toString());
 
106
        } else {
 
107
            ok = setLastName(id, value.toString());
 
108
        }
 
109
        refresh();
 
110
        return ok;
 
111
    }</pre>
 
112
<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>
 
113
<pre>&nbsp;   bool EditableSqlModel::setFirstName(int personId, const QString &amp;firstName)
 
114
    {
 
115
        QSqlQuery query;
 
116
        query.prepare(&quot;update person set firstname = ? where id = ?&quot;);
 
117
        query.addBindValue(firstName);
 
118
        query.addBindValue(personId);
 
119
        return query.exec();
 
120
    }</pre>
 
121
<p>See Qt's <tt>examples/sql</tt> directory for more examples.</p>
 
122
<a name="comparison-with-qt-3"></a>
 
123
<h2>Comparison with Qt 3</h2>
 
124
<p>The core SQL database classes haven't changed so much since Qt 3. Here's a list of the main changes:</p>
 
125
<ul>
 
126
<li><a href="qsqldatabase.html">QSqlDatabase</a> is now value-based instead of pointer-based.</li>
 
127
<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>
 
128
<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>
 
129
</ul>
 
130
<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">Qt3Support</a> library to aid porting to Qt 4.</p>
 
131
<p>
 
132
[Previous: <a href="qt4-accessibility.html">Cross-Platform Accessibility Support in Qt 4</a>]
 
133
[<a href="qt4-intro.html">Home</a>]
 
134
[Next: <a href="qt4-network.html">The Network Module in Qt 4</a>]
 
135
</p>
 
136
<p /><address><hr /><div align="center">
 
137
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
138
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
139
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
140
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
141
</tr></table></div></address></body>
 
142
</html>