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">"Na&me:"</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">"&Address:"</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">"A&ge (in years):"</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">"&Next"</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">"&Previous"</span>));
nameLabel<span class="operator">-</span><span class="operator">></span>setBuddy(nameEdit);
addressLabel<span class="operator">-</span><span class="operator">></span>setBuddy(addressEdit);
ageLabel<span class="operator">-</span><span class="operator">></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">></span>setModel(model);
mapper<span class="operator">-</span><span class="operator">></span>addMapping(nameEdit<span class="operator">,</span> <span class="number">0</span>);
mapper<span class="operator">-</span><span class="operator">></span>addMapping(addressEdit<span class="operator">,</span> <span class="number">1</span>);
mapper<span class="operator">-</span><span class="operator">></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">></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">></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">></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">></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">></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">></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">></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">></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">"Simple Widget Mapper"</span>));
mapper<span class="operator">-</span><span class="operator">></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"><</span><span class="operator"><</span> <span class="string">"Alice"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"Bob"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"Carol"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"Donald"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"Emma"</span>;
<span class="type"><a href="qstringlist.html">QStringList</a></span> addresses;
addresses <span class="operator"><</span><span class="operator"><</span> <span class="string">"<qt>123 Main Street<br/>Market Town</qt>"</span>
<span class="operator"><</span><span class="operator"><</span> <span class="string">"<qt>PO Box 32<br/>Mail Handling Service"</span>
<span class="string">"<br/>Service City</qt>"</span>
<span class="operator"><</span><span class="operator"><</span> <span class="string">"<qt>The Lighthouse<br/>Remote Island</qt>"</span>
<span class="operator"><</span><span class="operator"><</span> <span class="string">"<qt>47338 Park Avenue<br/>Big City</qt>"</span>
<span class="operator"><</span><span class="operator"><</span> <span class="string">"<qt>Research Station<br/>Base Camp<br/>Big Mountain</qt>"</span>;
<span class="type"><a href="qstringlist.html">QStringList</a></span> ages;
ages <span class="operator"><</span><span class="operator"><</span> <span class="string">"20"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"31"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"32"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"19"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"26"</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"><</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">></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">></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">></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">></span>setEnabled(row <span class="operator">></span> <span class="number">0</span>);
nextButton<span class="operator">-</span><span class="operator">></span>setEnabled(row <span class="operator"><</span> model<span class="operator">-</span><span class="operator">></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">©</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>
|