|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the documentation of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 /*! |
|
43 \example uitools/textfinder |
|
44 \title Text Finder Example |
|
45 |
|
46 The Text Finder example demonstrates how to dynamically process forms |
|
47 using the QtUiTools module. Dynamic form processing enables a form to |
|
48 be processed at run-time only by changing the UI file for the project. |
|
49 The program allows the user to look up a particular word within the |
|
50 contents of a text file. This text file is included in the project's |
|
51 resource and is loaded into the display at startup. |
|
52 |
|
53 \table |
|
54 \row \o \inlineimage textfinder-example-find.png |
|
55 \o \inlineimage textfinder-example-find2.png |
|
56 \endtable |
|
57 |
|
58 \section1 Setting Up The Resource File |
|
59 |
|
60 The resources required for Text Finder are: |
|
61 \list |
|
62 \o \e{textfinder.ui} - the user interface file created in QtDesigner |
|
63 \o \e{input.txt} - a text file containing some text to be displayed |
|
64 in the QTextEdit |
|
65 \endlist |
|
66 |
|
67 \e{textfinder.ui} contains all the necessary QWidget objects for the |
|
68 Text Finder. A QLineEdit is used for the user input, a QTextEdit is |
|
69 used to display the contents of \e{input.txt}, a QLabel is used to |
|
70 display the text "Keyword", and a QPushButton is used for the "Find" |
|
71 button. The screenshot below shows the preview obtained in QtDesigner. |
|
72 |
|
73 \image textfinder-example-userinterface.png |
|
74 |
|
75 A \e{textfinder.qrc} file is used to store both the \e{textfinder.ui} |
|
76 and \e{input.txt} in the application's executable. The file contains |
|
77 the following code: |
|
78 |
|
79 \quotefile examples/uitools/textfinder/textfinder.qrc |
|
80 |
|
81 For more information on resource files, see \l{The Qt Resource System}. |
|
82 |
|
83 To generate a form at run-time, the example is linked against the |
|
84 QtUiTools module library. This is done in the \c{textfinder.pro} file |
|
85 that contains the following lines: |
|
86 |
|
87 \snippet doc/src/snippets/code/doc_src_examples_textfinder.qdoc 0 |
|
88 |
|
89 \section1 TextFinder Class Definition |
|
90 |
|
91 The \c TextFinder class is a subclass of QWidget and it hosts the |
|
92 \l{QWidget}s we need to access in the user interface. The QLabel in the |
|
93 user interface is not declared here as we do not need to access it. |
|
94 |
|
95 \snippet examples/uitools/textfinder/textfinder.h 0 |
|
96 |
|
97 The slot \c{on_find_Button_clicked()} is a slot named according to the |
|
98 \l{Using a Designer UI File in Your Application#Automatic Connections} |
|
99 {Automatic Connection} naming convention required |
|
100 by \c uic. |
|
101 |
|
102 \section1 TextFinder Class Implementation |
|
103 |
|
104 The \c TextFinder class's constructor calls the \c loadUiFile() function |
|
105 and then uses \c qFindChild() to access the user interface's |
|
106 \l{QWidget}s. |
|
107 |
|
108 \snippet examples/uitools/textfinder/textfinder.cpp 0 |
|
109 |
|
110 We then use QMetaObject's system to enable signal and slot connections. |
|
111 |
|
112 \snippet examples/uitools/textfinder/textfinder.cpp 2 |
|
113 |
|
114 The loadTextFile() function is called to load \c{input.txt} into |
|
115 QTextEdit to displays its contents. |
|
116 |
|
117 \snippet examples/uitools/textfinder/textfinder.cpp 3a |
|
118 |
|
119 The \c{TextFinder}'s layout is set with \l{QWidget::}{setLayout()}. |
|
120 |
|
121 \snippet examples/uitools/textfinder/textfinder.cpp 3b |
|
122 |
|
123 Finally, the window title is set to \e {Text Finder} and \c isFirstTime is |
|
124 set to true. |
|
125 |
|
126 \c isFirstTime is used as a flag to indicate whether the search operation |
|
127 has been performed more than once. This is further explained with the |
|
128 \c{on_findButton_clicked()} function. |
|
129 |
|
130 The \c{loadUiFile()} function is used to load the user interface file |
|
131 previously created in QtDesigner. The QUiLoader class is instantiated |
|
132 and its \c load() function is used to load the form into \c{formWidget} |
|
133 that acts as a place holder for the user interface. The function then |
|
134 returns \c{formWidget} to its caller. |
|
135 |
|
136 \snippet examples/uitools/textfinder/textfinder.cpp 4 |
|
137 |
|
138 As mentioned earlier, the loadTextFile() function loads \e{input.txt} |
|
139 into QTextEdit to display its contents. Data is read using QTextStream |
|
140 into a QString object, \c line with the QTextStream::readAll() function. |
|
141 The contents of \c line are then appended to \c{ui_textEdit}. |
|
142 |
|
143 \snippet examples/uitools/textfinder/textfinder.cpp 5 |
|
144 |
|
145 The \c{on_findButton_clicked()} function is a slot that is connected to |
|
146 \c{ui_findButton}'s \c clicked() signal. The \c searchString is extracted |
|
147 from the \c ui_lineEdit and the \c document is extracted from \c textEdit. |
|
148 In event there is an empty \c searchString, a QMessageBox is used, |
|
149 requesting the user to enter a word. Otherwise, we traverse through the |
|
150 words in \c ui_textEdit, and highlight all ocurrences of the |
|
151 \c searchString . Two QTextCursor objects are used: One to traverse through |
|
152 the words in \c line and another to keep track of the edit blocks. |
|
153 |
|
154 \snippet examples/uitools/textfinder/textfinder.cpp 7 |
|
155 |
|
156 The \c isFirstTime flag is set to false the moment \c findButton is |
|
157 clicked. This is necessary to undo the previous text highlight before |
|
158 highlighting the user's next search string. Also, the \c found flag |
|
159 is used to indicate if the \c searchString was found within the contents |
|
160 of \c ui_textEdit. If it was not found, a QMessageBox is used |
|
161 to inform the user. |
|
162 |
|
163 \snippet examples/uitools/textfinder/textfinder.cpp 9 |
|
164 |
|
165 \section1 \c main() Function |
|
166 |
|
167 \snippet examples/uitools/textfinder/main.cpp 0 |
|
168 |
|
169 The \c main() function initialises the \e{textfinder.qrc} resource file |
|
170 and instantiates as well as displays \c TextFinder. |
|
171 |
|
172 \sa{Calculator Builder Example}, {World Time Clock Builder Example} |
|
173 */ |