QCompleter Class
The QCompleter class provides completions based on an item model. More...
| Header: | #include <QCompleter> |
| qmake: | QT += widgets |
| Since: | Qt 4.2 |
| Inherits: | QObject |
This class was introduced in Qt 4.2.
Public Types
| enum | CompletionMode { PopupCompletion, InlineCompletion, UnfilteredPopupCompletion } |
| enum | ModelSorting { UnsortedModel, CaseSensitivelySortedModel, CaseInsensitivelySortedModel } |
Properties
|
|
Public Functions
| Qt::CaseSensitivity | caseSensitivity() const |
| int | completionColumn() const |
| QCompleter::CompletionMode | completionMode() const |
| QString | completionPrefix() const |
| int | completionRole() const |
| Qt::MatchFlags | filterMode() const |
| int | maxVisibleItems() const |
| QCompleter::ModelSorting | modelSorting() const |
| void | setCaseSensitivity(Qt::CaseSensitivity caseSensitivity) |
| void | setCompletionColumn(int column) |
| void | setCompletionMode(QCompleter::CompletionMode mode) |
| void | setCompletionRole(int role) |
| void | setFilterMode(Qt::MatchFlags filterMode) |
| void | setMaxVisibleItems(int maxItems) |
| void | setModelSorting(QCompleter::ModelSorting sorting) |
| bool | wrapAround() const |
Public Slots
| void | setCompletionPrefix(const QString &prefix) |
| void | setWrapAround(bool wrap) |
Detailed Description
You can use QCompleter to provide auto completions in any Qt widget, such as QLineEdit and QComboBox. When the user starts typing a word, QCompleter suggests possible ways of completing the word, based on a word list. The word list is provided as a QAbstractItemModel. (For simple applications, where the word list is static, you can pass a QStringList to QCompleter's constructor.)
Basic Usage
A QCompleter is used typically with a QLineEdit or QComboBox. For example, here's how to provide auto completions from a simple word list in a QLineEdit:
QStringList wordList; wordList << "alpha" << "omega" << "omicron" << "zeta"; QLineEdit *lineEdit = new QLineEdit(this); QCompleter *completer = new QCompleter(wordList, this); completer->setCaseSensitivity(Qt::CaseInsensitive); lineEdit->setCompleter(completer);
A QFileSystemModel can be used to provide auto completion of file names. For example:
QCompleter *completer = new QCompleter(this); completer->setModel(new QFileSystemModel(completer)); lineEdit->setCompleter(completer);
To set the model on which QCompleter should operate, call setModel(). By default, QCompleter will attempt to match the completion prefix (i.e., the word that the user has started typing) against the Qt::EditRole data stored in column 0 in the model case sensitively. This can be changed using setCompletionRole(), setCompletionColumn(), and setCaseSensitivity().
If the model is sorted on the column and role that are used for completion, you can call setModelSorting() with either QCompleter::CaseSensitivelySortedModel or QCompleter::CaseInsensitivelySortedModel as the argument. On large models, this can lead to significant performance improvements, because QCompleter can then use binary search instead of linear search. The binary search only works when the filterMode is Qt::MatchStartsWith.
The model can be a list model, a table model, or a tree model. Completion on tree models is slightly more involved and is covered in the Handling Tree Models section below.
The completionMode() determines the mode used to provide completions to the user.
Iterating Through Completions
To retrieve a single candidate string, call setCompletionPrefix() with the text that needs to be completed and call currentCompletion(). You can iterate through the list of completions as below:
for (int i = 0; completer->setCurrentRow(i); i++) qDebug() << completer->currentCompletion() << " is match number " << i;
completionCount() returns the total number of completions for the current prefix. completionCount() should be avoided when possible, since it requires a scan of the entire model.
The Completion Model
completionModel() return a list model that contains all possible completions for the current completion prefix, in the order in which they appear in the model. This model can be used to display the current completions in a custom view. Calling setCompletionPrefix() automatically refreshes the completion model.
Handling Tree Models
QCompleter can look for completions in tree models, assuming that any item (or sub-item or sub-sub-item) can be unambiguously represented as a string by specifying the path to the item. The completion is then performed one level at a time.
Let's take the example of a user typing in a file system path. The model is a (hierarchical) QFileSystemModel. The completion occurs for every element in the path. For example, if the current text is C:\Wind, QCompleter might suggest Windows to complete the current path element. Similarly, if the current text is C:\Windows\Sy, QCompleter might suggest System.
For this kind of completion to work, QCompleter needs to be able to split the path into a list of strings that are matched at each level. For C:\Windows\Sy, it needs to be split as "C:", "Windows" and "Sy". The default implementation of splitPath(), splits the completionPrefix using QDir::separator() if the model is a QFileSystemModel.
To provide completions, QCompleter needs to know the path from an index. This is provided by pathFromIndex(). The default implementation of pathFromIndex(), returns the data for the edit role for list models and the absolute file path if the mode is a QFileSystemModel.
See also QAbstractItemModel, QLineEdit, QComboBox, and Completer Example.
Member Type Documentation
enum QCompleter::CompletionMode
This enum specifies how completions are provided to the user.
| Constant | Value | Description |
|---|---|---|
QCompleter::PopupCompletion | 0 | Current completions are displayed in a popup window. |
QCompleter::InlineCompletion | 2 | Completions appear inline (as selected text). |
QCompleter::UnfilteredPopupCompletion | 1 | All possible completions are displayed in a popup window with the most likely suggestion indicated as current. |
See also setCompletionMode().
enum QCompleter::ModelSorting
This enum specifies how the items in the model are sorted.
| Constant | Value | Description |
|---|---|---|
QCompleter::UnsortedModel | 0 | The model is unsorted. |
QCompleter::CaseSensitivelySortedModel | 1 | The model is sorted case sensitively. |
QCompleter::CaseInsensitivelySortedModel | 2 | The model is sorted case insensitively. |
See also setModelSorting().
Property Documentation
caseSensitivity : Qt::CaseSensitivity
This property holds the case sensitivity of the matching
The default value is Qt::CaseSensitive.
Access functions:
| Qt::CaseSensitivity | caseSensitivity() const |
| void | setCaseSensitivity(Qt::CaseSensitivity caseSensitivity) |
See also completionColumn, completionRole, modelSorting, and filterMode.
completionColumn : int
This property holds the column in the model in which completions are searched for.
If the popup() is a QListView, it is automatically setup to display this column.
By default, the match column is 0.
Access functions:
| int | completionColumn() const |
| void | setCompletionColumn(int column) |
See also completionRole and caseSensitivity.
completionMode : CompletionMode
how the completions are provided to the user
The default value is QCompleter::PopupCompletion.
Access functions:
| QCompleter::CompletionMode | completionMode() const |
| void | setCompletionMode(QCompleter::CompletionMode mode) |
completionPrefix : QString
This property holds the completion prefix used to provide completions.
The completionModel() is updated to reflect the list of possible matches for prefix.
Access functions:
| QString | completionPrefix() const |
| void | setCompletionPrefix(const QString &prefix) |
completionRole : int
This property holds the item role to be used to query the contents of items for matching.
The default role is Qt::EditRole.
Access functions:
| int | completionRole() const |
| void | setCompletionRole(int role) |
See also completionColumn and caseSensitivity.
filterMode : Qt::MatchFlags
This property controls how filtering is performed.
If filterMode is set to Qt::MatchStartsWith, only those entries that start with the typed characters will be displayed. Qt::MatchContains will display the entries that contain the typed characters, and Qt::MatchEndsWith the ones that end with the typed characters.
Setting filterMode to any other Qt::MatchFlag will issue a warning, and no action will be performed. Because of this, the Qt::MatchCaseSensitive flag has no effect. Use the caseSensitivity property to control case sensitivity.
The default mode is Qt::MatchStartsWith.
This property was introduced in Qt 5.2.
Access functions:
| Qt::MatchFlags | filterMode() const |
| void | setFilterMode(Qt::MatchFlags filterMode) |
See also caseSensitivity.
maxVisibleItems : int
This property holds the maximum allowed size on screen of the completer, measured in items
By default, this property has a value of 7.
This property was introduced in Qt 4.6.
Access functions:
| int | maxVisibleItems() const |
| void | setMaxVisibleItems(int maxItems) |
modelSorting : ModelSorting
This property holds the way the model is sorted
By default, no assumptions are made about the order of the items in the model that provides the completions.
If the model's data for the completionColumn() and completionRole() is sorted in ascending order, you can set this property to CaseSensitivelySortedModel or CaseInsensitivelySortedModel. On large models, this can lead to significant performance improvements because the completer object can then use a binary search algorithm instead of linear search algorithm.
The sort order (i.e ascending or descending order) of the model is determined dynamically by inspecting the contents of the model.
Note: The performance improvements described above cannot take place when the completer's caseSensitivity is different to the case sensitivity used by the model's when sorting.
Access functions:
| QCompleter::ModelSorting | modelSorting() const |
| void | setModelSorting(QCompleter::ModelSorting sorting) |
See also setCaseSensitivity() and QCompleter::ModelSorting.
wrapAround : bool
This property holds the completions wrap around when navigating through items
The default is true.
This property was introduced in Qt 4.3.
Access functions:
| bool | wrapAround() const |
| void | setWrapAround(bool wrap) |