0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 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 tools/treemodelcompleter
|
|
44 |
\title Tree Model Completer Example
|
|
45 |
|
|
46 |
The Tree Model Completer example shows how to provide completion
|
|
47 |
facilities for a hierarchical model, using a period as the separator
|
|
48 |
to access Child, GrandChild and GrandGrandChild level objects.
|
|
49 |
|
|
50 |
\image treemodelcompleter-example.png
|
|
51 |
|
|
52 |
Similar to the \l{Completer Example}, we provide QComboBox objects to
|
|
53 |
enable selection for completion mode and case sensitivity, as well as
|
|
54 |
a QCheckBox for wrap completions.
|
|
55 |
|
|
56 |
\section1 The Resource File
|
|
57 |
|
|
58 |
The contents of the TreeModelCompleter is read from \e treemodel.txt.
|
|
59 |
This file is embedded within the \e treemodelcompleter.qrc resource file,
|
|
60 |
which contains the following:
|
|
61 |
|
|
62 |
\quotefile examples/tools/treemodelcompleter/treemodelcompleter.qrc
|
|
63 |
|
|
64 |
\section1 TreeModelCompleter Class Definition
|
|
65 |
|
|
66 |
The \c TreeModelCompleter is a subclass of QCompleter with two
|
|
67 |
constructors - one with \a parent as an argument and another with
|
|
68 |
\a parent and \a model as arguments.
|
|
69 |
|
|
70 |
\snippet examples/tools/treemodelcompleter/treemodelcompleter.h 0
|
|
71 |
|
|
72 |
The class reimplements the protected functions
|
|
73 |
\l{QCompleter::splitPath()}{splitPath()} and
|
|
74 |
\l{QCompleter::pathFromIndex()}{pathFromIndex()} to suit a tree model.
|
|
75 |
For more information on customizing QCompleter to suit tree models, refer
|
|
76 |
to \l{QCompleter#Handling Tree Models}{Handling Tree Models}.
|
|
77 |
|
|
78 |
\c TreeModelCompleter also has a separator property which is declared
|
|
79 |
using the Q_PROPERTY() macro. The separator has READ and WRITE attributes
|
|
80 |
and the corresponding functions \c separator() and \c setSeparator(). For
|
|
81 |
more information on Q_PROPERTY(), refer to \l{Qt's Property System}.
|
|
82 |
|
|
83 |
\section1 TreeModelCompleter Class Implementation
|
|
84 |
|
|
85 |
The first constructor constructs a \c TreeModelCompleter object with a
|
|
86 |
parent while the second constructor constructs an object with a parent
|
|
87 |
and a QAbstractItemModel, \a model.
|
|
88 |
|
|
89 |
\snippet examples/tools/treemodelcompleter/treemodelcompleter.cpp 0
|
|
90 |
\codeline
|
|
91 |
\snippet examples/tools/treemodelcompleter/treemodelcompleter.cpp 1
|
|
92 |
|
|
93 |
The \c separator() function is a getter function that returns the
|
|
94 |
separator string.
|
|
95 |
|
|
96 |
\snippet examples/tools/treemodelcompleter/treemodelcompleter.cpp 2
|
|
97 |
|
|
98 |
As mentioned earlier, the \c splitPath() function is reimplemented because
|
|
99 |
the default implementation is more suited to QDirModel or list models. In
|
|
100 |
order for QCompleter to split the path into a list of strings that are
|
|
101 |
matched at each level, we split it using QString::split() with \c sep as its
|
|
102 |
separator.
|
|
103 |
|
|
104 |
\snippet examples/tools/treemodelcompleter/treemodelcompleter.cpp 3
|
|
105 |
|
|
106 |
The \c pathFromIndex() function returns data for the completionRole() for a
|
|
107 |
tree model. This function is reimplemented as its default implementation is
|
|
108 |
more suitable for list models. If there is no separator, we use
|
|
109 |
\l{QCompleter}'s default implementation, otherwise we use the
|
|
110 |
\l{QStringList::prepend()}{prepend()} function to navigate upwards and
|
|
111 |
accumulate the data. The function then returns a QStringList, \c dataList,
|
|
112 |
using a separator to join objects of different levels.
|
|
113 |
|
|
114 |
\snippet examples/tools/treemodelcompleter/treemodelcompleter.cpp 4
|
|
115 |
|
|
116 |
\section1 MainWindow Class Definition
|
|
117 |
|
|
118 |
The \c MainWindow class is a subclass of QMainWindow and implements five
|
|
119 |
custom slots: \c about(), \c changeCase(), \c changeMode(),
|
|
120 |
\c highlight(), and \c updateContentsLabel().
|
|
121 |
|
|
122 |
\snippet examples/tools/treemodelcompleter/mainwindow.h 0
|
|
123 |
|
|
124 |
In addition, the class has two private functions, \c createMenu() and
|
|
125 |
\c modelFromFile(), as well as private instances of QTreeView, QComboBox,
|
|
126 |
QLabel, \c TreeModelCompleter and QLineEdit.
|
|
127 |
|
|
128 |
\snippet examples/tools/treemodelcompleter/mainwindow.h 1
|
|
129 |
|
|
130 |
\section1 MainWindow Class Implementation
|
|
131 |
|
|
132 |
The \c{MainWindow}'s constructor creates a \c MainWindow object with a
|
|
133 |
parent and initializes the \c completer and \c lineEdit. The
|
|
134 |
\c createMenu() function is invoked to set up the "File" menu and "Help"
|
|
135 |
menu. The \c{completer}'s model is set to the QAbstractItemModel obtained
|
|
136 |
from \c modelFromFile(), and the \l{QCompleter::highlighted()}
|
|
137 |
{highlighted()} signal is connected to \c{MainWindow}'s \c highlight()
|
|
138 |
slot.
|
|
139 |
|
|
140 |
\snippet examples/tools/treemodelcompleter/mainwindow.cpp 0
|
|
141 |
|
|
142 |
The QLabel objects \c modelLabel, \c modeLabel and \c caseLabel are
|
|
143 |
instantiated. Also, the QComboBox objects, \c modeCombo and \c caseCombo,
|
|
144 |
are instantiated and populated. By default, the \c{completer}'s mode is
|
|
145 |
"Filtered Popup" and the case is insensitive.
|
|
146 |
|
|
147 |
\snippet examples/tools/treemodelcompleter/mainwindow.cpp 1
|
|
148 |
\codeline
|
|
149 |
\snippet examples/tools/treemodelcompleter/mainwindow.cpp 2
|
|
150 |
|
|
151 |
We use a QGridLayout to place all the objects in the \c MainWindow.
|
|
152 |
|
|
153 |
\snippet examples/tools/treemodelcompleter/mainwindow.cpp 3
|
|
154 |
|
|
155 |
The \c createMenu() function sets up the QAction objects required and
|
|
156 |
adds them to the "File" menu and "Help" menu. The
|
|
157 |
\l{QAction::triggered()}{triggered()} signals from these actions are
|
|
158 |
connected to their respective slots.
|
|
159 |
|
|
160 |
\snippet examples/tools/treemodelcompleter/mainwindow.cpp 4
|
|
161 |
|
|
162 |
The \c changeMode() function accepts an \a index corresponding to the
|
|
163 |
user's choice of completion mode and changes the \c{completer}'s mode
|
|
164 |
accordingly.
|
|
165 |
|
|
166 |
\snippet examples/tools/treemodelcompleter/mainwindow.cpp 5
|
|
167 |
|
|
168 |
The \c about() function provides a brief description on the Tree Model
|
|
169 |
Completer example.
|
|
170 |
|
|
171 |
\snippet examples/tools/treemodelcompleter/mainwindow.cpp 6
|
|
172 |
|
|
173 |
The \c changeCase() function alternates between \l{Qt::CaseSensitive}
|
|
174 |
{Case Sensitive} and \l{Qt::CaseInsensitive}{Case Insensitive} modes,
|
|
175 |
depending on the value of \a cs.
|
|
176 |
|
|
177 |
\snippet examples/tools/treemodelcompleter/mainwindow.cpp 7
|
|
178 |
|
|
179 |
\section1 \c main() Function
|
|
180 |
|
|
181 |
The \c main() function instantiates \c MainWindow and invokes the
|
|
182 |
\l{QWidget::show()}{show()} function to display it.
|
|
183 |
|
|
184 |
\snippet examples/tools/treemodelcompleter/main.cpp 0
|
|
185 |
*/
|