/usr/share/qt5/doc/qtuitools/qtuitools-textfinder-example.html is in qttools5-doc-html 5.9.5-0ubuntu1.
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | <?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.9</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.9</td><td ><a href="qtuitools-index.html">Qt UI Tools</a></td><td ><a href="examples-qtuitools.html">Qt UI Tools Examples</a></td><td >Text Finder Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 Reference Documentation</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="#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="#loading-the-resources">Loading the Resources</a></li>
<li class="level1"><a href="#textfinder-class-implementation">TextFinder Class Implementation</a></li>
<li class="level1"><a href="#main-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>The TextFinder example shows how to load and setup a <code>.ui</code> file dynamically using the <a href="quiloader.html">QUiLoader</a> class that is part of the <a href="qtuitools-index.html">Qt UI Tools</a> library.</p>
<p>The program allows the user to look up a particular word within the contents of a text. The visual elements and layout of the user interface is loaded at runtime, from a the program resources.</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 the example are:</p>
<ul>
<li><code>textfinder.ui</code> - the user interface file created in Qt Designer</li>
<li><code>input.txt</code> - a text file containing some text to be displayed in a <a href="../qtwidgets/qtextedit.html">QTextEdit</a></li>
</ul>
<p><code>textfinder.ui</code> 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 <code>input.txt</code>, 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 <b>Find</b> button. Note that all the widgets have sensible <code>objectName</code>'s assigned. These are used in code to identify them.</p>
<p>The screenshot below shows the preview obtained in Qt Designer.</p>
<p class="centerAlign"><img src="images/textfinder-example-userinterface.png" alt="" /></p><p>In this example, we store both resources in the applicaton's executable by including the <code>textfinder.qrc</code> file. Alternatively, the files could also be loaded at runtime from the file system, or from an external binary resource <code>.rcc</code> file. For more information on resource files, see <a href="../qtcore/resources.html">The Qt Resource System</a>.</p>
<p>The <code>textfinder.qrc</code> file lists all files that should be included as a resource:</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>To generate a form at run-time, the example is linked against the <a href="qtuitools-index.html">Qt Ui Tools</a> library. This is done in the <code>textfinder.pro</code> file:</p>
<pre class="cpp">
QT += widgets uitools
HEADERS = textfinder.h
SOURCES = textfinder.cpp main.cpp
RESOURCES = textfinder.qrc
</pre>
<a name="textfinder-class-definition"></a>
<h2 id="textfinder-class-definition">TextFinder Class Definition</h2>
<p>The <code>TextFinder</code> class contains the main user interface. It declares pointers to the <a href="../qtwidgets/qpushbutton.html">QPushButton</a>, <a href="../qtwidgets/qtextedit.html">QTextEdit</a> and <a href="../qtwidgets/qlineedit.html">QLineEdit</a> elements described above. The <a href="../qtwidgets/qlabel.html">QLabel</a> in the user interface is not declared here as we do not need to access it from code.</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>:
<span class="keyword">explicit</span> TextFinder(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr);
<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/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;
};
</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="loading-the-resources"></a>
<h2 id="loading-the-resources">Loading the Resources</h2>
<p>We use <a href="../qtcore/qfile.html">QFile</a> to load the data from the program resources at runtime. The code for this is in two method methods on top of <code>textfinder.cpp</code>: <code>loadUiFile</code> and <code>loadTextFile</code>.</p>
<p>The <code>loadUiFile</code> function loads the user interface file previously created in Qt Designer. First, the content of the <code>textfinder.ui</code> file is loaded from the resource system. Then a <a href="quiloader.html">QUiLoader</a> instance is created, and the <a href="quiloader.html#load">QUiLoader::load</a>() function is called, with the first argument being the open file, and the second argument being the pointer of the widget that should be set as the parent. The created <a href="../qtwidgets/qwidget.html">QWidget</a> is returned.</p>
<pre class="cpp">
<span class="keyword">static</span> <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>loadUiFile(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
{
<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/qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly);
<span class="type"><a href="quiloader.html">QUiLoader</a></span> loader;
<span class="keyword">return</span> loader<span class="operator">.</span>load(<span class="operator">&</span>file<span class="operator">,</span> parent);
}
</pre>
<p>In a similar vein, the <code>loadTextFile</code> function loads <code>input.txt</code> from the resources. Data is read using <a href="../qtcore/qtextstream.html">QTextStream</a> into a <a href="../qtcore/qstring.html">QString</a> with the <a href="../qtcore/qtextstream.html#readAll">QTextStream::readAll</a>() function. We explicitly set the encoding to <i>UTF-8</i>, because <a href="../qtcore/qtextstream.html">QTextStream</a> by default uses the current system locale. Finally, the loaded text is returned.</p>
<pre class="cpp">
<span class="keyword">static</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></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);
in<span class="operator">.</span>setCodec(<span class="string">"UTF-8"</span>);
<span class="keyword">return</span> in<span class="operator">.</span>readAll();
}
</pre>
<a name="textfinder-class-implementation"></a>
<h2 id="textfinder-class-implementation">TextFinder Class Implementation</h2>
<p>The <code>TextFinder</code> class's constructor does not instantiate any child widgets directly. Instead, it calls the <code>loadUiFile()</code> function, and then uses <a href="../qtcore/qobject.html#findChild">QObject::findChild</a>() to locate the created <a href="../qtwidgets/qwidget.html">QWidget</a>s by object name.</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(<span class="keyword">this</span>);
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#connectSlotsByName">QMetaObject::connectSlotsByName</a>() to enable the automatic calling of the <code>on_findButton_clicked()</code> slot.</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 <code>loadTextFile</code> function is called to get the text to be shown in the <a href="../qtwidgets/qtextedit.html">QTextEdit</a>.</p>
<pre class="cpp">
ui_textEdit<span class="operator">-</span><span class="operator">></span>setText(loadTextFile());
</pre>
<p>The dynamically loaded user interface in <code>formWidget</code> is now properly set up. We now embed <code>formWidget</code> through a <code>QVBoxLayout</code>.</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>At the end of the constructor we set a window title.</p>
<pre class="cpp">
setWindowTitle(tr(<span class="string">"Text Finder"</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>ui_textEdit</code>. If 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="comment">// undo previous change (if any)</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>
tr(<span class="string">"The search field is empty. "</span>
<span class="string">"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>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">
<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>
tr(<span class="string">"Sorry, the word cannot be found."</span>));
}
}
}
</pre>
<a name="main-function"></a>
<h2 id="main-function"><code>main()</code> Function</h2>
<p>The <code>main()</code> function instantiates and shows <code>TextFinder</code>.</p>
<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>)
{
<span class="type"><a href="../qtwidgets/qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);
TextFinder textFinder;
textFinder<span class="operator">.</span>show();
<span class="keyword">return</span> app<span class="operator">.</span>exec();
}
</pre>
<p>There are various approaches to include forms into applications. Using QUILoader is just one of them. See Using a Designer UI File in Your Application for more information on the other approaches available.</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> 2017 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>
|