source: trunk/doc/src/examples/completer.qdoc@ 357

Last change on this file since 357 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 9.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \example tools/completer
44 \title Completer Example
45
46 The Completer example shows how to provide string-completion facilities
47 for an input widget based on data provided by a model.
48
49 \image completer-example.png
50
51 This example uses a custom item model, \c DirModel, and a QCompleter object.
52 QCompleter is a class that provides completions based on an item model. The
53 type of model, the completion mode, and the case sensitivity can be
54 selected using combo boxes.
55
56 \section1 The Resource File
57
58 The Completer example requires a resource file in order to store the
59 \e{countries.txt} and \e{words.txt}. The resource file contains the
60 following code:
61
62 \quotefile examples/tools/completer/completer.qrc
63
64 \section1 DirModel Class Definition
65
66 The \c DirModel class is a subclass of QDirModel, which provides a data
67 model for the local filesystem.
68
69 \snippet examples/tools/completer/dirmodel.h 0
70
71 This class only has a constructor and a \c data() function as it is only
72 created to enable \c data() to return the entire file path for the
73 display role, unlike \l{QDirModel}'s \c data() function that only returns
74 the folder and not the drive label. This is further explained in
75 \c DirModel's implementation.
76
77 \section1 DirModel Class Implementation
78
79 The constructor for the \c DirModel class is used to pass \a parent to
80 QDirModel.
81
82 \snippet examples/tools/completer/dirmodel.cpp 0
83
84 As mentioned earlier, the \c data() function is reimplemented in order to
85 get it to return the entire file parth for the display role. For example,
86 with a QDirModel, you will see "Program Files" in the view. However, with
87 \c DirModel, you will see "C:\\Program Files".
88
89 \snippet examples/tools/completer/dirmodel.cpp 1
90
91 The screenshots below illustrate this difference:
92
93 \table
94 \row \o \inlineimage completer-example-qdirmodel.png
95 \o \inlineimage completer-example-dirmodel.png
96 \endtable
97
98 The Qt::EditRole, which QCompleter uses to look for matches, is left
99 unchanged.
100
101 \section1 MainWindow Class Definition
102
103 The \c MainWindow class is a subclass of QMainWindow and implements four
104 private slots - \c about(), \c changeCase(), \c changeMode(), and
105 \c changeModel().
106
107 \snippet examples/tools/completer/mainwindow.h 0
108
109 Within the \c MainWindow class, we have two private functions:
110 \c createMenu() and \c modelFromFile(). We also declare the private widgets
111 needed - three QComboBox objects, a QCheckBox, a QCompleter, a QLabel, and
112 a QLineEdit.
113
114 \snippet examples/tools/completer/mainwindow.h 1
115
116 \section1 MainWindow Class Implementation
117
118 The constructor of \c MainWindow constructs a \c MainWindow with a parent
119 widget and initializes the private members. The \c createMenu() function
120 is then invoked.
121
122 We set up three QComboBox objects, \c modelComb, \c modeCombo and
123 \c caseCombo. By default, the \c modelCombo is set to QDirModel,
124 the \c modeCombo is set to "Filtered Popup" and the \c caseCombo is set
125 to "Case Insensitive".
126
127 \snippet examples/tools/completer/mainwindow.cpp 0
128
129 The \c wrapCheckBox is then set up. This \c checkBox determines if the
130 \c{completer}'s \l{QCompleter::setWrapAround()}{setWrapAround()} property
131 is enabled or disabled.
132
133 \snippet examples/tools/completer/mainwindow.cpp 1
134
135 We instantiate \c contentsLabel and set its size policy to
136 \l{QSizePolicy::Fixed}{fixed}. The combo boxes' \l{QComboBox::activated()}
137 {activated()} signals are then connected to their respective slots.
138
139 \snippet examples/tools/completer/mainwindow.cpp 2
140
141 The \c lineEdit is set up and then we arrange all the widgets using a
142 QGridLayout. The \c changeModel() function is called, to initialize the
143 \c completer.
144
145 \snippet examples/tools/completer/mainwindow.cpp 3
146
147 The \c createMenu() function is used to instantiate the QAction objects
148 needed to fill the \c fileMenu and \c helpMenu. The actions'
149 \l{QAction::triggered()}{triggered()} signals are connected to their
150 respective slots.
151
152 \snippet examples/tools/completer/mainwindow.cpp 4
153
154 The \c modelFromFile() function accepts the \a fileName of a file and
155 processes it depending on its contents.
156
157 We first validate the \c file to ensure that it can be opened in
158 QFile::ReadOnly mode. If this is unsuccessful, the function returns an
159 empty QStringListModel.
160
161 \snippet examples/tools/completer/mainwindow.cpp 5
162
163 The mouse cursor is then overriden with Qt::WaitCursor before we fill
164 a QStringList object, \c words, with the contents of \c file. Once this
165 is done, we restore the mouse cursor.
166
167 \snippet examples/tools/completer/mainwindow.cpp 6
168
169 As mentioned earlier, the resources file contains two files -
170 \e{countries.txt} and \e{words.txt}. If the \c file read is \e{words.txt},
171 we return a QStringListModel with \c words as its QStringList and
172 \c completer as its parent.
173
174 \snippet examples/tools/completer/mainwindow.cpp 7
175
176 If the \c file read is \e{countries.txt}, then we require a
177 QStandardItemModel with \c words.count() rows, 2 columns, and \c completer
178 as its parent.
179
180 \snippet examples/tools/completer/mainwindow.cpp 8
181
182 A standard line in \e{countries.txt} is:
183 \quotation
184 Norway NO
185 \endquotation
186
187 Hence, to populate the QStandardItemModel object, \c m, we have to
188 split the country name and its symbol. Once this is done, we return
189 \c m.
190
191 \snippet examples/tools/completer/mainwindow.cpp 9
192
193 The \c changeMode() function sets the \c{completer}'s mode, depending on
194 the value of \c index.
195
196 \snippet examples/tools/completer/mainwindow.cpp 10
197
198 The \c changeModel() function changes the item model used based on the
199 model selected by the user.
200
201 A \c switch statement is used to change the item model based on the index
202 of \c modelCombo. If \c case is 0, we use an unsorted QDirModel, providing
203 us with a file path excluding the drive label.
204
205 \snippet examples/tools/completer/mainwindow.cpp 11
206
207 Note that we create the model with \c completer as the parent as this
208 allows us to replace the model with a new model. The \c completer will
209 ensure that the old one is deleted the moment a new model is assigned
210 to it.
211
212 If \c case is 1, we use the \c DirModel we defined earlier, resulting in
213 full paths for the files.
214
215 \snippet examples/tools/completer/mainwindow.cpp 12
216
217 When \c case is 2, we attempt to complete names of countries. This requires
218 a QTreeView object, \c treeView. The country names are extracted from
219 \e{countries.txt} and set the popup used to display completions to
220 \c treeView.
221
222 \snippet examples/tools/completer/mainwindow.cpp 13
223
224 The screenshot below shows the Completer with the country list model.
225
226 \image completer-example-country.png
227
228 If \c case is 3, we attempt to complete words. This is done using a
229 QStringListModel that contains data extracted from \e{words.txt}. The
230 model is sorted \l{QCompleter::CaseInsensitivelySortedModel}
231 {case insensitively}.
232
233 The screenshot below shows the Completer with the word list model.
234
235 \image completer-example-word.png
236
237 Once the model type is selected, we call the \c changeMode() function and
238 the \c changeCase() function and set the wrap option accordingly. The
239 \c{wrapCheckBox}'s \l{QCheckBox::clicked()}{clicked()} signal is connected
240 to the \c{completer}'s \l{QCompleter::setWrapAround()}{setWrapAround()}
241 slot.
242
243 \snippet examples/tools/completer/mainwindow.cpp 14
244
245 The \c about() function provides a brief description about the example.
246
247 \snippet examples/tools/completer/mainwindow.cpp 15
248
249 \section1 \c main() Function
250
251 The \c main() function instantiates QApplication and \c MainWindow and
252 invokes the \l{QWidget::show()}{show()} function.
253
254 \snippet examples/tools/completer/main.cpp 0
255 */
Note: See TracBrowser for help on using the repository browser.