Debian Codegraph @

examples/uitools/doc/src/textfinder.qdoc

raw
1 // Copyright (C) 2017 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4 /*!
5 \example textfinder
6 \examplecategory {Desktop}
7 \meta tags {widgets,designer}
8 \ingroup examples-qtuitools
9 \title Text Finder
10
11 \brief Dynamically loading .ui files using QUiLoader.
12
13 The TextFinder example shows how to load and setup a \c{.ui} file
14 dynamically using the \l{QUiLoader} class that is part of the
15 \l{Qt UI Tools} library.
16
17 The program allows the user to look up a particular word within the
18 contents of a text. The visual elements and layout of the user interface is
19 loaded at runtime, from a the program resources.
20
21 \table
22 \row \li \inlineimage {textfinder-example-find.webp} {TextFinder find button and input}
23 \li \inlineimage {textfinder-example-find2.webp} {TextFinder Highlighted results}
24 \endtable
25
26 \section1 Setting Up The Resource File
27
28 The resources required for the example are:
29 \list
30 \li \c{textfinder.ui} - the user interface file created in
31 \l{Qt Widgets Designer}
32 \li \c{input.txt} - a text file containing some text to be displayed in
33 a QTextEdit
34 \endlist
35
36 \c textfinder.ui contains all the necessary QWidget objects for the Text
37 Finder. A QLineEdit is used for the user input, a QTextEdit is used to
38 display the contents of \c input.txt, a QLabel is used to display the text
39 "Keyword", and a QPushButton is used for the \uicontrol Find button. Note
40 that all the widgets have sensible \c{objectName}'s assigned. These are
41 used in code to identify them.
42
43 The screenshot below shows the preview obtained in \l{Qt Widgets Designer}.
44
45 \image {doc/images/textfinder-example-userinterface.webp} {Screenshot of TestFinder UI}
46
47 In this example, we store both resources in the applicaton's executable by
48 including the \c{textfinder.qrc} file. Alternatively, the files could also
49 be loaded at runtime from the file system, or from an external binary
50 resource \c{.rcc} file. For more information on resource files, see
51 \l{The Qt Resource System}.
52
53 The \c{textfinder.qrc} file lists all files that should be included as a
54 resource:
55
56 \quotefile textfinder/textfinder.qrc
57
58 To generate a form at run-time, the example is linked against the
59 \l{Qt Ui Tools} library. This is done in the \c{textfinder.pro} file:
60
61 \snippet textfinder/textfinder.pro 0
62
63 \section1 TextFinder Class Definition
64
65 The \c TextFinder class contains the main user interface. It declares
66 pointers to the QPushButton, QTextEdit and QLineEdit elements described
67 above. The QLabel in the user interface is not declared here as we do
68 not need to access it from code.
69
70 \snippet textfinder/textfinder.h 0
71
72 The slot \c{on_findButton_clicked()} is a slot named according to the
73 \l{Using a Designer UI File in Your Application#Automatic Connections}
74 {Automatic Connection} naming convention required
75 by \c uic.
76
77 \section1 Loading the Resources
78
79 We use QFile to load the data from the program resources at runtime. The
80 code for this is in two method methods on top of \c{textfinder.cpp}:
81 \c{loadUiFile} and \c{loadTextFile}.
82
83 The \c{loadUiFile} function loads the user interface file previously
84 created in \l{Qt Widgets Designer}. First, the \c{textfinder.ui}
85 file is located and opened from the resource system.
86 Then a QUiLoader instance is
87 created, and the QUiLoader::load() function is called, with the first
88 argument being the open file, and the second argument being the pointer of
89 the widget that should be set as the parent. The created QWidget is
90 returned.
91
92 \snippet textfinder/textfinder.cpp 4
93
94 In a similar vein, the \c loadTextFile function locates and opens
95 \c{input.txt} from the resources. It then returns the file's content
96 using the QTextStream::readAll() function.
97
98 \snippet textfinder/textfinder.cpp 5
99
100 \section1 TextFinder Class Implementation
101
102 The \c TextFinder class's constructor does not instantiate any child widgets
103 directly. Instead, it calls the \c loadUiFile() function, and then uses
104 QObject::findChild() to locate the created \l{QWidget}s by object name.
105
106 \snippet textfinder/textfinder.cpp 0
107
108 We then use QMetaObject::connectSlotsByName() to enable the automatic
109 calling of the \c on_findButton_clicked() slot.
110
111 \snippet textfinder/textfinder.cpp 2
112
113 The \c loadTextFile function is called to get the text to be shown in the
114 QTextEdit.
115
116 \snippet textfinder/textfinder.cpp 3a
117
118 The dynamically loaded user interface in \c formWidget is now properly set
119 up. We now embed \c formWidget through a \c QVBoxLayout.
120
121 \snippet textfinder/textfinder.cpp 3b
122
123 At the end of the constructor we set a window title.
124
125 \snippet textfinder/textfinder.cpp 3c
126
127 The \c{on_findButton_clicked()} function is a slot that is connected to
128 \c{ui_findButton}'s \c clicked() signal. The \c searchString is extracted
129 from the \c ui_lineEdit and the \c document is extracted from
130 \c ui_textEdit. If there is an empty \c searchString, a QMessageBox is
131 used, requesting the user to enter a word. Otherwise, we traverse through
132 the words in \c ui_textEdit, and highlight all ocurrences of the
133 \c searchString. Two QTextCursor objects are used: One to traverse through
134 the words in \c line and another to keep track of the edit blocks.
135
136 \snippet textfinder/textfinder.cpp 7
137
138 The \c found flag is used to indicate if the \c searchString was found
139 within the contents of \c ui_textEdit. If it was not found, a QMessageBox
140 is used to inform the user.
141
142 \snippet textfinder/textfinder.cpp 9
143
144 \section1 \c main() Function
145
146 The \c main() function instantiates and shows \c TextFinder.
147
148 \snippet textfinder/main.cpp 0
149
150 There are various approaches to include forms into applications. Using
151 \l QUiLoader is just one of them. See \l{Using a Designer UI File in Your
152 Application} for more information on the other approaches available.
153
154 \sa {Calculator Builder}
155 */