File: sql-sqlwidgetmapper.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (220 lines) | stat: -rw-r--r-- 23,807 bytes parent folder | download
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?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" />
<!-- sqlwidgetmapper.qdoc -->
  <title>Qt 4.8: SQL Widget Mapper Example</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><a href="all-examples.html">Examples</a></li>
<li>SQL Widget Mapper Example</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#window-class-definition">Window Class Definition</a></li>
<li class="level1"><a href="#window-class-implementation">Window Class Implementation</a></li>
<li class="level1"><a href="#summary-and-further-reading">Summary and Further Reading</a></li>
</ul>
</div>
<h1 class="title">SQL Widget Mapper Example</h1>
<span class="subtitle"></span>
<!-- $$$sql/sqlwidgetmapper-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="sql-sqlwidgetmapper-window-cpp.html">sql/sqlwidgetmapper/window.cpp</a></li>
<li><a href="sql-sqlwidgetmapper-window-h.html">sql/sqlwidgetmapper/window.h</a></li>
<li><a href="sql-sqlwidgetmapper-main-cpp.html">sql/sqlwidgetmapper/main.cpp</a></li>
<li><a href="sql-sqlwidgetmapper-sqlwidgetmapper-pro.html">sql/sqlwidgetmapper/sqlwidgetmapper.pro</a></li>
</ul>
<p>The SQL Widget Mapper example shows how to use a map information from a database to widgets on a form.<p class="centerAlign"><img src="images/sql-widget-mapper.png" alt="" /></p><p>In the <a href="itemviews-combowidgetmapper.html">Combo Widget Mapper Example</a>, we showed how to use a named mapping between a widget mapper and a <a href="qcombobox.html">QComboBox</a> widget with a special purpose model to relate values in the model to a list of choices.</p>
<p>Again, we create a <tt>Window</tt> class with an almost identical user interface, providing a combo box to allow their addresses to be classified as &quot;Home&quot;, &quot;Work&quot; or &quot;Other&quot;. However, instead of using a separate model to hold these address types, we use one database table to hold the example data and another to hold the address types. In this way, we store all the information in the same place.</p>
<a name="window-class-definition"></a>
<h2>Window Class Definition</h2>
<p>The class provides a constructor, a slot to keep the buttons up to date, and a private function to set up the model:</p>
<pre class="cpp"> <span class="keyword">class</span> Window : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     Window(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> updateButtons(<span class="type">int</span> row);

 <span class="keyword">private</span>:
     <span class="type">void</span> setupModel();

     <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>nameLabel;
     <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>addressLabel;
     <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>typeLabel;
     <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>nameEdit;
     <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>addressEdit;
     <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>typeComboBox;
     <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>nextButton;
     <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>previousButton;

     <span class="type"><a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a></span> <span class="operator">*</span>model;
     <span class="type"><a href="qitemselectionmodel.html">QItemSelectionModel</a></span> <span class="operator">*</span>selectionModel;
     <span class="type"><a href="qdatawidgetmapper.html">QDataWidgetMapper</a></span> <span class="operator">*</span>mapper;
     <span class="type">int</span> typeIndex;
 };</pre>
<p>In addition to the <a href="qdatawidgetmapper.html">QDataWidgetMapper</a> object and the controls used to make up the user interface, we use a <a href="qstandarditemmodel.html">QStandardItemModel</a> to hold our data and a <a href="qstringlistmodel.html">QStringListModel</a> to hold information about the types of address that can be applied to each person's data.</p>
<a name="window-class-implementation"></a>
<h2>Window Class Implementation</h2>
<p>The first act performed by the <tt>Window</tt> class constructor is to set up the model used to hold the example data. Since this is a key part of the example, we will look at this first.</p>
<p>The model is initialized in the window's <tt>setupModel()</tt> function. Here, we create a SQLite database containing a &quot;person&quot; table with primary key, name, address and type fields.</p>
<pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>setupModel()
 {
     <span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span> db <span class="operator">=</span> <span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">::</span>addDatabase(<span class="string">&quot;QSQLITE&quot;</span>);
     db<span class="operator">.</span>setDatabaseName(<span class="string">&quot;:memory:&quot;</span>);
     <span class="keyword">if</span> (<span class="operator">!</span>db<span class="operator">.</span>open()) {
         <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>critical(<span class="number">0</span><span class="operator">,</span> tr(<span class="string">&quot;Cannot open database&quot;</span>)<span class="operator">,</span>
             tr(<span class="string">&quot;Unable to establish a database connection.\n&quot;</span>
                <span class="string">&quot;This example needs SQLite support. Please read &quot;</span>
                <span class="string">&quot;the Qt SQL driver documentation for information how &quot;</span>
                <span class="string">&quot;to build it.&quot;</span>)<span class="operator">,</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Cancel);
         <span class="keyword">return</span>;
     }

     <span class="type"><a href="qsqlquery.html">QSqlQuery</a></span> query;
     query<span class="operator">.</span>exec(<span class="string">&quot;create table person (id int primary key, &quot;</span>
                <span class="string">&quot;name varchar(20), address varchar(200), typeid int)&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into person values(1, 'Alice', &quot;</span>
                <span class="string">&quot;'&lt;qt&gt;123 Main Street&lt;br/&gt;Market Town&lt;/qt&gt;', 101)&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into person values(2, 'Bob', &quot;</span>
                <span class="string">&quot;'&lt;qt&gt;PO Box 32&lt;br/&gt;Mail Handling Service&quot;</span>
                <span class="string">&quot;&lt;br/&gt;Service City&lt;/qt&gt;', 102)&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into person values(3, 'Carol', &quot;</span>
                <span class="string">&quot;'&lt;qt&gt;The Lighthouse&lt;br/&gt;Remote Island&lt;/qt&gt;', 103)&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into person values(4, 'Donald', &quot;</span>
                <span class="string">&quot;'&lt;qt&gt;47338 Park Avenue&lt;br/&gt;Big City&lt;/qt&gt;', 101)&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into person values(5, 'Emma', &quot;</span>
                <span class="string">&quot;'&lt;qt&gt;Research Station&lt;br/&gt;Base Camp&lt;br/&gt;&quot;</span>
                <span class="string">&quot;Big Mountain&lt;/qt&gt;', 103)&quot;</span>);</pre>
<p>On each row of the table, we insert default values for these fields, including values for the address types that correspond to the address types are stored in a separate table.</p>
<p class="centerAlign"><img src="images/widgetmapper-sql-mapping-table.png" alt="" /></p><p>We create an &quot;addresstype&quot; table containing the identifiers used in the &quot;person&quot; table and the corresponding strings:</p>
<pre class="cpp">     query<span class="operator">.</span>exec(<span class="string">&quot;create table addresstype (id int, description varchar(20))&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into addresstype values(101, 'Home')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into addresstype values(102, 'Work')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into addresstype values(103, 'Other')&quot;</span>);

     model <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a></span>(<span class="keyword">this</span>);
     model<span class="operator">-</span><span class="operator">&gt;</span>setTable(<span class="string">&quot;person&quot;</span>);
     model<span class="operator">-</span><span class="operator">&gt;</span>setEditStrategy(<span class="type"><a href="qsqltablemodel.html">QSqlTableModel</a></span><span class="operator">::</span>OnManualSubmit);

     typeIndex <span class="operator">=</span> model<span class="operator">-</span><span class="operator">&gt;</span>fieldIndex(<span class="string">&quot;typeid&quot;</span>);

     model<span class="operator">-</span><span class="operator">&gt;</span>setRelation(typeIndex<span class="operator">,</span>
            <span class="type"><a href="qsqlrelation.html">QSqlRelation</a></span>(<span class="string">&quot;addresstype&quot;</span><span class="operator">,</span> <span class="string">&quot;id&quot;</span><span class="operator">,</span> <span class="string">&quot;description&quot;</span>));
     model<span class="operator">-</span><span class="operator">&gt;</span>select();
 }</pre>
<p>The &quot;typeid&quot; field in the &quot;person&quot; table is related to the contents of the &quot;addresstype&quot; table via a relation in a <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a>. This kind of model performs all the necessary work to store the data in a database and also allows any relations to be used as models in their own right.</p>
<p>In this case, we have defined a relation for the &quot;typeid&quot; field in the &quot;person&quot; table that relates it to the &quot;id&quot; field in the &quot;addresstype&quot; table and which causes the contents of the &quot;description&quot; field to be used wherever the &quot;typeid&quot; is presented to the user. (See the <a href="qsqlrelationaltablemodel.html#setRelation">QSqlRelationalTableModel::setRelation</a>() documentation for details.)</p>
<p class="centerAlign"><img src="images/widgetmapper-sql-mapping.png" alt="" /></p><p>The constructor of the <tt>Window</tt> class can be explained in three parts. In the first part, we set up the model used to hold the data, then we set up the widgets used for the user interface:</p>
<pre class="cpp"> Window<span class="operator">::</span>Window(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
     : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
 {
     setupModel();

     nameLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Na&amp;me:&quot;</span>));
     nameEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>();
     addressLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;&amp;Address:&quot;</span>));
     addressEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>();
     typeLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;&amp;Type:&quot;</span>));
     typeComboBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>();
     nextButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;&amp;Next&quot;</span>));
     previousButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;&amp;Previous&quot;</span>));

     nameLabel<span class="operator">-</span><span class="operator">&gt;</span>setBuddy(nameEdit);
     addressLabel<span class="operator">-</span><span class="operator">&gt;</span>setBuddy(addressEdit);
     typeLabel<span class="operator">-</span><span class="operator">&gt;</span>setBuddy(typeComboBox);</pre>
<p>We obtain a model for the combo box from the main model, based on the relation we set up for the &quot;typeid&quot; field. The call to the combo box's <a href="qcombobox.html#modelColumn-prop">setModelColumn()</a> selects the field in the field in the model to display.</p>
<p>Note that this approach is similar to the one used in the <a href="itemviews-combowidgetmapper.html">Combo Widget Mapper Example</a> in that we set up a model for the combo box. However, in this case, we obtain a model based on a relation in the <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> rather than create a separate one.</p>
<p>Next, we set up the widget mapper, relating each input widget to a field in the model:</p>
<pre class="cpp">     <span class="type"><a href="qsqltablemodel.html">QSqlTableModel</a></span> <span class="operator">*</span>relModel <span class="operator">=</span> model<span class="operator">-</span><span class="operator">&gt;</span>relationModel(typeIndex);
     typeComboBox<span class="operator">-</span><span class="operator">&gt;</span>setModel(relModel);
     typeComboBox<span class="operator">-</span><span class="operator">&gt;</span>setModelColumn(relModel<span class="operator">-</span><span class="operator">&gt;</span>fieldIndex(<span class="string">&quot;description&quot;</span>));

     mapper <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdatawidgetmapper.html">QDataWidgetMapper</a></span>(<span class="keyword">this</span>);
     mapper<span class="operator">-</span><span class="operator">&gt;</span>setModel(model);
     mapper<span class="operator">-</span><span class="operator">&gt;</span>setItemDelegate(<span class="keyword">new</span> <span class="type"><a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a></span>(<span class="keyword">this</span>));
     mapper<span class="operator">-</span><span class="operator">&gt;</span>addMapping(nameEdit<span class="operator">,</span> model<span class="operator">-</span><span class="operator">&gt;</span>fieldIndex(<span class="string">&quot;name&quot;</span>));
     mapper<span class="operator">-</span><span class="operator">&gt;</span>addMapping(addressEdit<span class="operator">,</span> model<span class="operator">-</span><span class="operator">&gt;</span>fieldIndex(<span class="string">&quot;address&quot;</span>));
     mapper<span class="operator">-</span><span class="operator">&gt;</span>addMapping(typeComboBox<span class="operator">,</span> typeIndex);</pre>
<p>For the combo box, we already know the index of the field in the model from the <tt>setupModel()</tt> function. We use a <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a> as a proxy between the mapper and the input widgets to match up the &quot;typeid&quot; values in the model with those in the combo box's model and populate the combo box with descriptions rather than integer values.</p>
<p>As a result, the user is able to select an item from the combo box, and the associated value is written back to the model.</p>
<p>The rest of the constructor is very similar to that of the <a href="itemviews-simplewidgetmapper.html">Simple Widget Mapper Example</a>:</p>
<pre class="cpp">     connect(previousButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             mapper<span class="operator">,</span> SLOT(toPrevious()));
     connect(nextButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             mapper<span class="operator">,</span> SLOT(toNext()));
     connect(mapper<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(updateButtons(<span class="type">int</span>)));

     <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>();
     layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(nameLabel<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
     layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(nameEdit<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
     layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(previousButton<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
     layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(addressLabel<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
     layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(addressEdit<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">1</span>);
     layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(nextButton<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
     layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(typeLabel<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
     layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(typeComboBox<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
     setLayout(layout);

     setWindowTitle(tr(<span class="string">&quot;SQL Widget Mapper&quot;</span>));
     mapper<span class="operator">-</span><span class="operator">&gt;</span>toFirst();
 }</pre>
<p>We show the implementation of the <tt>updateButtons()</tt> slot for completeness:</p>
<pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>updateButtons(<span class="type">int</span> row)
 {
     previousButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(row <span class="operator">&gt;</span> <span class="number">0</span>);
     nextButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(row <span class="operator">&lt;</span> model<span class="operator">-</span><span class="operator">&gt;</span>rowCount() <span class="operator">-</span> <span class="number">1</span>);
 }</pre>
<a name="summary-and-further-reading"></a>
<h2>Summary and Further Reading</h2>
<p>The use of a separate model for the combo box and a special delegate for the widget mapper allows us to present a menu of choices to the user. Although the choices are stored in the same database as the user's data, they are held in a separate table. Using this approach, we can reconstructed complete records at a later time while using database features appropriately.</p>
<p>If SQL models are not being used, it is still possible to use more than one model to present choices to the user. This is covered by the <a href="itemviews-combowidgetmapper.html">Combo Widget Mapper Example</a>.</p>
</div>
<!-- @@@sql/sqlwidgetmapper -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</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>