<?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 5 - Adding a Find Function | 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 5 - Adding a Find Function</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-finddialog-class">Defining the FindDialog Class</a></li> <li class="level1"><a href="#implementing-the-finddialog-class">Implementing the FindDialog Class</a></li> <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 5 - Adding a Find Function</h1> <span class="subtitle"></span> <!-- $$$tutorials/addressbook/part5-brief --> <p>Describes how to add a find function.</p> <!-- @@@tutorials/addressbook/part5 --> <!-- $$$tutorials/addressbook/part5-description --> <div class="descr"> <a name="details"></a> <p>Here we look at ways to locate contacts and addresses in the address book.</p> <p class="centerAlign"><img src="images/addressbook-tutorial-part5-screenshot.png" alt="" /></p><p>As we add contacts to our address book, it becomes tedious to navigate the list with the <i>Next</i> and <i>Previous</i> buttons. A <i>Find</i> function would be more efficient. The screenshot above shows the <i>Find</i> button and its position on the panel of buttons.</p> <p>When the user clicks on the <i>Find</i> button, it is useful to display a dialog that prompts for a contact's name. Qt provides <a href="qdialog.html">QDialog</a>, which we subclass here to implement a <code>FindDialog</code> class.</p> <a name="defining-the-finddialog-class"></a> <h2 id="defining-the-finddialog-class">Defining the FindDialog Class</h2> <p class="centerAlign"><img src="images/addressbook-tutorial-part5-finddialog.png" alt="" /></p><p>In order to subclass <a href="qdialog.html">QDialog</a>, we first include the header for <a href="qdialog.html">QDialog</a> in the <code>finddialog.h</code> file. Also, we use forward declaration to declare <a href="qlineedit.html">QLineEdit</a> and <a href="qpushbutton.html">QPushButton</a> since we will be using those widgets in our dialog class.</p> <p>As in our <code>AddressBook</code> class, the <code>FindDialog</code> class includes the <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> macro and its constructor is defined to accept a parent <a href="qwidget.html">QWidget</a>, even though the dialog will be opened as a separate window.</p> <pre class="cpp"> <span class="preprocessor">#include <QDialog></span> <span class="keyword">class</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>; <span class="keyword">class</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>; <span class="keyword">class</span> FindDialog : <span class="keyword">public</span> <span class="type"><a href="qdialog.html">QDialog</a></span> { Q_OBJECT <span class="keyword">public</span>: FindDialog(<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="type"><a href="../qtcore/qstring.html">QString</a></span> getFindText(); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> findClicked(); <span class="keyword">private</span>: <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>findButton; <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>lineEdit; <span class="type"><a href="../qtcore/qstring.html">QString</a></span> findText; }; </pre> <p>We define a public function, <code>getFindText()</code>, to be used by classes that instantiate <code>FindDialog</code>. This function allows these classes to obtain the search string entered by the user. A public slot, <code>findClicked()</code>, is also defined to handle the search string when the user clicks the <b>Find</b> button.</p> <p>Lastly, we define the private variables, <code>findButton</code>, <code>lineEdit</code> and <code>findText</code>, corresponding to the <b>Find</b> button, the line edit into which the user types the search string, and an internal string used to store the search string for later use.</p> <a name="implementing-the-finddialog-class"></a> <h2 id="implementing-the-finddialog-class">Implementing the FindDialog Class</h2> <p>Within the constructor of <code>FindDialog</code>, we set up the private variables, <code>lineEdit</code>, <code>findButton</code> and <code>findText</code>. We use a <a href="qhboxlayout.html">QHBoxLayout</a> to position the widgets.</p> <pre class="cpp"> FindDialog<span class="operator">::</span>FindDialog(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="qdialog.html">QDialog</a></span>(parent) { <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>findLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"Enter the name of a contact:"</span>)); lineEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>; findButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"&Find"</span>)); findText <span class="operator">=</span> <span class="string">""</span>; <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span>; layout<span class="operator">-</span><span class="operator">></span>addWidget(findLabel); layout<span class="operator">-</span><span class="operator">></span>addWidget(lineEdit); layout<span class="operator">-</span><span class="operator">></span>addWidget(findButton); setLayout(layout); setWindowTitle(tr(<span class="string">"Find a Contact"</span>)); connect(findButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(findClicked())); connect(findButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(accept())); } </pre> <p>We set the layout and window title, as well as connect the signals to their respective slots. Notice that <code>findButton</code>'s <a href="qabstractbutton.html#clicked">clicked()</a> signal is connected to <code>findClicked()</code> and <a href="qdialog.html#accept">accept()</a>. The <a href="qdialog.html#accept">accept()</a> slot provided by <a href="qdialog.html">QDialog</a> hides the dialog and sets the result code to <a href="qdialog.html#DialogCode-enum">Accepted</a>. We use this function to help <code>AddressBook</code>'s <code>findContact()</code> function know when the <code>FindDialog</code> object has been closed. We will explain this logic in further detail when discussing the <code>findContact()</code> function.</p> <p class="centerAlign"><img src="images/addressbook-tutorial-part5-signals-and-slots.png" alt="" /></p><p>In <code>findClicked()</code>, we validate <code>lineEdit</code> to ensure that the user did not click the <b>Find</b> button without entering a contact's name. Then, we set <code>findText</code> to the search string, extracted from <code>lineEdit</code>. After that, we clear the contents of <code>lineEdit</code> and hide the dialog.</p> <pre class="cpp"> <span class="type">void</span> FindDialog<span class="operator">::</span>findClicked() { <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text <span class="operator">=</span> lineEdit<span class="operator">-</span><span class="operator">></span>text(); <span class="keyword">if</span> (text<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."</span>)); <span class="keyword">return</span>; } <span class="keyword">else</span> { findText <span class="operator">=</span> text; lineEdit<span class="operator">-</span><span class="operator">></span>clear(); hide(); } } </pre> <p>The <code>findText</code> variable has a public getter function, <code>getFindText()</code>, associated with it. Since we only ever set <code>findText</code> directly in both the constructor and in the <code>findClicked()</code> function, we do not create a setter function to accompany <code>getFindText()</code>. Because <code>getFindText()</code> is public, classes instantiating and using <code>FindDialog</code> can always access the search string that the user has entered and accepted.</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> FindDialog<span class="operator">::</span>getFindText() { <span class="keyword">return</span> findText; } </pre> <a name="defining-the-addressbook-class"></a> <h2 id="defining-the-addressbook-class">Defining the AddressBook Class</h2> <p>To ensure we can use <code>FindDialog</code> from within our <code>AddressBook</code> class, we include <code>finddialog.h</code> in the <code>addressbook.h</code> file.</p> <pre class="cpp"> <span class="preprocessor">#include "finddialog.h"</span> </pre> <p>So far, all our address book features have a <a href="qpushbutton.html">QPushButton</a> and a corresponding slot. Similarly, for the <b>Find</b> feature we have <code>findButton</code> and <code>findContact()</code>.</p> <p>The <code>findButton</code> is declared as a private variable and the <code>findContact()</code> function is declared as a public slot.</p> <pre class="cpp"> <span class="type">void</span> findContact(); ... <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>findButton; </pre> <p>Lastly, we declare the private variable, <code>dialog</code>, which we will use to refer to an instance of <code>FindDialog</code>.</p> <pre class="cpp"> FindDialog <span class="operator">*</span>dialog; </pre> <p>Once we have instantiated a dialog, we will want to use it more than once; using a private variable allows us to refer to it from more than one place in the class.</p> <a name="implementing-the-addressbook-class"></a> <h2 id="implementing-the-addressbook-class">Implementing the AddressBook Class</h2> <p>Within the <code>AddressBook</code> class's constructor, we instantiate our private objects, <code>findButton</code> and <code>findDialog</code>:</p> <pre class="cpp"> findButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"&Find"</span>)); findButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">false</span>); ... dialog <span class="operator">=</span> <span class="keyword">new</span> FindDialog(<span class="keyword">this</span>); </pre> <p>Next, we connect the <code>findButton</code>'s <a href="qabstractbutton.html#clicked">clicked()</a> signal to <code>findContact()</code>.</p> <pre class="cpp"> connect(findButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(findContact())); </pre> <p>Now all that is left is the code for our <code>findContact()</code> function:</p> <pre class="cpp"> <span class="type">void</span> AddressBook<span class="operator">::</span>findContact() { dialog<span class="operator">-</span><span class="operator">></span>show(); <span class="keyword">if</span> (dialog<span class="operator">-</span><span class="operator">></span>exec() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>Accepted) { <span class="type"><a href="../qtcore/qstring.html">QString</a></span> contactName <span class="operator">=</span> dialog<span class="operator">-</span><span class="operator">></span>getFindText(); <span class="keyword">if</span> (contacts<span class="operator">.</span>contains(contactName)) { nameLine<span class="operator">-</span><span class="operator">></span>setText(contactName); addressText<span class="operator">-</span><span class="operator">></span>setText(contacts<span class="operator">.</span>value(contactName)); } <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">"Contact Not Found"</span>)<span class="operator">,</span> tr(<span class="string">"Sorry, \"%1\" is not in your address book."</span>)<span class="operator">.</span>arg(contactName)); <span class="keyword">return</span>; } } updateInterface(NavigationMode); } </pre> <p>We start out by displaying the <code>FindDialog</code> instance, <code>dialog</code>. This is when the user enters a contact name to look up. Once the user clicks the dialog's <code>findButton</code>, the dialog is hidden and the result code is set to <a href="qdialog.html#DialogCode-enum">QDialog::Accepted</a>. This ensures that our <code>if</code> statement is always true.</p> <p>We then proceed to extract the search string, which in this case is <code>contactName</code>, using <code>FindDialog</code>'s <code>getFindText()</code> function. If the contact exists in our address book, we display it immediately. Otherwise, we display the <a href="qmessagebox.html">QMessageBox</a> shown below to indicate that their search failed.</p> <p class="centerAlign"><img src="images/addressbook-tutorial-part5-notfound.png" alt="" /></p><p>Files:</p> <ul> <li><a href="qtwidgets-tutorials-addressbook-part5-addressbook-cpp.html">tutorials/addressbook/part5/addressbook.cpp</a></li> <li><a href="qtwidgets-tutorials-addressbook-part5-addressbook-h.html">tutorials/addressbook/part5/addressbook.h</a></li> <li><a href="qtwidgets-tutorials-addressbook-part5-finddialog-cpp.html">tutorials/addressbook/part5/finddialog.cpp</a></li> <li><a href="qtwidgets-tutorials-addressbook-part5-finddialog-h.html">tutorials/addressbook/part5/finddialog.h</a></li> <li><a href="qtwidgets-tutorials-addressbook-part5-main-cpp.html">tutorials/addressbook/part5/main.cpp</a></li> <li><a href="qtwidgets-tutorials-addressbook-part5-part5-pro.html">tutorials/addressbook/part5/part5.pro</a></li> </ul> </div> <!-- @@@tutorials/addressbook/part5 --> </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>