<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- addressbook-tutorial.qdoc --> <title>Part 2 - Adding Addresses | Qt Widgets 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td >Part 2 - Adding Addresses</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#defining-the-addressbook-class">Defining the AddressBook Class</a></li> <li class="level1"><a href="#implementing-the-addressbook-class">Implementing the AddressBook Class</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Part 2 - Adding Addresses</h1> <span class="subtitle"></span> <!-- $$$tutorials/addressbook/part2-brief --> <p>Describes the code for inserting records in the Address Book Example.</p> <!-- @@@tutorials/addressbook/part2 --> <!-- $$$tutorials/addressbook/part2-description --> <div class="descr"> <a name="details"></a> <p>The next step in creating the address book is to implement some user interactions.</p> <p class="centerAlign"><img src="images/addressbook-tutorial-part2-add-contact.png" alt="" /></p><p>We will provide a push button that the user can click to add a new contact. Also, some form of data structure is needed to store these contacts in an organized way.</p> <a name="defining-the-addressbook-class"></a> <h2 id="defining-the-addressbook-class">Defining the AddressBook Class</h2> <p>Now that we have the labels and input fields set up, we add push buttons to complete the process of adding a contact. This means that our <code>addressbook.h</code> file now has three <a href="qpushbutton.html">QPushButton</a> objects declared and three corresponding public slots.</p> <pre class="cpp"> <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> addContact(); <span class="type">void</span> submitContact(); <span class="type">void</span> cancel(); </pre> <p>A slot is a function that responds to a particular signal. We will discuss this concept in further detail when implementing the <code>AddressBook</code> class. However, for an overview of Qt's signals and slots concept, you can refer to the <a href="../qtcore/signalsandslots.html">Signals and Slots</a> document.</p> <p>Three <a href="qpushbutton.html">QPushButton</a> objects (<code>addButton</code>, <code>submitButton</code>, and <code>cancelButton</code>) are now included in our private variable declarations, along with <code>nameLine</code> and <code>addressText</code>.</p> <pre class="cpp"> <span class="keyword">private</span>: <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>addButton; <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>submitButton; <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>cancelButton; <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>nameLine; <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>addressText; </pre> <p>We need a container to store our address book contacts, so that we can traverse and display them. A <a href="../qtcore/qmap.html">QMap</a> object, <code>contacts</code>, is used for this purpose as it holds a key-value pair: the contact's name as the <i>key</i>, and the contact's address as the <i>value</i>.</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qmap.html">QMap</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">,</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> contacts; <span class="type"><a href="../qtcore/qstring.html">QString</a></span> oldName; <span class="type"><a href="../qtcore/qstring.html">QString</a></span> oldAddress; }; </pre> <p>We also declare two private <a href="../qtcore/qstring.html">QString</a> objects, <code>oldName</code> and <code>oldAddress</code>. These objects are needed to hold the name and address of the contact that was last displayed, before the user clicked <b>Add</b>. So, when the user clicks <b>Cancel</b>, we can revert to displaying the details of the last contact.</p> <a name="implementing-the-addressbook-class"></a> <h2 id="implementing-the-addressbook-class">Implementing the AddressBook Class</h2> <p>Within the constructor of <code>AddressBook</code>, we set the <code>nameLine</code> and <code>addressText</code> to read-only, so that we can only display but not edit existing contact details.</p> <pre class="cpp"> ... nameLine<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">true</span>); ... addressText<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">true</span>); </pre> <p>Then, we instantiate our push buttons: <code>addButton</code>, <code>submitButton</code>, and <code>cancelButton</code>.</p> <pre class="cpp"> addButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"&Add"</span>)); addButton<span class="operator">-</span><span class="operator">></span>show(); submitButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"&Submit"</span>)); submitButton<span class="operator">-</span><span class="operator">></span>hide(); cancelButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"&Cancel"</span>)); cancelButton<span class="operator">-</span><span class="operator">></span>hide(); </pre> <p>The <code>addButton</code> is displayed by invoking the <a href="qwidget.html#show">show()</a> function, while the <code>submitButton</code> and <code>cancelButton</code> are hidden by invoking <a href="qwidget.html#hide">hide()</a>. These two push buttons will only be displayed when the user clicks <b>Add</b> and this is handled by the <code>addContact()</code> function discussed below.</p> <pre class="cpp"> connect(addButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(addContact())); connect(submitButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(submitContact())); connect(cancelButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(cancel())); </pre> <p>We connect the push buttons' <a href="qabstractbutton.html#clicked">clicked()</a> signal to their respective slots. The figure below illustrates this.</p> <p class="centerAlign"><img src="images/addressbook-tutorial-part2-signals-and-slots.png" alt="" /></p><p>Next, we arrange our push buttons neatly to the right of our address book widget, using a <a href="qvboxlayout.html">QVBoxLayout</a> to line them up vertically.</p> <pre class="cpp"> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>buttonLayout1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>; buttonLayout1<span class="operator">-</span><span class="operator">></span>addWidget(addButton<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignTop); buttonLayout1<span class="operator">-</span><span class="operator">></span>addWidget(submitButton); buttonLayout1<span class="operator">-</span><span class="operator">></span>addWidget(cancelButton); buttonLayout1<span class="operator">-</span><span class="operator">></span>addStretch(); </pre> <p>The <a href="qboxlayout.html#addStretch">addStretch()</a> function is used to ensure the push buttons are not evenly spaced, but arranged closer to the top of the widget. The figure below shows the difference between using <a href="qboxlayout.html#addStretch">addStretch()</a> and not using it.</p> <p class="centerAlign"><img src="images/addressbook-tutorial-part2-stretch-effects.png" alt="" /></p><p>We then add <code>buttonLayout1</code> to <code>mainLayout</code>, using <a href="qgridlayout.html#addLayout">addLayout()</a>. This gives us nested layouts as <code>buttonLayout1</code> is now a child of <code>mainLayout</code>.</p> <pre class="cpp"> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>mainLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>; mainLayout<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>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(nameLine<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span>); mainLayout<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="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignTop); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(addressText<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addLayout(buttonLayout1<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>); </pre> <p>Our layout coordinates now look like this:</p> <p class="centerAlign"><img src="images/addressbook-tutorial-part2-labeled-layout.png" alt="" /></p><p>In the <code>addContact()</code> function, we store the last displayed contact details in <code>oldName</code> and <code>oldAddress</code>. Then we clear these input fields and turn off the read-only mode. The focus is set on <code>nameLine</code> and we display <code>submitButton</code> and <code>cancelButton</code>.</p> <pre class="cpp"> <span class="type">void</span> AddressBook<span class="operator">::</span>addContact() { oldName <span class="operator">=</span> nameLine<span class="operator">-</span><span class="operator">></span>text(); oldAddress <span class="operator">=</span> addressText<span class="operator">-</span><span class="operator">></span>toPlainText(); nameLine<span class="operator">-</span><span class="operator">></span>clear(); addressText<span class="operator">-</span><span class="operator">></span>clear(); nameLine<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">false</span>); nameLine<span class="operator">-</span><span class="operator">></span>setFocus(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>OtherFocusReason); addressText<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">false</span>); addButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">false</span>); submitButton<span class="operator">-</span><span class="operator">></span>show(); cancelButton<span class="operator">-</span><span class="operator">></span>show(); } </pre> <p>The <code>submitContact()</code> function can be divided into three parts:</p> <ol class="1" type="1"><li>We extract the contact's details from <code>nameLine</code> and <code>addressText</code> and store them in <a href="../qtcore/qstring.html">QString</a> objects. We also validate to make sure that the user did not click <b>Submit</b> with empty input fields; otherwise, a <a href="qmessagebox.html">QMessageBox</a> is displayed to remind the user for a name and address.<pre class="cpp"> <span class="type">void</span> AddressBook<span class="operator">::</span>submitContact() { <span class="type"><a href="../qtcore/qstring.html">QString</a></span> name <span class="operator">=</span> nameLine<span class="operator">-</span><span class="operator">></span>text(); <span class="type"><a href="../qtcore/qstring.html">QString</a></span> address <span class="operator">=</span> addressText<span class="operator">-</span><span class="operator">></span>toPlainText(); <span class="keyword">if</span> (name<span class="operator">.</span>isEmpty() <span class="operator">|</span><span class="operator">|</span> address<span class="operator">.</span>isEmpty()) { <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">"Empty Field"</span>)<span class="operator">,</span> tr(<span class="string">"Please enter a name and address."</span>)); <span class="keyword">return</span>; } </pre> </li> <li>We then proceed to check if the contact already exists. If it does not exist, we add the contact to <code>contacts</code> and we display a <a href="qmessagebox.html">QMessageBox</a> to inform the user that the contact has been added.<pre class="cpp"> <span class="keyword">if</span> (<span class="operator">!</span>contacts<span class="operator">.</span>contains(name)) { contacts<span class="operator">.</span>insert(name<span class="operator">,</span> address); <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">"Add Successful"</span>)<span class="operator">,</span> tr(<span class="string">"\"%1\" has been added to your address book."</span>)<span class="operator">.</span>arg(name)); } <span class="keyword">else</span> { <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">"Add Unsuccessful"</span>)<span class="operator">,</span> tr(<span class="string">"Sorry, \"%1\" is already in your address book."</span>)<span class="operator">.</span>arg(name)); <span class="keyword">return</span>; } </pre> <p>If the contact already exists, again, we display a <a href="qmessagebox.html">QMessageBox</a> to inform the user about this, preventing the user from adding duplicate contacts. Our <code>contacts</code> object is based on key-value pairs of name and address, hence, we want to ensure that <i>key</i> is unique.</p> </li> <li>Once we have handled both cases mentioned above, we restore the push buttons to their normal state with the following code:<pre class="cpp"> <span class="keyword">if</span> (contacts<span class="operator">.</span>isEmpty()) { nameLine<span class="operator">-</span><span class="operator">></span>clear(); addressText<span class="operator">-</span><span class="operator">></span>clear(); } nameLine<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">true</span>); addressText<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">true</span>); addButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">true</span>); submitButton<span class="operator">-</span><span class="operator">></span>hide(); cancelButton<span class="operator">-</span><span class="operator">></span>hide(); } </pre> </li> </ol> <p>The screenshot below shows the <a href="qmessagebox.html">QMessageBox</a> object we use to display information messages to the user.</p> <p class="centerAlign"><img src="images/addressbook-tutorial-part2-add-successful.png" alt="" /></p><p>The <code>cancel()</code> function restores the last displayed contact details and enables <code>addButton</code>, as well as hides <code>submitButton</code> and <code>cancelButton</code>.</p> <pre class="cpp"> <span class="type">void</span> AddressBook<span class="operator">::</span>cancel() { nameLine<span class="operator">-</span><span class="operator">></span>setText(oldName); nameLine<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">true</span>); addressText<span class="operator">-</span><span class="operator">></span>setText(oldAddress); addressText<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">true</span>); addButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">true</span>); submitButton<span class="operator">-</span><span class="operator">></span>hide(); cancelButton<span class="operator">-</span><span class="operator">></span>hide(); } </pre> <p>The general idea behind adding a contact is to give the user the flexibility to click <b>Submit</b> or <b>Cancel</b> at any time. The flowchart below further explains this concept:</p> <p class="centerAlign"><img src="images/addressbook-tutorial-part2-add-flowchart.png" alt="" /></p><p>Files:</p> <ul> <li><a href="qtwidgets-tutorials-addressbook-part2-addressbook-cpp.html">tutorials/addressbook/part2/addressbook.cpp</a></li> <li><a href="qtwidgets-tutorials-addressbook-part2-addressbook-h.html">tutorials/addressbook/part2/addressbook.h</a></li> <li><a href="qtwidgets-tutorials-addressbook-part2-main-cpp.html">tutorials/addressbook/part2/main.cpp</a></li> <li><a href="qtwidgets-tutorials-addressbook-part2-part2-pro.html">tutorials/addressbook/part2/part2.pro</a></li> </ul> </div> <!-- @@@tutorials/addressbook/part2 --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> 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.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>