source: trunk/doc/src/examples/customcompleter.qdoc@ 5

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

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

File size: 8.2 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/customcompleter
44 \title Custom Completer Example
45
46 The Custom Completer example shows how to provide string-completion
47 facilities for an input widget based on data provided by a model. The
48 completer pops up suggestions for possible words based on the first three
49 characters input by the user and the user's choice of word is inserted
50 into the \c TextEdit using QTextCursor.
51
52 \image customcompleter-example.png
53
54 \section1 Setting Up The Resource File
55
56 The Custom Completer example requires a resource file, \e wordlist.txt,
57 that has a list of words to help QCompleter complete words. This file
58 contains the following:
59
60 \quotefile examples/tools/customcompleter/customcompleter.qrc
61
62 \section1 TextEdit Class Definition
63
64 The \c TextEdit class is a subclass of QTextEdit with a custom
65 \c insertCompletion() slot and it reimplements the
66 \l{QAbstractScrollArea::keyPressEvent()}{keyPressEvent()} and the
67 \l{QWidget::focusInEvent()}{focusInEvent()} functions. \c TextEdit also
68 contains a private function \c textUnderCursor() and a private instance
69 of QCompleter, \c c.
70
71 \snippet examples/tools/customcompleter/textedit.h 0
72
73 \section1 TextEdit Class Implementation
74
75 The constructor for \c TextEdit constructs a \c TextEdit with a parent and
76 initializes \c c. The instructions to use the completer is displayed on
77 the \c TextEdit object, using the
78 \l{QTextEdit::setPlainText()}{setPlainText()} function.
79
80 \snippet examples/tools/customcompleter/textedit.cpp 0
81
82 In addition, \c TextEdit also includes a default destructor:
83
84 \snippet examples/tools/customcompleter/textedit.cpp 1
85
86 The \c setCompleter() function accepts a \a completer and sets it up.
87 We use \c{if (c)} to check if \c c has been initialized. If it has been
88 initialized, the QObject::disconnect() function is invoked to disconnect
89 the signal from the slot. This is to ensure that no previous completer
90 object is still connected to the slot.
91
92 \snippet examples/tools/customcompleter/textedit.cpp 2
93
94 We then instantiate \c c with \a completer and set it as \c{TextEdit}'s
95 widget. The completion mode and case sensitivity are also set and then
96 we connect the \l{QCompleter::activated()}{activated()} signal to the
97 \c insertCompletion() slot.
98
99 The \c completer() function is a getter function that returns \c c.
100
101 \snippet examples/tools/customcompleter/textedit.cpp 3
102
103 The completer pops up the options available, based on the contents of
104 \e wordlist.txt, but the text cursor is responsible for filling in the
105 missing characters, according to the user's choice of word.
106
107 Suppose the user inputs "ACT" and accepts the completer's suggestion of
108 "ACTUAL". The \c completion string is then sent to \c insertCompletion()
109 by the completer's \l{QCompleter::activated()}{activated()} signal.
110
111 The \c insertCompletion() function is responsible for completing the word
112 using a QTextCursor object, \c tc. It validates to ensure that the
113 completer's widget is \c TextEdit before using \c tc to insert the extra
114 characters to complete the word.
115
116 \snippet examples/tools/customcompleter/textedit.cpp 4
117
118 The figure below illustrates this process:
119
120 \image customcompleter-insertcompletion.png
121
122 \c{completion.length()} = 6
123
124 \c{c->completionPrefix().length()}=3
125
126 The difference between these two values is \c extra, which is 3. This
127 means that the last three characters from the right, "U", "A", and "L",
128 will be inserted by \c tc.
129
130 The \c textUnderCursor() function uses a QTextCursor, \c tc, to select a
131 word under the cursor and return it.
132
133 \snippet examples/tools/customcompleter/textedit.cpp 5
134
135 The \c TextEdit class reimplements \l{QWidget::focusInEvent()}
136 {focusInEvent()} function, which is an event handler used to receive
137 keyboard focus events for the widget.
138
139 \snippet examples/tools/customcompleter/textedit.cpp 6
140
141 The \l{QAbstractScrollArea::keyPressEvent()}{keyPressEvent()} is
142 reimplemented to ignore key events like Qt::Key_Enter, Qt::Key_Return,
143 Qt::Key_Escape, Qt::Key_Tab, and Qt::Key_Backtab so the completer can
144 handle them.
145
146 If there is an active completer, we cannot process the shortcut, Ctrl+E.
147
148 \snippet examples/tools/customcompleter/textedit.cpp 7
149
150 We also handle other modifiers and shortcuts for which we do not want the
151 completer to respond to.
152
153 \snippet examples/tools/customcompleter/textedit.cpp 8
154
155 Finally, we pop up the completer.
156
157 \section1 MainWindow Class Definition
158
159 The \c MainWindow class is a subclass of QMainWindow and implements a
160 private slot, \c about(). This class also has two private functions,
161 \c createMenu() and \c modelFromFile() as well as private instances of
162 QCompleter and \c TextEdit.
163
164 \snippet examples/tools/customcompleter/mainwindow.h 0
165
166 \section1 MainWindow Class Implementation
167
168 The constructor constructs a \c MainWindow with a parent and initializes
169 the \c completer. It also instantiates a \c TextEdit and sets its
170 completer. A QStringListModel, obtained from \c modelFromFile(), is used
171 to populate the \c completer. The \c{MainWindow}'s central widget is set
172 to \c TextEdit and its size is set to 500 x 300.
173
174 \snippet examples/tools/customcompleter/mainwindow.cpp 0
175
176 The \c createMenu() function creates the necessary QAction objects needed
177 for the "File" and "Help" menu and their \l{QAction::triggered()}
178 {triggered()} signals are connected to the \c quit(), \c about(), and
179 \c aboutQt() slots respectively.
180
181 \snippet examples/tools/customcompleter/mainwindow.cpp 1
182
183 The \c modelFromFile() function accepts a \a fileName and attempts to
184 extract the contents of this file into a QStringListModel. We display the
185 Qt::WaitCursor when we are populating the QStringList, \c words, and
186 restore the mouse cursor when we are done.
187
188 \snippet examples/tools/customcompleter/mainwindow.cpp 2
189
190 The \c about() function provides a brief description about the Custom
191 Completer example.
192
193 \snippet examples/tools/customcompleter/mainwindow.cpp 3
194
195 \section1 \c main() Function
196
197 The \c main() function instantiates \c MainWindow and invokes the
198 \l{QWidget::show()}{show()} function.
199
200 \snippet examples/tools/customcompleter/main.cpp 0
201*/
Note: See TracBrowser for help on using the repository browser.