source: trunk/doc/src/examples/findfiles.qdoc@ 1168

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 10.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \example dialogs/findfiles
30 \title Find Files Example
31
32 The Find Files example shows how to use QProgressDialog to provide
33 feedback on the progress of a slow operation. The example also
34 shows how to use QFileDialog to facilitate browsing, how to use
35 QTextStream's streaming operators to read a file, and how to use
36 QTableWidget to provide standard table display facilities for
37 applications. In addition, files can be opened using the
38 QDesktopServices class.
39
40 \image findfiles-example.png Screenshot of the Find Files example
41
42 With the Find Files application the user can search for files in a
43 specified directory, matching a specified file name (using wild
44 cards if appropiate) and containing a specified text.
45
46 The user is provided with a \gui Browse option, and the result of
47 the search is displayed in a table with the names of the files
48 found and their sizes. In addition the application provides a
49 total count of the files found.
50
51 \section1 Window Class Definition
52
53 The \c Window class inherits QWidget, and is the main application
54 widget. It shows the search options, and displays the search
55 results.
56
57 \snippet examples/dialogs/findfiles/window.h 0
58
59 We need two private slots: The \c browse() slot is called whenever
60 the user wants to browse for a directory to search in, and the \c
61 find() slot is called whenever the user requests a search to be
62 performed by pressing the \gui Find button.
63
64 In addition we declare several private functions: We use the \c
65 findFiles() function to search for files matching the user's
66 specifications, we call the \c showFiles() function to display the
67 results, and we use \c createButton(), \c createComboBox() and \c
68 createFilesTable() when we are constructing the widget.
69
70 \section1 Window Class Implementation
71
72 In the constructor we first create the application's widgets.
73
74 \snippet examples/dialogs/findfiles/window.cpp 0
75
76 We create the application's buttons using the private \c
77 createButton() function. Then we create the comboboxes associated
78 with the search specifications, using the private \c
79 createComboBox() function. We also create the application's labels
80 before we use the private \c createFilesTable() function to create
81 the table displaying the search results.
82
83 \snippet examples/dialogs/findfiles/window.cpp 1
84
85 Then we add all the widgets to a main layout using QGridLayout. We
86 have, however, put the \c Find and \c Quit buttons and a
87 stretchable space in a separate QHBoxLayout first, to make the
88 buttons appear in the \c Window widget's bottom right corner.
89
90 \snippet examples/dialogs/findfiles/window.cpp 2
91
92 The \c browse() slot presents a file dialog to the user, using the
93 QFileDialog class. QFileDialog enables a user to traverse the file
94 system in order to select one or many files or a directory. The
95 easiest way to create a QFileDialog is to use the convenience
96 static functions.
97
98 Here we use the static QFileDialog::getExistingDirectory()
99 function which returns an existing directory selected by the
100 user. Then we display the directory in the directory combobox
101 using the QComboBox::addItem() function, and updates the current
102 index.
103
104 QComboBox::addItem() adds an item to the combobox with the given
105 text (if it is not already present in the list), and containing
106 the specified userData. The item is appended to the list of
107 existing items.
108
109 \snippet examples/dialogs/findfiles/window.cpp 3
110
111 The \c find() slot is called whenever the user requests a new
112 search by pressing the \gui Find button.
113
114 First we eliminate any previous search results by setting the
115 table widgets row count to zero. Then we retrieve the
116 specified file name, text and directory path from the respective
117 comboboxes.
118
119 \snippet examples/dialogs/findfiles/window.cpp 4
120
121 We use the directory's path to create a QDir; the QDir class
122 provides access to directory structures and their contents. We
123 create a list of the files (contained in the newly created QDir)
124 that match the specified file name. If the file name is empty
125 the list will contain all the files in the directory.
126
127 Then we search through all the files in the list, using the private
128 \c findFiles() function, eliminating the ones that don't contain
129 the specified text. And finally, we display the results using the
130 private \c showFiles() function.
131
132 If the user didn't specify any text, there is no reason to search
133 through the files, and we display the results immediately.
134
135 \image findfiles_progress_dialog.png Screenshot of the Progress Dialog
136
137 \snippet examples/dialogs/findfiles/window.cpp 5
138
139 In the private \c findFiles() function we search through a list of
140 files, looking for the ones that contain a specified text. This
141 can be a very slow operation depending on the number of files as
142 well as their sizes. In case there are a large number of files, or
143 there exists some large files on the list, we provide a
144 QProgressDialog.
145
146 The QProgressDialog class provides feedback on the progress of a
147 slow operation. It is used to give the user an indication of how
148 long an operation is going to take, and to demonstrate that the
149 application has not frozen. It can also give the user an
150 opportunity to abort the operation.
151
152 \snippet examples/dialogs/findfiles/window.cpp 6
153
154 We run through the files, one at a time, and for each file we
155 update the QProgressDialog value. This property holds the current
156 amount of progress made. We also update the progress dialog's
157 label.
158
159 Then we call the QCoreApplication::processEvents() function using
160 the QApplication object. In this way we interleave the display of
161 the progress made with the process of searching through the files
162 so the application doesn't appear to be frozen.
163
164 The QApplication class manages the GUI application's control flow
165 and main settings. It contains the main event loop, where all
166 events from the window system and other sources are processed and
167 dispatched. QApplication inherits QCoreApplication. The
168 QCoreApplication::processEvents() function processes all pending
169 events according to the specified QEventLoop::ProcessEventFlags
170 until there are no more events to process. The default flags are
171 QEventLoop::AllEvents.
172
173 \snippet examples/dialogs/findfiles/window.cpp 7
174
175 After updating the QProgressDialog, we create a QFile using the
176 QDir::absoluteFilePath() function which returns the absolute path
177 name of a file in the directory. We open the file in read-only
178 mode, and read one line at a time using QTextStream.
179
180 The QTextStream class provides a convenient interface for reading
181 and writing text. Using QTextStream's streaming operators, you can
182 conveniently read and write words, lines and numbers.
183
184 For each line we read we check if the QProgressDialog has been
185 canceled. If it has, we abort the operation, otherwise we check if
186 the line contains the specified text. When we find the text within
187 one of the files, we add the file's name to a list of found files
188 that contain the specified text, and start searching a new file.
189
190 Finally, we return the list of the files found.
191
192 \snippet examples/dialogs/findfiles/window.cpp 8
193
194 Both the \c findFiles() and \c showFiles() functions are called from
195 the \c find() slot. In the \c showFiles() function we run through
196 the provided list of file names, adding each file name to the
197 first column in the table widget and retrieving the file's size using
198 QFile and QFileInfo for the second column.
199
200 We also update the total number of files found.
201
202 \snippet examples/dialogs/findfiles/window.cpp 9
203
204 The private \c createButton() function is called from the
205 constructor. We create a QPushButton with the provided text,
206 connect it to the provided slot, and return a pointer to the
207 button.
208
209 \snippet examples/dialogs/findfiles/window.cpp 10
210
211 The private \c createComboBox() function is also called from the
212 contructor. We create a QComboBox with the given text, and make it
213 editable.
214
215 When the user enters a new string in an editable combobox, the
216 widget may or may not insert it, and it can insert it in several
217 locations, depending on the QComboBox::InsertPolicy. The default
218 policy is is QComboBox::InsertAtBottom.
219
220 Then we add the provided text to the combobox, and specify the
221 widget's size policies, before we return a pointer to the
222 combobox.
223
224 \snippet examples/dialogs/findfiles/window.cpp 11
225
226 The private \c createFilesTable() function is called from the
227 constructor. In this function we create the QTableWidget that
228 will display the search results. We set its horizontal headers and
229 their resize mode.
230
231 QTableWidget inherits QTableView which provides a default
232 model/view implementation of a table view. The
233 QTableView::horizontalHeader() function returns the table view's
234 horizontal header as a QHeaderView. The QHeaderView class provides
235 a header row or header column for item views, and the
236 QHeaderView::setResizeMode() function sets the constraints on how
237 the section in the header can be resized.
238
239 Finally, we hide the QTableWidget's vertical headers using the
240 QWidget::hide() function, and remove the default grid drawn for
241 the table using the QTableView::setShowGrid() function.
242
243 \snippet examples/dialogs/findfiles/window.cpp 12
244
245 The \c openFileOfItem() slot is invoked when the user double
246 clicks on a cell in the table. The QDesktopServices::openUrl()
247 knows how to open a file given the file name.
248*/
249
Note: See TracBrowser for help on using the repository browser.