/usr/share/qt5/doc/qtuitools/qtuitools-textfinder-example.html is in qttools5-doc-html 5.5.1-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- textfinder.qdoc -->
<title>Text Finder Example | Qt UI Tools 5.5</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.5</li>
<li><a href="qtuitools-index.html">Qt UI Tools</a></li>
<li>Text Finder Example</li>
<li id="buildversion">Qt 5.5.1 Reference Documentation</li>
</ul>
</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="#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="#func-target-main-main-func-function"><code>main()</code> Function</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Text Finder Example</h1>
<span class="subtitle"></span>
<!-- $$$textfinder-description -->
<div class="descr"> <a name="details"></a>
<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>
<div class="table"><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></div>
<a name="setting-up-the-resource-file"></a>
<h2 id="setting-up-the-resource-file">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 QtDesigner</li>
<li><i>input.txt</i> - a text file containing some text to be displayed in the <a href="../qtwidgets/qtextedit.html">QTextEdit</a></li>
</ul>
<p><i>textfinder.ui</i> contains all the necessary <a href="../qtwidgets/qwidget.html">QWidget</a> objects for the Text Finder. A <a href="../qtwidgets/qlineedit.html">QLineEdit</a> is used for the user input, a <a href="../qtwidgets/qtextedit.html">QTextEdit</a> is used to display the contents of <i>input.txt</i>, a <a href="../qtwidgets/qlabel.html">QLabel</a> is used to display the text "Keyword", and a <a href="../qtwidgets/qpushbutton.html">QPushButton</a> is used for the "Find" button. The screenshot below shows the preview obtained in QtDesigner.</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="../qtcore/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-module.html">QtUiTools</a> module library. This is done in the <code>textfinder.pro</code> file that contains the following lines:</p>
<pre class="cpp">QT += uitools
HEADERS = textfinder.h
RESOURCES = textfinder.qrc
SOURCES = textfinder.cpp main.cpp</pre>
<a name="textfinder-class-definition"></a>
<h2 id="textfinder-class-definition">TextFinder Class Definition</h2>
<p>The <code>TextFinder</code> class is a subclass of <a href="../qtwidgets/qwidget.html">QWidget</a> and it hosts the <a href="../qtwidgets/qwidget.html">QWidget</a>s we need to access in the user interface. The <a href="../qtwidgets/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="../qtwidgets/qwidget.html">QWidget</a></span>
{
Q_OBJECT
<span class="keyword">public</span>:
TextFinder(<span class="type"><a href="../qtwidgets/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="../qtwidgets/qwidget.html">QWidget</a></span><span class="operator">*</span> loadUiFile();
<span class="type">void</span> loadTextFile();
<span class="type"><a href="../qtwidgets/qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>ui_findButton;
<span class="type"><a href="../qtwidgets/qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>ui_textEdit;
<span class="type"><a href="../qtwidgets/qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>ui_lineEdit;
bool isFirstTime;
};</pre>
<p>The slot <code>on_findButton_clicked()</code> is a slot named according to the Automatic Connection naming convention required by <code>uic</code>.</p>
<a name="textfinder-class-implementation"></a>
<h2 id="textfinder-class-implementation">TextFinder Class Implementation</h2>
<p>The <code>TextFinder</code> class's constructor calls the <code>loadUiFile()</code> function and then uses <code>qFindChild()</code> to access the user interface's <a href="../qtwidgets/qwidget.html">QWidget</a>s.</p>
<pre class="cpp">TextFinder<span class="operator">::</span>TextFinder(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
: <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span>(parent)
{
<span class="type"><a href="../qtwidgets/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="../qtwidgets/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="../qtwidgets/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="../qtwidgets/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="../qtcore/qmetaobject.html">QMetaObject</a>'s system to enable signal and slot connections.</p>
<pre class="cpp"> <span class="type"><a href="../qtcore/qmetaobject.html">QMetaObject</a></span><span class="operator">::</span>connectSlotsByName(<span class="keyword">this</span>);</pre>
<p>The loadTextFile() function is called to load <code>input.txt</code> into <a href="../qtwidgets/qtextedit.html">QTextEdit</a> to displays its contents.</p>
<pre class="cpp"> loadTextFile();</pre>
<p>The <code>TextFinder</code>'s layout is set with <a href="../qtwidgets/qwidget.html#setLayout">setLayout()</a>.</p>
<pre class="cpp"> <span class="type"><a href="../qtwidgets/qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/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 <code>isFirstTime</code> is set to true.</p>
<p><code>isFirstTime</code> is used as a flag to indicate whether the search operation has been performed more than once. This is further explained with the <code>on_findButton_clicked()</code> function.</p>
<p>The <code>loadUiFile()</code> function is used to load the user interface file previously created in QtDesigner. The <a href="quiloader.html">QUiLoader</a> class is instantiated and its <code>load()</code> function is used to load the form into <code>formWidget</code> that acts as a place holder for the user interface. The function then returns <code>formWidget</code> to its caller.</p>
<pre class="cpp"><span class="type"><a href="../qtwidgets/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="../qtcore/qfile.html">QFile</a></span> file(<span class="string">":/forms/textfinder.ui"</span>);
file<span class="operator">.</span>open(<span class="type"><a href="../qtcore/qfile.html">QFile</a></span><span class="operator">::</span>ReadOnly);
<span class="type"><a href="../qtwidgets/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="../qtwidgets/qtextedit.html">QTextEdit</a> to display its contents. Data is read using <a href="../qtcore/qtextstream.html">QTextStream</a> into a <a href="../qtcore/qstring.html">QString</a> object, <code>line</code> with the <a href="../qtcore/qtextstream.html#readAll">QTextStream::readAll</a>() function. The contents of <code>line</code> are then appended to <code>ui_textEdit</code>.</p>
<pre class="cpp"><span class="type">void</span> TextFinder<span class="operator">::</span>loadTextFile()
{
<span class="type"><a href="../qtcore/qfile.html">QFile</a></span> inputFile(<span class="string">":/forms/input.txt"</span>);
inputFile<span class="operator">.</span>open(<span class="type"><a href="../qtcore/qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly);
<span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span> in(<span class="operator">&</span>inputFile);
<span class="type"><a href="../qtcore/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 <code>on_findButton_clicked()</code> function is a slot that is connected to <code>ui_findButton</code>'s <code>clicked()</code> signal. The <code>searchString</code> is extracted from the <code>ui_lineEdit</code> and the <code>document</code> is extracted from <code>textEdit</code>. In event there is an empty <code>searchString</code>, a <a href="../qtwidgets/qmessagebox.html">QMessageBox</a> is used, requesting the user to enter a word. Otherwise, we traverse through the words in <code>ui_textEdit</code>, and highlight all ocurrences of the <code>searchString</code> . Two QTextCursor objects are used: One to traverse through the words in <code>line</code> 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="../qtcore/qstring.html">QString</a></span> searchString <span class="operator">=</span> ui_lineEdit<span class="operator">-</span><span class="operator">></span>text();
<span class="type">QTextDocument</span> <span class="operator">*</span>document <span class="operator">=</span> ui_textEdit<span class="operator">-</span><span class="operator">></span>document();
bool 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="../qtwidgets/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">QTextCursor</span> highlightCursor(document);
<span class="type">QTextCursor</span> cursor(document);
cursor<span class="operator">.</span>beginEditBlock();
<span class="type">QTextCharFormat</span> plainFormat(highlightCursor<span class="operator">.</span>charFormat());
<span class="type">QTextCharFormat</span> colorFormat <span class="operator">=</span> plainFormat;
colorFormat<span class="operator">.</span>setForeground(<span class="type"><a href="../qtcore/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">QTextDocument</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">QTextCursor</span><span class="operator">::</span>WordRight<span class="operator">,</span>
<span class="type">QTextCursor</span><span class="operator">::</span>KeepAnchor);
highlightCursor<span class="operator">.</span>mergeCharFormat(colorFormat);
}
}
cursor<span class="operator">.</span>endEditBlock();</pre>
<p>The <code>isFirstTime</code> flag is set to false the moment <code>findButton</code> is clicked. This is necessary to undo the previous text highlight before highlighting the user's next search string. Also, the <code>found</code> flag is used to indicate if the <code>searchString</code> was found within the contents of <code>ui_textEdit</code>. If it was not found, a <a href="../qtwidgets/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="../qtwidgets/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="func-target-main-main-func-function"></a>
<h2 id="func-target-main-main-func-function"><code>main()</code> 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="../qtwidgets/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 <code>main()</code> function initialises the <i>textfinder.qrc</i> resource file and instantiates as well as displays <code>TextFinder</code>.</p>
<p>Files:</p>
<ul>
<li><a href="qtuitools-textfinder-textfinder-cpp.html">textfinder/textfinder.cpp</a></li>
<li><a href="qtuitools-textfinder-textfinder-h.html">textfinder/textfinder.h</a></li>
<li><a href="qtuitools-textfinder-forms-textfinder-ui.html">textfinder/forms/textfinder.ui</a></li>
<li><a href="qtuitools-textfinder-main-cpp.html">textfinder/main.cpp</a></li>
<li><a href="qtuitools-textfinder-textfinder-pro.html">textfinder/textfinder.pro</a></li>
<li><a href="qtuitools-textfinder-textfinder-qrc.html">textfinder/textfinder.qrc</a></li>
</ul>
</div>
<p><b>See also </b>Calculator Builder Example and World Time Clock Builder Example.</p>
<!-- @@@textfinder -->
</div>
</div>
</div>
</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.<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>
|