[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[2] | 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
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[2] | 15 | **
|
---|
[846] | 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.
|
---|
[2] | 21 | **
|
---|
[561] | 22 | ** If you have questions regarding the use of this file, please contact
|
---|
| 23 | ** Nokia at [email protected].
|
---|
[2] | 24 | ** $QT_END_LICENSE$
|
---|
| 25 | **
|
---|
| 26 | ****************************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
| 29 | \example tools/completer
|
---|
| 30 | \title Completer Example
|
---|
| 31 |
|
---|
| 32 | The Completer example shows how to provide string-completion facilities
|
---|
| 33 | for an input widget based on data provided by a model.
|
---|
| 34 |
|
---|
| 35 | \image completer-example.png
|
---|
| 36 |
|
---|
[846] | 37 | This example uses a custom item model, \c FileSystemModel, and a QCompleter object.
|
---|
[2] | 38 | QCompleter is a class that provides completions based on an item model. The
|
---|
| 39 | type of model, the completion mode, and the case sensitivity can be
|
---|
| 40 | selected using combo boxes.
|
---|
| 41 |
|
---|
| 42 | \section1 The Resource File
|
---|
| 43 |
|
---|
| 44 | The Completer example requires a resource file in order to store the
|
---|
| 45 | \e{countries.txt} and \e{words.txt}. The resource file contains the
|
---|
| 46 | following code:
|
---|
| 47 |
|
---|
| 48 | \quotefile examples/tools/completer/completer.qrc
|
---|
| 49 |
|
---|
[846] | 50 | \section1 FileSystemModel Class Definition
|
---|
[2] | 51 |
|
---|
[846] | 52 | The \c FileSystemModel class is a subclass of QFileSystemModel, which provides a data
|
---|
[2] | 53 | model for the local filesystem.
|
---|
| 54 |
|
---|
[846] | 55 | \snippet examples/tools/completer/fsmodel.h 0
|
---|
[2] | 56 |
|
---|
| 57 | This class only has a constructor and a \c data() function as it is only
|
---|
| 58 | created to enable \c data() to return the entire file path for the
|
---|
[846] | 59 | display role, unlike \l{QFileSystemModel}'s \c data() function that only returns
|
---|
[2] | 60 | the folder and not the drive label. This is further explained in
|
---|
[846] | 61 | \c FileSystemModel's implementation.
|
---|
[2] | 62 |
|
---|
[846] | 63 | \section1 FileSystemModel Class Implementation
|
---|
[2] | 64 |
|
---|
[846] | 65 | The constructor for the \c FileSystemModel class is used to pass \a parent to
|
---|
| 66 | QFileSystemModel.
|
---|
[2] | 67 |
|
---|
[846] | 68 | \snippet examples/tools/completer/fsmodel.cpp 0
|
---|
[2] | 69 |
|
---|
| 70 | As mentioned earlier, the \c data() function is reimplemented in order to
|
---|
| 71 | get it to return the entire file parth for the display role. For example,
|
---|
[846] | 72 | with a QFileSystemModel, you will see "Program Files" in the view. However, with
|
---|
| 73 | \c FileSystemModel, you will see "C:\\Program Files".
|
---|
[2] | 74 |
|
---|
[846] | 75 | \snippet examples/tools/completer/fsmodel.cpp 1
|
---|
[2] | 76 |
|
---|
| 77 | The screenshots below illustrate this difference:
|
---|
| 78 |
|
---|
| 79 | \table
|
---|
| 80 | \row \o \inlineimage completer-example-qdirmodel.png
|
---|
| 81 | \o \inlineimage completer-example-dirmodel.png
|
---|
| 82 | \endtable
|
---|
| 83 |
|
---|
| 84 | The Qt::EditRole, which QCompleter uses to look for matches, is left
|
---|
| 85 | unchanged.
|
---|
| 86 |
|
---|
| 87 | \section1 MainWindow Class Definition
|
---|
| 88 |
|
---|
[561] | 89 | The \c MainWindow class is a subclass of QMainWindow and implements five
|
---|
| 90 | private slots - \c about(), \c changeCase(), \c changeMode(), \c changeModel(),
|
---|
| 91 | and \c changeMaxVisible().
|
---|
[2] | 92 |
|
---|
| 93 | \snippet examples/tools/completer/mainwindow.h 0
|
---|
| 94 |
|
---|
| 95 | Within the \c MainWindow class, we have two private functions:
|
---|
| 96 | \c createMenu() and \c modelFromFile(). We also declare the private widgets
|
---|
| 97 | needed - three QComboBox objects, a QCheckBox, a QCompleter, a QLabel, and
|
---|
| 98 | a QLineEdit.
|
---|
| 99 |
|
---|
| 100 | \snippet examples/tools/completer/mainwindow.h 1
|
---|
| 101 |
|
---|
| 102 | \section1 MainWindow Class Implementation
|
---|
| 103 |
|
---|
| 104 | The constructor of \c MainWindow constructs a \c MainWindow with a parent
|
---|
| 105 | widget and initializes the private members. The \c createMenu() function
|
---|
| 106 | is then invoked.
|
---|
| 107 |
|
---|
| 108 | We set up three QComboBox objects, \c modelComb, \c modeCombo and
|
---|
[846] | 109 | \c caseCombo. By default, the \c modelCombo is set to QFileSystemModel,
|
---|
[2] | 110 | the \c modeCombo is set to "Filtered Popup" and the \c caseCombo is set
|
---|
| 111 | to "Case Insensitive".
|
---|
| 112 |
|
---|
| 113 | \snippet examples/tools/completer/mainwindow.cpp 0
|
---|
| 114 |
|
---|
[561] | |
---|