Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > d5e62c01ae8d1e579463c6a871dd44bf > files > 4982

qtbase5-doc-5.12.6-2.mga7.noarch.rpm

<?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 4 - Editing and Removing 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 4 - Editing and Removing 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>
<li class="level2"><a href="#updating-the-user-interface">Updating the User Interface</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Part 4 - Editing and Removing Addresses</h1>
<span class="subtitle"></span>
<!-- $$$tutorials/addressbook/part4-brief -->
<p>Explains how to add edit and remove functionality.</p>
<!-- @@@tutorials/addressbook/part4 -->
<!-- $$$tutorials/addressbook/part4-description -->
<div class="descr"> <a name="details"></a>
<p>Now we look at ways to modify the contents of contacts stored in the address book.</p>
<p class="centerAlign"><img src="images/addressbook-tutorial-screenshot.png" alt="" /></p><p>We now have an address book that not only holds contacts in an organized manner, but also allows navigation. It would be convenient to include edit and remove functions so that a contact's details can be changed when needed. However, this requires a little improvement, in the form of enums. We defined two modes: <code>AddingMode</code> and <code>NavigationMode</code>, but they were not defined as enum values. Instead, we enabled and disabled the corresponding buttons manually, resulting in multiple lines of repeated code.</p>
<p>Here we define the <code>Mode</code> enum with three different values:</p>
<ul>
<li><code>NavigationMode</code>,</li>
<li><code>AddingMode</code>, and</li>
<li><code>EditingMode</code>.</li>
</ul>
<a name="defining-the-addressbook-class"></a>
<h2 id="defining-the-addressbook-class">Defining the AddressBook Class</h2>
<p>The <code>addressbook.h</code> file is updated to contain the <code>Mode</code> enum:</p>
<pre class="cpp">

      <span class="keyword">enum</span> Mode { NavigationMode<span class="operator">,</span> AddingMode<span class="operator">,</span> EditingMode };

</pre>
<p>We also add two new slots, <code>editContact()</code> and <code>removeContact()</code>, to our current list of public slots.</p>
<pre class="cpp">

      <span class="type">void</span> editContact();
      <span class="type">void</span> removeContact();

</pre>
<p>In order to switch between modes, we introduce the <code>updateInterface()</code> function to control the enabling and disabling of all <a href="qpushbutton.html">QPushButton</a> objects. We also add two new push buttons, <code>editButton</code> and <code>removeButton</code>, for the edit and remove functions mentioned earlier.</p>
<pre class="cpp">

      <span class="type">void</span> updateInterface(Mode mode);
      ...
      <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>editButton;
      <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>removeButton;
      ...
      Mode currentMode;

</pre>
<p>Lastly, we declare <code>currentMode</code> to keep track of the enum's current mode.</p>
<a name="implementing-the-addressbook-class"></a>
<h2 id="implementing-the-addressbook-class">Implementing the AddressBook Class</h2>
<p>We now implement the mode-changing features of the address book. The <code>editButton</code> and <code>removeButton</code> are instantiated and disabled by default. The address book starts with zero contacts in memory.</p>
<pre class="cpp">

      editButton <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;Edit&quot;</span>));
      editButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
      removeButton <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;Remove&quot;</span>));
      removeButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

</pre>
<p>These buttons are then connected to their respective slots, <code>editContact()</code> and <code>removeContact()</code>, and we add them to <code>buttonLayout1</code>.</p>
<pre class="cpp">

      connect(editButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(editContact()));
      connect(removeButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(removeContact()));
      ...
      buttonLayout1<span class="operator">-</span><span class="operator">&gt;</span>addWidget(editButton);
      buttonLayout1<span class="operator">-</span><span class="operator">&gt;</span>addWidget(removeButton);

</pre>
<p>The <code>editContact()</code> function stores the contact's old details in <code>oldName</code> and <code>oldAddress</code>, before switching the mode to <code>EditingMode</code>. In this mode, the <code>submitButton</code> and <code>cancelButton</code> are both enabled, hence, the user can change the contact's details and click either button.</p>
<pre class="cpp">

  <span class="type">void</span> AddressBook<span class="operator">::</span>editContact()
  {
      oldName <span class="operator">=</span> nameLine<span class="operator">-</span><span class="operator">&gt;</span>text();
      oldAddress <span class="operator">=</span> addressText<span class="operator">-</span><span class="operator">&gt;</span>toPlainText();

      updateInterface(EditingMode);
  }

</pre>
<p>The <code>submitContact()</code> function has been divided in two with an <code>if-else</code> statement. We check <code>currentMode</code> to see if it's in <code>AddingMode</code>. If it is, we proceed with our adding process.</p>
<pre class="cpp">

  <span class="type">void</span> AddressBook<span class="operator">::</span>submitContact()
  {
      ...
      <span class="keyword">if</span> (currentMode <span class="operator">=</span><span class="operator">=</span> AddingMode) {

          <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">&quot;Add Successful&quot;</span>)<span class="operator">,</span>
                  tr(<span class="string">&quot;\&quot;%1\&quot; has been added to your address book.&quot;</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">&quot;Add Unsuccessful&quot;</span>)<span class="operator">,</span>
                  tr(<span class="string">&quot;Sorry, \&quot;%1\&quot; is already in your address book.&quot;</span>)<span class="operator">.</span>arg(name));
          }

</pre>
<p>Otherwise, we check to see if <code>currentMode</code> is in <code>EditingMode</code>. If it is, we compare <code>oldName</code> with <code>name</code>. If the name has changed, we remove the old contact from <code>contacts</code> and insert the newly updated contact.</p>
<pre class="cpp">

      } <span class="keyword">else</span> <span class="keyword">if</span> (currentMode <span class="operator">=</span><span class="operator">=</span> EditingMode) {

          <span class="keyword">if</span> (oldName <span class="operator">!</span><span class="operator">=</span> name) {
              <span class="keyword">if</span> (<span class="operator">!</span>contacts<span class="operator">.</span>contains(name)) {
                  <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">&quot;Edit Successful&quot;</span>)<span class="operator">,</span>
                      tr(<span class="string">&quot;\&quot;%1\&quot; has been edited in your address book.&quot;</span>)<span class="operator">.</span>arg(oldName));
                  contacts<span class="operator">.</span>remove(oldName);
                  contacts<span class="operator">.</span>insert(name<span class="operator">,</span> address);
              } <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">&quot;Edit Unsuccessful&quot;</span>)<span class="operator">,</span>
                      tr(<span class="string">&quot;Sorry, \&quot;%1\&quot; is already in your address book.&quot;</span>)<span class="operator">.</span>arg(name));
              }
          } <span class="keyword">else</span> <span class="keyword">if</span> (oldAddress <span class="operator">!</span><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">&quot;Edit Successful&quot;</span>)<span class="operator">,</span>
                  tr(<span class="string">&quot;\&quot;%1\&quot; has been edited in your address book.&quot;</span>)<span class="operator">.</span>arg(name));
              contacts<span class="operator">[</span>name<span class="operator">]</span> <span class="operator">=</span> address;
          }
      }

      updateInterface(NavigationMode);
  }

</pre>
<p>If only the address has changed (i.e&#x2e;, <code>oldAddress</code> is not the same as <code>address</code>), we update the contact's address. Lastly, we set <code>currentMode</code> to <code>NavigationMode</code>. This is an important step as it re-enables all the disabled push buttons.</p>
<p>To remove a contact from the address book, we implement the <code>removeContact()</code> function. This function checks to see if the contact exists in <code>contacts</code>.</p>
<pre class="cpp">

  <span class="type">void</span> AddressBook<span class="operator">::</span>removeContact()
  {
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> name <span class="operator">=</span> nameLine<span class="operator">-</span><span class="operator">&gt;</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">&gt;</span>toPlainText();

      <span class="keyword">if</span> (contacts<span class="operator">.</span>contains(name)) {

          <span class="type">int</span> button <span class="operator">=</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>question(<span class="keyword">this</span><span class="operator">,</span>
              tr(<span class="string">&quot;Confirm Remove&quot;</span>)<span class="operator">,</span>
              tr(<span class="string">&quot;Are you sure you want to remove \&quot;%1\&quot;?&quot;</span>)<span class="operator">.</span>arg(name)<span class="operator">,</span>
              <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Yes <span class="operator">|</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>No);

          <span class="keyword">if</span> (button <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Yes) {

              previous();
              contacts<span class="operator">.</span>remove(name);

              <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">&quot;Remove Successful&quot;</span>)<span class="operator">,</span>
                  tr(<span class="string">&quot;\&quot;%1\&quot; has been removed from your address book.&quot;</span>)<span class="operator">.</span>arg(name));
          }
      }

      updateInterface(NavigationMode);
  }

</pre>
<p>If it does, we display a <a href="qmessagebox.html">QMessageBox</a>, to confirm the removal with the user. Once the user has confirmed, we call <code>previous()</code> to ensure that the user interface shows another contact, and we remove the contact using <a href="../qtcore/qmap.html">QMap</a>'s <a href="../qtcore/qmap.html#remove">remove()</a> function. As a courtesy, we display a <a href="qmessagebox.html">QMessageBox</a> to inform the user. Both the message boxes used in this function are shown below:</p>
<p class="centerAlign"><img src="images/addressbook-tutorial-part4-remove.png" alt="" /></p><a name="updating-the-user-interface"></a>
<h3 id="updating-the-user-interface">Updating the User Interface</h3>
<p>We mentioned the <code>updateInterface()</code> function earlier as a means to enable and disable the push buttons depending on the current mode. The function updates the current mode according to the <code>mode</code> argument passed to it, assigning it to <code>currentMode</code> before checking its value.</p>
<p>Each of the push buttons is then enabled or disabled, depending on the current mode. The code for <code>AddingMode</code> and <code>EditingMode</code> is shown below:</p>
<pre class="cpp">

  <span class="type">void</span> AddressBook<span class="operator">::</span>updateInterface(Mode mode)
  {
      currentMode <span class="operator">=</span> mode;

      <span class="keyword">switch</span> (currentMode) {

      <span class="keyword">case</span> AddingMode:
      <span class="keyword">case</span> EditingMode:

          nameLine<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">false</span>);
          nameLine<span class="operator">-</span><span class="operator">&gt;</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">&gt;</span>setReadOnly(<span class="keyword">false</span>);

          addButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
          editButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
          removeButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

          nextButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
          previousButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

          submitButton<span class="operator">-</span><span class="operator">&gt;</span>show();
          cancelButton<span class="operator">-</span><span class="operator">&gt;</span>show();
          <span class="keyword">break</span>;

</pre>
<p>For <code>NavigationMode</code>, however, we include conditions within the parameters of the <a href="qwidget.html#enabled-prop">QPushButton::setEnabled</a>() function. This is to ensure that <code>editButton</code> and <code>removeButton</code> are enabled when there is at least one contact in the address book; <code>nextButton</code> and <code>previousButton</code> are only enabled when there is more than one contact in the address book.</p>
<pre class="cpp">

      <span class="keyword">case</span> NavigationMode:

          <span class="keyword">if</span> (contacts<span class="operator">.</span>isEmpty()) {
              nameLine<span class="operator">-</span><span class="operator">&gt;</span>clear();
              addressText<span class="operator">-</span><span class="operator">&gt;</span>clear();
          }

          nameLine<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">true</span>);
          addressText<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">true</span>);
          addButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);

          <span class="type">int</span> number <span class="operator">=</span> contacts<span class="operator">.</span>size();
          editButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(number <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">1</span>);
          removeButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(number <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">1</span>);
          nextButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(number <span class="operator">&gt;</span> <span class="number">1</span>);
          previousButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(number <span class="operator">&gt;</span><span class="number">1</span> );

          submitButton<span class="operator">-</span><span class="operator">&gt;</span>hide();
          cancelButton<span class="operator">-</span><span class="operator">&gt;</span>hide();
          <span class="keyword">break</span>;
      }
  }

</pre>
<p>By setting the mode and updating the user interface in the same function, we avoid the possibility of the user interface getting out of sync with the internal state of the application.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-tutorials-addressbook-part4-addressbook-cpp.html">tutorials/addressbook/part4/addressbook.cpp</a></li>
<li><a href="qtwidgets-tutorials-addressbook-part4-addressbook-h.html">tutorials/addressbook/part4/addressbook.h</a></li>
<li><a href="qtwidgets-tutorials-addressbook-part4-main-cpp.html">tutorials/addressbook/part4/main.cpp</a></li>
<li><a href="qtwidgets-tutorials-addressbook-part4-part4-pro.html">tutorials/addressbook/part4/part4.pro</a></li>
</ul>
</div>
<!-- @@@tutorials/addressbook/part4 -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</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>