<?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" /> <!-- addressbook.qdoc --> <title>Qt 4.8: Part 5 - Adding a Find Function</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <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>Part 5 - Adding a Find Function</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <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> <h1 class="title">Part 5 - Adding a Find Function</h1> <span class="subtitle"></span> <!-- $$$tutorials/addressbook/part5-description --> <div class="descr"> <a name="details"></a> <p>Files:</p> <ul> <li><a href="tutorials-addressbook-part5-addressbook-cpp.html">tutorials/addressbook/part5/addressbook.cpp</a></li> <li><a href="tutorials-addressbook-part5-addressbook-h.html">tutorials/addressbook/part5/addressbook.h</a></li> <li><a href="tutorials-addressbook-part5-finddialog-cpp.html">tutorials/addressbook/part5/finddialog.cpp</a></li> <li><a href="tutorials-addressbook-part5-finddialog-h.html">tutorials/addressbook/part5/finddialog.h</a></li> <li><a href="tutorials-addressbook-part5-main-cpp.html">tutorials/addressbook/part5/main.cpp</a></li> <li><a href="tutorials-addressbook-part5-part5-pro.html">tutorials/addressbook/part5/part5.pro</a></li> </ul> <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 <tt>FindDialog</tt> class.</p> <a name="defining-the-finddialog-class"></a> <h2>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 <tt>finddialog.h</tt> 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 <tt>AddressBook</tt> class, the <tt>FindDialog</tt> class includes the <a href="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="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="qstring.html">QString</a></span> findText; };</pre> <p>We define a public function, <tt>getFindText()</tt>, to be used by classes that instantiate <tt>FindDialog</tt>. This function allows these classes to obtain the search string entered by the user. A public slot, <tt>findClicked()</tt>, 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, <tt>findButton</tt>, <tt>lineEdit</tt> and <tt>findText</tt>, 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>Implementing the FindDialog Class</h2> <p>Within the constructor of <tt>FindDialog</tt>, we set up the private variables, <tt>lineEdit</tt>, <tt>findButton</tt> and <tt>findText</tt>. 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 <tt>findButton</tt>'s <a href="qabstractbutton.html#clicked">clicked()</a> signal is connected to to <tt>findClicked()</tt> 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 <tt>AddressBook</tt>'s <tt>findContact()</tt> function know when the <tt>FindDialog</tt> object has been closed. We will explain this logic in further detail when discussing the <tt>findContact()</tt> function.</p> <p class="centerAlign"><img src="images/addressbook-tutorial-part5-signals-and-slots.png" alt="" /></p><p>In <tt>findClicked()</tt>, we validate <tt>lineEdit</tt> to ensure that the user did not click the <b>Find</b> button without entering a contact's name. Then, we set <tt>findText</tt> to the search string, extracted from <tt>lineEdit</tt>. After that, we clear the contents of <tt>lineEdit</tt> and hide the dialog.</p> <pre class="cpp"> <span class="type">void</span> FindDialog<span class="operator">::</span>findClicked() { <span class="type"><a href="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 <tt>findText</tt> variable has a public getter function, <tt>getFindText()</tt>, associated with it. Since we only ever set <tt>findText</tt> directly in both the constructor and in the <tt>findClicked()</tt> function, we do not create a setter function to accompany <tt>getFindText()</tt>. Because <tt>getFindText()</tt> is public, classes instantiating and using <tt>FindDialog</tt> can always access the search string that the user has entered and accepted.</p> <pre class="cpp"> <span class="type"><a href="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>Defining the AddressBook Class</h2> <p>To ensure we can use <tt>FindDialog</tt> from within our <tt>AddressBook</tt> class, we include <tt>finddialog.h</tt> in the <tt>addressbook.h</tt> 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 <tt>findButton</tt> and <tt>findContact()</tt>.</p> <p>The <tt>findButton</tt> is declared as a private variable and the <tt>findContact()</tt> 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, <tt>dialog</tt>, which we will use to refer to an instance of <tt>FindDialog</tt>.</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>Implementing the AddressBook Class</h2> <p>Within the <tt>AddressBook</tt> class's constructor, we instantiate our private objects, <tt>findButton</tt> and <tt>findDialog</tt>:</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;</pre> <p>Next, we connect the <tt>findButton</tt>'s <a href="qabstractbutton.html#clicked">clicked()</a> signal to <tt>findContact()</tt>.</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 <tt>findContact()</tt> 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="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 <tt>FindDialog</tt> instance, <tt>dialog</tt>. This is when the user enters a contact name to look up. Once the user clicks the dialog's <tt>findButton</tt>, the dialog is hidden and the result code is set to <a href="qdialog.html#DialogCode-enum">QDialog::Accepted</a>. This ensures that our <tt>if</tt> statement is always true.</p> <p>We then proceed to extract the search string, which in this case is <tt>contactName</tt>, using <tt>FindDialog</tt>'s <tt>getFindText()</tt> 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></div> <!-- @@@tutorials/addressbook/part5 --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2013 Digia Plc 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> Digia, Qt and their respective logos are trademarks of Digia Plc 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> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>