<?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" /> <!-- textfinder.qdoc --> <title>Qt 4.8: Text Finder Example</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>Text Finder Example</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="#setting-up-the-resource-file">Setting Up The Resource File</a></li> <li class="level1"><a href="#textfinder-class-definition">TextFinder Class Definition</a></li> <li class="level1"><a href="#textfinder-class-implementation">TextFinder Class Implementation</a></li> <li class="level1"><a href="#function"><tt>main()</tt> Function</a></li> </ul> </div> <h1 class="title">Text Finder Example</h1> <span class="subtitle"></span> <!-- $$$uitools/textfinder-description --> <div class="descr"> <a name="details"></a> <p>Files:</p> <ul> <li><a href="uitools-textfinder-textfinder-cpp.html">uitools/textfinder/textfinder.cpp</a></li> <li><a href="uitools-textfinder-textfinder-h.html">uitools/textfinder/textfinder.h</a></li> <li><a href="uitools-textfinder-forms-textfinder-ui.html">uitools/textfinder/forms/textfinder.ui</a></li> <li><a href="uitools-textfinder-main-cpp.html">uitools/textfinder/main.cpp</a></li> <li><a href="uitools-textfinder-textfinder-pro.html">uitools/textfinder/textfinder.pro</a></li> <li><a href="uitools-textfinder-textfinder-qrc.html">uitools/textfinder/textfinder.qrc</a></li> </ul> <p>The Text Finder example demonstrates how to dynamically process forms using the <a href="qtuitools.html">QtUiTools</a> module.<p>Dynamic form processing enables a form to be processed at run-time only by changing the UI file for the project. The program allows the user to look up a particular word within the contents of a text file. This text file is included in the project's resource and is loaded into the display at startup.</p> <table class="generic"> <tr valign="top" class="odd"><td ><img src="images/textfinder-example-find.png" alt="" /></td><td ><img src="images/textfinder-example-find2.png" alt="" /></td></tr> </table> <a name="setting-up-the-resource-file"></a> <h2>Setting Up The Resource File</h2> <p>The resources required for Text Finder are:</p> <ul> <li><i>textfinder.ui</i> - the user interface file created in <a href="qtdesigner.html">QtDesigner</a></li> <li><i>input.txt</i> - a text file containing some text to be displayed in the <a href="qtextedit.html">QTextEdit</a></li> </ul> <p><i>textfinder.ui</i> contains all the necessary <a href="qwidget.html">QWidget</a> objects for the Text Finder. A <a href="qlineedit.html">QLineEdit</a> is used for the user input, a <a href="qtextedit.html">QTextEdit</a> is used to display the contents of <i>input.txt</i>, a <a href="qlabel.html">QLabel</a> is used to display the text "Keyword", and a <a href="qpushbutton.html">QPushButton</a> is used for the "Find" button. The screenshot below shows the preview obtained in <a href="qtdesigner.html">QtDesigner</a>.</p> <p class="centerAlign"><img src="images/textfinder-example-userinterface.png" alt="" /></p><p>A <i>textfinder.qrc</i> file is used to store both the <i>textfinder.ui</i> and <i>input.txt</i> in the application's executable. The file contains the following code:</p> <pre class="cpp"> <!DOCTYPE RCC><RCC version="1.0"> <qresource> <file>forms/textfinder.ui</file> <file>forms/input.txt</file> </qresource> </RCC></pre> <p>For more information on resource files, see <a href="resources.html">The Qt Resource System</a>.</p> <p>To generate a form at run-time, the example is linked against the <a href="qtuitools.html">QtUiTools</a> module library. This is done in the <tt>textfinder.pro</tt> file that contains the following lines:</p> <pre class="cpp"> CONFIG += uitools HEADERS = textfinder.h RESOURCES = textfinder.qrc SOURCES = textfinder.cpp main.cpp</pre> <a name="textfinder-class-definition"></a> <h2>TextFinder Class Definition</h2> <p>The <tt>TextFinder</tt> class is a subclass of <a href="qwidget.html">QWidget</a> and it hosts the <a href="qwidget.html">QWidget</a>s we need to access in the user interface. The <a href="qlabel.html">QLabel</a> in the user interface is not declared here as we do not need to access it.</p> <pre class="cpp"> <span class="keyword">class</span> TextFinder : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span> { Q_OBJECT <span class="keyword">public</span>: TextFinder(<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> on_findButton_clicked(); <span class="keyword">private</span>: <span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">*</span> loadUiFile(); <span class="type">void</span> loadTextFile(); <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>ui_findButton; <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>ui_textEdit; <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>ui_lineEdit; <span class="type">bool</span> isFirstTime; };</pre> <p>The slot <tt>on_findButton_clicked()</tt> is a slot named according to the <a href="designer-using-a-ui-file.html#automatic-connections">Automatic Connection</a> naming convention required by <tt>uic</tt>.</p> <a name="textfinder-class-implementation"></a> <h2>TextFinder Class Implementation</h2> <p>The <tt>TextFinder</tt> class's constructor calls the <tt>loadUiFile()</tt> function and then uses <tt>qFindChild()</tt> to access the user interface's <a href="qwidget.html">QWidget</a>s.</p> <pre class="cpp"> TextFinder<span class="operator">::</span>TextFinder(<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) { <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>formWidget <span class="operator">=</span> loadUiFile(); ui_findButton <span class="operator">=</span> findChild<span class="operator"><</span><span class="type"><a href="qpushbutton.html">QPushButton</a></span><span class="operator">*</span><span class="operator">></span>(<span class="string">"findButton"</span>); ui_textEdit <span class="operator">=</span> findChild<span class="operator"><</span><span class="type"><a href="qtextedit.html">QTextEdit</a></span><span class="operator">*</span><span class="operator">></span>(<span class="string">"textEdit"</span>); ui_lineEdit <span class="operator">=</span> findChild<span class="operator"><</span><span class="type"><a href="qlineedit.html">QLineEdit</a></span><span class="operator">*</span><span class="operator">></span>(<span class="string">"lineEdit"</span>);</pre> <p>We then use <a href="qmetaobject.html">QMetaObject</a>'s system to enable signal and slot connections.</p> <pre class="cpp"> <span class="type"><a href="qmetaobject.html">QMetaObject</a></span><span class="operator">::</span>connectSlotsByName(<span class="keyword">this</span>);</pre> <p>The loadTextFile() function is called to load <tt>input.txt</tt> into <a href="qtextedit.html">QTextEdit</a> to displays its contents.</p> <pre class="cpp"> loadTextFile();</pre> <p>The <tt>TextFinder</tt>'s layout is set with <a href="qwidget.html#setLayout">setLayout()</a>.</p> <pre class="cpp"> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>; layout<span class="operator">-</span><span class="operator">></span>addWidget(formWidget); setLayout(layout);</pre> <p>Finally, the window title is set to <i>Text Finder</i> and <tt>isFirstTime</tt> is set to true.</p> <p><tt>isFirstTime</tt> is used as a flag to indicate whether the search operation has been performed more than once. This is further explained with the <tt>on_findButton_clicked()</tt> function.</p> <p>The <tt>loadUiFile()</tt> function is used to load the user interface file previously created in <a href="qtdesigner.html">QtDesigner</a>. The <a href="quiloader.html">QUiLoader</a> class is instantiated and its <tt>load()</tt> function is used to load the form into <tt>formWidget</tt> that acts as a place holder for the user interface. The function then returns <tt>formWidget</tt> to its caller.</p> <pre class="cpp"> <span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">*</span> TextFinder<span class="operator">::</span>loadUiFile() { <span class="type"><a href="quiloader.html">QUiLoader</a></span> loader; <span class="type"><a href="qfile.html">QFile</a></span> file(<span class="string">":/forms/textfinder.ui"</span>); file<span class="operator">.</span>open(<span class="type"><a href="qfile.html">QFile</a></span><span class="operator">::</span>ReadOnly); <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>formWidget <span class="operator">=</span> loader<span class="operator">.</span>load(<span class="operator">&</span>file<span class="operator">,</span> <span class="keyword">this</span>); file<span class="operator">.</span>close(); <span class="keyword">return</span> formWidget; }</pre> <p>As mentioned earlier, the loadTextFile() function loads <i>input.txt</i> into <a href="qtextedit.html">QTextEdit</a> to display its contents. Data is read using <a href="qtextstream.html">QTextStream</a> into a <a href="qstring.html">QString</a> object, <tt>line</tt> with the <a href="qtextstream.html#readAll">QTextStream::readAll</a>() function. The contents of <tt>line</tt> are then appended to <tt>ui_textEdit</tt>.</p> <pre class="cpp"> <span class="type">void</span> TextFinder<span class="operator">::</span>loadTextFile() { <span class="type"><a href="qfile.html">QFile</a></span> inputFile(<span class="string">":/forms/input.txt"</span>); inputFile<span class="operator">.</span>open(<span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly); <span class="type"><a href="qtextstream.html">QTextStream</a></span> in(<span class="operator">&</span>inputFile); <span class="type"><a href="qstring.html">QString</a></span> line <span class="operator">=</span> in<span class="operator">.</span>readAll(); inputFile<span class="operator">.</span>close(); ui_textEdit<span class="operator">-</span><span class="operator">></span>append(line); ui_textEdit<span class="operator">-</span><span class="operator">></span>setUndoRedoEnabled(<span class="keyword">false</span>); ui_textEdit<span class="operator">-</span><span class="operator">></span>setUndoRedoEnabled(<span class="keyword">true</span>); }</pre> <p>The <tt>on_findButton_clicked()</tt> function is a slot that is connected to <tt>ui_findButton</tt>'s <tt>clicked()</tt> signal. The <tt>searchString</tt> is extracted from the <tt>ui_lineEdit</tt> and the <tt>document</tt> is extracted from <tt>textEdit</tt>. In event there is an empty <tt>searchString</tt>, a <a href="qmessagebox.html">QMessageBox</a> is used, requesting the user to enter a word. Otherwise, we traverse through the words in <tt>ui_textEdit</tt>, and highlight all ocurrences of the <tt>searchString</tt> . Two <a href="qtextcursor.html">QTextCursor</a> objects are used: One to traverse through the words in <tt>line</tt> and another to keep track of the edit blocks.</p> <pre class="cpp"> <span class="type">void</span> TextFinder<span class="operator">::</span>on_findButton_clicked() { <span class="type"><a href="qstring.html">QString</a></span> searchString <span class="operator">=</span> ui_lineEdit<span class="operator">-</span><span class="operator">></span>text(); <span class="type"><a href="qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>document <span class="operator">=</span> ui_textEdit<span class="operator">-</span><span class="operator">></span>document(); <span class="type">bool</span> found <span class="operator">=</span> <span class="keyword">false</span>; <span class="keyword">if</span> (isFirstTime <span class="operator">=</span><span class="operator">=</span> <span class="keyword">false</span>) document<span class="operator">-</span><span class="operator">></span>undo(); <span class="keyword">if</span> (searchString<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 Search Field"</span>)<span class="operator">,</span> <span class="string">"The search field is empty. Please enter a word and click Find."</span>); } <span class="keyword">else</span> { <span class="type"><a href="qtextcursor.html">QTextCursor</a></span> highlightCursor(document); <span class="type"><a href="qtextcursor.html">QTextCursor</a></span> cursor(document); cursor<span class="operator">.</span>beginEditBlock(); <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> plainFormat(highlightCursor<span class="operator">.</span>charFormat()); <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> colorFormat <span class="operator">=</span> plainFormat; colorFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>red); <span class="keyword">while</span> (<span class="operator">!</span>highlightCursor<span class="operator">.</span>isNull() <span class="operator">&</span><span class="operator">&</span> <span class="operator">!</span>highlightCursor<span class="operator">.</span>atEnd()) { highlightCursor <span class="operator">=</span> document<span class="operator">-</span><span class="operator">></span>find(searchString<span class="operator">,</span> highlightCursor<span class="operator">,</span> <span class="type"><a href="qtextdocument.html">QTextDocument</a></span><span class="operator">::</span>FindWholeWords); <span class="keyword">if</span> (<span class="operator">!</span>highlightCursor<span class="operator">.</span>isNull()) { found <span class="operator">=</span> <span class="keyword">true</span>; highlightCursor<span class="operator">.</span>movePosition(<span class="type"><a href="qtextcursor.html">QTextCursor</a></span><span class="operator">::</span>WordRight<span class="operator">,</span> <span class="type"><a href="qtextcursor.html">QTextCursor</a></span><span class="operator">::</span>KeepAnchor); highlightCursor<span class="operator">.</span>mergeCharFormat(colorFormat); } } cursor<span class="operator">.</span>endEditBlock();</pre> <p>The <tt>isFirstTime</tt> flag is set to false the moment <tt>findButton</tt> is clicked. This is necessary to undo the previous text highlight before highlighting the user's next search string. Also, the <tt>found</tt> flag is used to indicate if the <tt>searchString</tt> was found within the contents of <tt>ui_textEdit</tt>. If it was not found, a <a href="qmessagebox.html">QMessageBox</a> is used to inform the user.</p> <pre class="cpp"> isFirstTime <span class="operator">=</span> <span class="keyword">false</span>; <span class="keyword">if</span> (found <span class="operator">=</span><span class="operator">=</span> <span class="keyword">false</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">"Word Not Found"</span>)<span class="operator">,</span> <span class="string">"Sorry, the word cannot be found."</span>); } } }</pre> <a name="function"></a> <h2><tt>main()</tt> Function</h2> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>) { Q_INIT_RESOURCE(textfinder); <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv); TextFinder <span class="operator">*</span>textFinder <span class="operator">=</span> <span class="keyword">new</span> TextFinder; textFinder<span class="operator">-</span><span class="operator">></span>show(); <span class="keyword">return</span> app<span class="operator">.</span>exec(); }</pre> <p>The <tt>main()</tt> function initialises the <i>textfinder.qrc</i> resource file and instantiates as well as displays <tt>TextFinder</tt>.</p> </div> <p><b>See also </b><a href="designer-calculatorbuilder.html">Calculator Builder Example</a> and <a href="designer-worldtimeclockbuilder.html">World Time Clock Builder Example</a>.</p> <!-- @@@uitools/textfinder --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2015 The Qt Company Ltd. 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> 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. <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>