QCompleter露
The
QCompleterclass provides completions based on an item model. More鈥

Synopsis露
Functions露
def
caseSensitivity()def
completionColumn()def
completionCount()def
completionMode()def
completionModel()def
completionPrefix()def
completionRole()def
currentCompletion()def
currentIndex()def
currentRow()def
filterMode()def
maxVisibleItems()def
model()def
modelSorting()def
popup()def
setCaseSensitivity(caseSensitivity)def
setCompletionColumn(column)def
setCompletionMode(mode)def
setCompletionRole(role)def
setCurrentRow(row)def
setFilterMode(filterMode)def
setMaxVisibleItems(maxItems)def
setModel(c)def
setModelSorting(sorting)def
setPopup(popup)def
setWidget(widget)def
widget()def
wrapAround()
Virtual functions露
def
pathFromIndex(index)def
splitPath(path)
Slots露
def
complete([rect=QRect()])def
setCompletionPrefix(prefix)def
setWrapAround(wrap)
Signals露
def
activated(index)def
activated(text)def
highlighted(index)def
highlighted(text)
Detailed Description露
You can use
QCompleterto provide auto completions in any Qt widget, such asQLineEditandQComboBox. When the user starts typing a word,QCompletersuggests possible ways of completing the word, based on a word list. The word list is provided as aQAbstractItemModel. (For simple applications, where the word list is static, you can pass aQStringListtoQCompleter鈥榮 constructor.)
Basic Usage露
A
QCompleteris used typically with aQLineEditorQComboBox. For example, here鈥檚 how to provide auto completions from a simple word list in aQLineEdit:wordList = ["alpha", "omega", "omicron", "zeta"] lineEdit = QLineEdit(self) completer = QCompleter(wordList, self) completer.setCaseSensitivity(Qt.CaseInsensitive) lineEdit.setCompleter(completer)A
QFileSystemModelcan be used to provide auto completion of file names. For example:completer = QCompleter(self) completer.setModel(QDirModel(completer)) lineEdit.setCompleter(completer)To set the model on which
QCompletershould operate, callsetModel(). By default,QCompleterwill attempt to match thecompletion prefix(i.e., the word that the user has started typing) against theEditRoledata stored in column 0 in the model case sensitively. This can be changed usingsetCompletionRole(),setCompletionColumn(), andsetCaseSensitivity().If the model is sorted on the column and role that are used for completion, you can call
setModelSorting()with eitherCaseSensitivelySortedModelorCaseInsensitivelySortedModelas the argument. On large models, this can lead to significant performance improvements, becauseQCompletercan then use binary search instead of linear search. The binary search only works when thefilterModeisMatchStartsWith.The model can be a
list model, atable model, or atree model. Completion on tree models is slightly more involved and is covered in theHandling Tree Modelssection 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 callcurrentCompletion(). You can iterate through the list of completions as below:i = 0 while completer.setCurrentRow(i): print "%s is match number %d" % (completer.currentCompletion(), i) i += 1
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. CallingsetCompletionPrefix()automatically refreshes the completion model.
Handling Tree Models露
QCompletercan 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鈥檚 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 isC:\Wind,QCompletermight suggestWindowsto complete the current path element. Similarly, if the current text isC:\Windows\Sy,QCompletermight suggestSystem.For this kind of completion to work,
QCompleterneeds to be able to split the path into a list of strings that are matched at each level. ForC:\Windows\Sy, it needs to be split as 鈥淐:鈥, 鈥淲indows鈥 and 鈥淪y鈥. The default implementation ofsplitPath(), splits thecompletionPrefixusingseparator()if the model is aQFileSystemModel.To provide completions,
QCompleterneeds to know the path from an index. This is provided bypathFromIndex(). The default implementation ofpathFromIndex(), returns the data for theedit rolefor list models and the absolute file path if the mode is aQFileSystemModel.See also
QAbstractItemModelQLineEditQComboBoxCompleter Example
- class PySide2.QtWidgets.QCompleter(model[, parent=None])露
PySide2.QtWidgets.QCompleter([parent=None])
PySide2.QtWidgets.QCompleter(completions[, parent=None])
- param parent:
- param model:
- param completions:
list of strings
Constructs a completer object with the given
parentthat provides completions from the specifiedmodel.Constructs a completer object with the given
parent.
- PySide2.QtWidgets.QCompleter.CompletionMode露
This enum specifies how completions are provided to the user.
Constant
Description
QCompleter.PopupCompletion
Current completions are displayed in a popup window.
QCompleter.InlineCompletion
Completions appear inline (as selected text).
QCompleter.UnfilteredPopupCompletion
All possible completions are displayed in a popup window with the most likely suggestion indicated as current.
See also
- PySide2.QtWidgets.QCompleter.ModelSorting露
This enum specifies how the items in the model are sorted.
Constant
Description
QCompleter.UnsortedModel
The model is unsorted.
QCompleter.CaseSensitivelySortedModel
The model is sorted case sensitively.
QCompleter.CaseInsensitivelySortedModel
The model is sorted case insensitively.
See also
- PySide2.QtWidgets.QCompleter.activated(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex
- PySide2.QtWidgets.QCompleter.activated(text)
- Parameters:
text 鈥 str
- PySide2.QtWidgets.QCompleter.caseSensitivity()露
- Return type:
This property holds the case sensitivity of the matching.
The default value is
Qt::CaseSensitive.
- PySide2.QtWidgets.QCompleter.complete([rect=QRect()])露
- Parameters:
rect 鈥
PySide2.QtCore.QRect
For
PopupCompletionand QCompletion::UnfilteredPopupCompletion modes, calling this function displays the popup displaying the current completions. By default, ifrectis not specified, the popup is displayed on the bottom of thewidget(). Ifrectis specified the popup is displayed on the left edge of the rectangle.For
InlineCompletionmode, thehighlighted()signal is fired with the current completion.
- PySide2.QtWidgets.QCompleter.completionColumn()露
- Return type:
int
This property holds the column in the model in which completions are searched for..
If the
popup()is aQListView, it is automatically setup to display this column.By default, the match column is 0.
See also
- PySide2.QtWidgets.QCompleter.completionCount()露
- Return type:
int
Returns the number of completions for the current prefix. For an unsorted model with a large number of items this can be expensive. Use
setCurrentRow()andcurrentCompletion()to iterate through all the completions.
- PySide2.QtWidgets.QCompleter.completionMode()露
- Return type:
This property holds how the completions are provided to the user.
The default value is
PopupCompletion.
- PySide2.QtWidgets.QCompleter.completionModel()露
- Return type:
Returns the completion model. The completion model is a read-only list model that contains all the possible matches for the current completion prefix. The completion model is auto-updated to reflect the current completions.
Note
The return value of this function is defined to be an
QAbstractItemModelpurely for generality. This actual kind of model returned is an instance of anQAbstractProxyModelsubclass.See also
- PySide2.QtWidgets.QCompleter.completionPrefix()露
- Return type:
str
This property holds the completion prefix used to provide completions..
The
completionModel()is updated to reflect the list of possible matches forprefix.
- PySide2.QtWidgets.QCompleter.completionRole()露
- Return type:
int
This property holds the item role to be used to query the contents of items for matching..
The default role is
EditRole.See also
- PySide2.QtWidgets.QCompleter.currentCompletion()露
- Return type:
str
Returns the current completion string. This includes the
completionPrefix. When used alongsidesetCurrentRow(), it can be used to iterate through all the matches.See also
- PySide2.QtWidgets.QCompleter.currentIndex()露
- Return type:
Returns the model index of the current completion in the
completionModel().See also
- PySide2.QtWidgets.QCompleter.currentRow()露
- Return type:
int
Returns the current row.
See also
- PySide2.QtWidgets.QCompleter.filterMode()露
- Return type:
MatchFlags
This property holds This property controls how filtering is performed..
If is set to
MatchStartsWith, only those entries that start with the typed characters will be displayed.MatchContainswill display the entries that contain the typed characters, andMatchEndsWiththe ones that end with the typed characters.Setting to any other
MatchFlagwill issue a warning, and no action will be performed. Because of this, theQt::MatchCaseSensitiveflag has no effect. Use thecaseSensitivityproperty to control case sensitivity.The default mode is
MatchStartsWith.See also
- PySide2.QtWidgets.QCompleter.highlighted(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex
- PySide2.QtWidgets.QCompleter.highlighted(text)
- Parameters:
text 鈥 str
- PySide2.QtWidgets.QCompleter.maxVisibleItems()露
- Return type:
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.
- PySide2.QtWidgets.QCompleter.model()露
- Return type:
Returns the model that provides completion strings.
See also
- PySide2.QtWidgets.QCompleter.modelSorting()露
- Return type:
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鈥檚 data for the
completionColumn()andcompletionRole()is sorted in ascending order, you can set this property toCaseSensitivelySortedModelorCaseInsensitivelySortedModel. 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鈥檚
caseSensitivityis different to the case sensitivity used by the model鈥檚 when sorting.See also
setCaseSensitivity()ModelSorting
- PySide2.QtWidgets.QCompleter.pathFromIndex(index)露
- Parameters:
index 鈥
PySide2.QtCore.QModelIndex- Return type:
str
Returns the path for the given
index. The completer object uses this to obtain the completion text from the underlying model.The default implementation returns the
edit roleof the item for list models. It returns the absolute file path if the model is aQFileSystemModel.See also
- PySide2.QtWidgets.QCompleter.popup()露
- Return type:
Returns the popup used to display completions.
See also
- PySide2.QtWidgets.QCompleter.setCaseSensitivity(caseSensitivity)露
- Parameters:
caseSensitivity 鈥
CaseSensitivity
This property holds the case sensitivity of the matching.
The default value is
Qt::CaseSensitive.
- PySide2.QtWidgets.QCompleter.setCompletionColumn(column)露
- Parameters:
column 鈥 int
This property holds the column in the model in which completions are searched for..
If the
popup()is aQListView, it is automatically setup to display this column.By default, the match column is 0.
See also
- PySide2.QtWidgets.QCompleter.setCompletionMode(mode)露
- Parameters:
mode 鈥
CompletionMode
This property holds how the completions are provided to the user.
The default value is
PopupCompletion.
- PySide2.QtWidgets.QCompleter.setCompletionPrefix(prefix)露
- Parameters:
prefix 鈥 str
This property holds the completion prefix used to provide completions..
The
completionModel()is updated to reflect the list of possible matches forprefix.
- PySide2.QtWidgets.QCompleter.setCompletionRole(role)露
- Parameters:
role 鈥 int
This property holds the item role to be used to query the contents of items for matching..
The default role is
EditRole.See also
- PySide2.QtWidgets.QCompleter.setCurrentRow(row)露
- Parameters:
row 鈥 int
- Return type:
bool
Sets the current row to the
rowspecified. Returnstrueif successful; otherwise returnsfalse.This function may be used along with
currentCompletion()to iterate through all the possible completions.
- PySide2.QtWidgets.QCompleter.setFilterMode(filterMode)露
- Parameters:
filterMode 鈥
MatchFlags
This property holds This property controls how filtering is performed..
If is set to
MatchStartsWith, only those entries that start with the typed characters will be displayed.MatchContainswill display the entries that contain the typed characters, andMatchEndsWiththe ones that end with the typed characters.Setting to any other
MatchFlagwill issue a warning, and no action will be performed. Because of this, theQt::MatchCaseSensitiveflag has no effect. Use thecaseSensitivityproperty to control case sensitivity.The default mode is
MatchStartsWith.See also
- PySide2.QtWidgets.QCompleter.setMaxVisibleItems(maxItems)露
- Parameters:
maxItems 鈥 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.
- PySide2.QtWidgets.QCompleter.setModel(c)露
- Parameters:
Sets the model which provides completions to
model. Themodelcan be list model or a tree model. If a model has been already previously set and it has theQCompleteras its parent, it is deleted.For convenience, if
modelis aQFileSystemModel,QCompleterswitches itscaseSensitivitytoCaseInsensitiveon Windows andCaseSensitiveon other platforms.See also
completionModel()modelSortingHandling Tree Models
- PySide2.QtWidgets.QCompleter.setModelSorting(sorting)露
- Parameters:
sorting 鈥
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鈥檚 data for the
completionColumn()andcompletionRole()is sorted in ascending order, you can set this property toCaseSensitivelySortedModelorCaseInsensitivelySortedModel. 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鈥檚
caseSensitivityis different to the case sensitivity used by the model鈥檚 when sorting.See also
setCaseSensitivity()ModelSorting
- PySide2.QtWidgets.QCompleter.setPopup(popup)露
- Parameters:
Sets the popup used to display completions to
popup.QCompletertakes ownership of the view.A
QListViewis automatically created when thecompletionMode()is set toPopupCompletionorUnfilteredPopupCompletion. The default popup displays thecompletionColumn().Ensure that this function is called before the view settings are modified. This is required since view鈥檚 properties may require that a model has been set on the view (for example, hiding columns in the view requires a model to be set on the view).
See also
- PySide2.QtWidgets.QCompleter.setWidget(widget)露
- Parameters:
widget 鈥
PySide2.QtWidgets.QWidget
Sets the widget for which completion are provided for to
widget. This function is automatically called when aQCompleteris set on aQLineEditusingsetCompleter()or on aQComboBoxusingsetCompleter(). The widget needs to be set explicitly when providing completions for custom widgets.See also
- PySide2.QtWidgets.QCompleter.setWrapAround(wrap)露
- Parameters:
wrap 鈥 bool
This property holds the completions wrap around when navigating through items.
The default is true.
- PySide2.QtWidgets.QCompleter.splitPath(path)露
- Parameters:
path 鈥 str
- Return type:
list of strings
Splits the given
pathinto strings that are used to match at each level in themodel().The default implementation of splits a file system path based on
separator()when the sourceModel() is aQFileSystemModel.When used with list models, the first item in the returned list is used for matching.
See also
pathFromIndex()Handling Tree Models
- PySide2.QtWidgets.QCompleter.widget()露
- Return type:
Returns the widget for which the completer object is providing completions.
See also
- PySide2.QtWidgets.QCompleter.wrapAround()露
- Return type:
bool
This property holds the completions wrap around when navigating through items.
The default is true.
漏 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.