File: itemviews-simplewidgetmapper.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 (193 lines) | stat: -rw-r--r-- 20,955 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
<?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" />
<!-- simplewidgetmapper.qdoc -->
  <title>Qt 4.8: Simple 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>Simple 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="#more-complex-mappings">More Complex Mappings</a></li>
</ul>
</div>
<h1 class="title">Simple Widget Mapper Example</h1>
<span class="subtitle"></span>
<!-- $$$itemviews/simplewidgetmapper-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="itemviews-simplewidgetmapper-window-cpp.html">itemviews/simplewidgetmapper/window.cpp</a></li>
<li><a href="itemviews-simplewidgetmapper-window-h.html">itemviews/simplewidgetmapper/window.h</a></li>
<li><a href="itemviews-simplewidgetmapper-main-cpp.html">itemviews/simplewidgetmapper/main.cpp</a></li>
<li><a href="itemviews-simplewidgetmapper-simplewidgetmapper-pro.html">itemviews/simplewidgetmapper/simplewidgetmapper.pro</a></li>
</ul>
<p>The Simple Widget Mapper example shows how to use a widget mapper to display data from a model in a collection of widgets.<p class="centerAlign"><img src="images/simplewidgetmapper-example.png" alt="" /></p><p>The <a href="qdatawidgetmapper.html">QDataWidgetMapper</a> class allows information obtained from a <a href="model-view-programming.html#model-classes">model</a> to be viewed and edited in a collection of widgets instead of in an <a href="model-view-programming.html#view-classes">item view</a>. Any model derived from <a href="qabstractitemmodel.html">QAbstractItemModel</a> can be used as the source of data and almost any input widget can be used to display it.</p>
<p>The example itself is very simple: we create <tt>Window</tt>, a <a href="qwidget.html">QWidget</a> subclass that we use to hold the widgets used to present the data, and show it. The <tt>Window</tt> class will provide buttons that the user can click to show different records from the model.</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>ageLabel;
     <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="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>ageSpinBox;
     <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="qstandarditemmodel.html">QStandardItemModel</a></span> <span class="operator">*</span>model;
     <span class="type"><a href="qdatawidgetmapper.html">QDataWidgetMapper</a></span> <span class="operator">*</span>mapper;
 };</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. We could use a custom model, but this standard implementation is sufficient for our purposes.</p>
<a name="window-class-implementation"></a>
<h2>Window Class Implementation</h2>
<p>The constructor of the <tt>Window</tt> class can be explained in three parts. In the first part, 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>();
     ageLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;A&amp;ge (in years):&quot;</span>));
     ageSpinBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qspinbox.html">QSpinBox</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);
     ageLabel<span class="operator">-</span><span class="operator">&gt;</span>setBuddy(ageSpinBox);</pre>
<p>We also set up the buddy relationships between various labels and the corresponding input widgets.</p>
<p>Next, we set up the widget mapper, relating each input widget to a column in the model specified by the call to <a href="qdatawidgetmapper.html#setModel">setModel()</a>:</p>
<pre class="cpp">     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>addMapping(nameEdit<span class="operator">,</span> <span class="number">0</span>);
     mapper<span class="operator">-</span><span class="operator">&gt;</span>addMapping(addressEdit<span class="operator">,</span> <span class="number">1</span>);
     mapper<span class="operator">-</span><span class="operator">&gt;</span>addMapping(ageSpinBox<span class="operator">,</span> <span class="number">2</span>);

     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>)));</pre>
<p>We also connect the mapper to the <b>Next</b> and <b>Previous</b> buttons via its <a href="qdatawidgetmapper.html#toNext">toNext()</a> and <a href="qdatawidgetmapper.html#toPrevious">toPrevious()</a> slots. The mapper's <a href="qdatawidgetmapper.html#currentIndexChanged">currentIndexChanged()</a> signal is connected to the <tt>updateButtons()</tt> slot in the window which we'll show later.</p>
<p>In the final part of the constructor, we set up the layout, placing each of the widgets in a grid (we could also use a <a href="qformlayout.html">QFormLayout</a> for this):</p>
<pre class="cpp">     <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(ageLabel<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(ageSpinBox<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;Simple Widget Mapper&quot;</span>));
     mapper<span class="operator">-</span><span class="operator">&gt;</span>toFirst();
 }</pre>
<p>Lastly, we set the window title and initialize the mapper by setting it to refer to the first row in the model.</p>
<p>The model is initialized in the window's <tt>setupModel()</tt> function. Here, we create a standard model with 5 rows and 3 columns, and we insert some sample names, addresses and ages into each row:</p>
<pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>setupModel()
 {
     model <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstandarditemmodel.html">QStandardItemModel</a></span>(<span class="number">5</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="keyword">this</span>);

     <span class="type"><a href="qstringlist.html">QStringList</a></span> names;
     names <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Alice&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Bob&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Carol&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Donald&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Emma&quot;</span>;

     <span class="type"><a href="qstringlist.html">QStringList</a></span> addresses;
     addresses <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;&lt;qt&gt;123 Main Street&lt;br/&gt;Market Town&lt;/qt&gt;&quot;</span>
               <span class="operator">&lt;</span><span class="operator">&lt;</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;&quot;</span>
               <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;&lt;qt&gt;The Lighthouse&lt;br/&gt;Remote Island&lt;/qt&gt;&quot;</span>
               <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;&lt;qt&gt;47338 Park Avenue&lt;br/&gt;Big City&lt;/qt&gt;&quot;</span>
               <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;&lt;qt&gt;Research Station&lt;br/&gt;Base Camp&lt;br/&gt;Big Mountain&lt;/qt&gt;&quot;</span>;

     <span class="type"><a href="qstringlist.html">QStringList</a></span> ages;
     ages <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;20&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;31&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;32&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;19&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;26&quot;</span>;

     <span class="keyword">for</span> (<span class="type">int</span> row <span class="operator">=</span> <span class="number">0</span>; row <span class="operator">&lt;</span> <span class="number">5</span>; <span class="operator">+</span><span class="operator">+</span>row) {
       <span class="type"><a href="qstandarditem.html">QStandardItem</a></span> <span class="operator">*</span>item <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstandarditem.html">QStandardItem</a></span>(names<span class="operator">[</span>row<span class="operator">]</span>);
       model<span class="operator">-</span><span class="operator">&gt;</span>setItem(row<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> item);
       item <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstandarditem.html">QStandardItem</a></span>(addresses<span class="operator">[</span>row<span class="operator">]</span>);
       model<span class="operator">-</span><span class="operator">&gt;</span>setItem(row<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> item);
       item <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstandarditem.html">QStandardItem</a></span>(ages<span class="operator">[</span>row<span class="operator">]</span>);
       model<span class="operator">-</span><span class="operator">&gt;</span>setItem(row<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> item);
     }
 }</pre>
<p>As a result, each row can be treated like a record in a database, and the widget mapper will read the data from each row, using the column numbers specified earlier to access the correct data for each widget. This is shown in the following diagram:</p>
<p class="centerAlign"><img src="images/widgetmapper-simple-mapping.png" alt="" /></p><p>Since the user can navigate using the buttons in the user interface, the example is fully-functional at this point, but to make it a bit more user-friendly, we implement the <tt>updateButtons()</tt> slot to show when the user is viewing the first or last records:</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>
<p>If the mapper is referring to the first row in the model, the <b>Previous</b> button is disabled. Similarly, the <b>Next</b> button is disabled if the mapper reaches the last row in the model.</p>
<a name="more-complex-mappings"></a>
<h2>More Complex Mappings</h2>
<p>The <a href="qdatawidgetmapper.html">QDataWidgetMapper</a> class makes it easy to relate information from a model to widgets in a user interface. However, it is sometimes necessary to use input widgets which offer choices to the user, such as <a href="qcombobox.html">QComboBox</a>, in conjunction with a widget mapper.</p>
<p>In these situations, although the mapping to input widgets remains simple, more work needs to be done to expose additional data to the widget mapper. This is covered by the <a href="itemviews-combowidgetmapper.html">Combo Widget Mapper</a> and <a href="sql-sqlwidgetmapper.html">SQL Widget Mapper</a> examples.</p>
</div>
<!-- @@@itemviews/simplewidgetmapper -->
  <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>