PySide6.QtWidgets.QCompleter¶
- class QCompleter¶
The
QCompleter
class provides completions based on an item model. More…Synopsis¶
Properties¶
caseSensitivityᅟ
- The case sensitivity of the matchingcompletionColumnᅟ
- The column in the model in which completions are searched forcompletionModeᅟ
- How the completions are provided to the usercompletionPrefixᅟ
- The completion prefix used to provide completionscompletionRoleᅟ
- The item role to be used to query the contents of items for matchingfilterModeᅟ
- This property controls how filtering is performedmaxVisibleItemsᅟ
- The maximum allowed size on screen of the completer, measured in itemsmodelSortingᅟ
- The way the model is sortedwrapAroundᅟ
- The completions wrap around when navigating through items
Methods¶
def
__init__()
def
completionMode()
def
completionRole()
def
currentIndex()
def
currentRow()
def
filterMode()
def
model()
def
modelSorting()
def
popup()
def
setCurrentRow()
def
setFilterMode()
def
setModel()
def
setPopup()
def
setWidget()
def
widget()
def
wrapAround()
Virtual methods¶
def
pathFromIndex()
def
splitPath()
Slots¶
def
complete()
def
setWrapAround()
Signals¶
def
activated()
def
highlighted()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
You can use
QCompleter
to provide auto completions in any Qt widget, such asQLineEdit
andQComboBox
. 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 toQCompleter
‘s constructor.)Basic Usage¶
A
QCompleter
is used typically with aQLineEdit
orQComboBox
. For example, here’s how to provide auto completions from a simple word list in aQLineEdit
:wordList = QStringList() wordList << "alpha" << "omega" << "omicron" << "zeta" lineEdit = QLineEdit(self) completer = QCompleter(wordList, self) completer.setCaseSensitivity(Qt.CaseInsensitive) lineEdit.setCompleter(completer)
A QFileSystemModel can be used to provide auto completion of file names. For example:
completer = QCompleter(self) completer.setModel(QFileSystemModel(completer)) lineEdit.setCompleter(completer)
To set the model on which
QCompleter
should operate, callsetModel()
. By default,QCompleter
will attempt to match thecompletion 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 usingsetCompletionRole()
,setCompletionColumn()
, andsetCaseSensitivity()
.If the model is sorted on the column and role that are used for completion, you can call
setModelSorting()
with eitherCaseSensitivelySortedModel
orCaseInsensitivelySortedModel
as the argument. On large models, this can lead to significant performance improvements, becauseQCompleter
can then use binary search instead of linear search. The binary search only works when thefilterMode
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 callcurrentCompletion()
. You can iterate through the list of completions as below:for (int i = 0; completer.setCurrentRow(i); i++) print(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. CallingsetCompletionPrefix()
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 suggestWindows
to complete the current path element. Similarly, if the current text isC:\Windows\Sy
,QCompleter
might suggestSystem
.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. ForC:\Windows\Sy
, it needs to be split as “C:”, “Windows” and “Sy”. The default implementation ofsplitPath()
, splits thecompletionPrefix
using QDir::separator() if the model is a QFileSystemModel.To provide completions,
QCompleter
needs to know the path from an index. This is provided bypathFromIndex()
. The default implementation ofpathFromIndex()
, returns the data for the edit role for list models and the absolute file path if the mode is a QFileSystemModel.- class 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
- class 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
Note
Properties can be used directly when
from __feature__ import true_property
is used or via accessor functions otherwise.- property caseSensitivityᅟ: Qt.CaseSensitivity¶
This property holds the case sensitivity of the matching.
The default value is
Qt::CaseSensitive
.- Access functions:
- property completionColumnᅟ: 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
- Access functions:
- property completionModeᅟ: QCompleter.CompletionMode¶
This property holds how the completions are provided to the user.
The default value is
PopupCompletion
.- Access functions:
- property completionPrefixᅟ: str¶
This property holds the completion prefix used to provide completions..
The
completionModel()
is updated to reflect the list of possible matches forprefix
.- Access functions:
- property 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.
See also
- Access functions:
- property filterModeᅟ: Combination of Qt.MatchFlag¶
This property holds 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 thecaseSensitivity
property to control case sensitivity.The default mode is Qt::MatchStartsWith.
See also
- Access functions:
- property 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.
- Access functions:
- property modelSortingᅟ: QCompleter.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()
andcompletionRole()
is sorted in ascending order, you can set this property toCaseSensitivelySortedModel
orCaseInsensitivelySortedModel
. 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.See also
- Access functions:
- property wrapAroundᅟ: bool¶
This property holds the completions wrap around when navigating through items.
The default is true.
- Access functions:
Constructs a completer object with the given
parent
.- __init__(model[, parent=None])
- Parameters:
model –
QAbstractItemModel
parent –
QObject
Constructs a completer object with the given
parent
that provides completions from the specifiedmodel
.- __init__(completions[, parent=None])
- Parameters:
completions – list of strings
parent –
QObject
Constructs a
QCompleter
object with the givenparent
that uses the specifiedlist
as a source of possible completions.- activated(index)¶
- Parameters:
index –
QModelIndex
This signal is sent when an item in the
popup()
is activated by the user. (by clicking or pressing return). The item’sindex
in thecompletionModel()
is given.- activated(text)
- Parameters:
text – str
This signal is sent when an item in the
popup()
is activated by the user (by clicking or pressing return). The item’stext
is given.- caseSensitivity()¶
- Return type:
See also
Getter of property
caseSensitivityᅟ
.For
PopupCompletion
and QCompletion::UnfilteredPopupCompletion modes, calling this function displays the popup displaying the current completions. By default, ifrect
is not specified, the popup is displayed on the bottom of thewidget()
. Ifrect
is specified the popup is displayed on the left edge of the rectangle.For
InlineCompletion
mode, thehighlighted()
signal is fired with the current completion.- completionColumn()¶
- Return type:
int
See also
Getter of property
completionColumnᅟ
.- 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.- completionMode()¶
- Return type:
See also
Getter of property
completionModeᅟ
.- 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 QAbstractItemModel purely for generality. This actual kind of model returned is an instance of an QAbstractProxyModel subclass.
See also
- completionPrefix()¶
- Return type:
str
See also
Getter of property
completionPrefixᅟ
.- completionRole()¶
- Return type:
int
See also
Getter of property
completionRoleᅟ
.- 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
- currentIndex()¶
- Return type:
Returns the model index of the current completion in the
completionModel()
.See also
- currentRow()¶
- Return type:
int
Returns the current row.
See also
Getter of property
filterModeᅟ
.- highlighted(index)¶
- Parameters:
index –
QModelIndex
This signal is sent when an item in the
popup()
is highlighted by the user. It is also sent ifcomplete()
is called with thecompletionMode()
set toInlineCompletion
. The item’sindex
in thecompletionModel()
is given.- highlighted(text)
- Parameters:
text – str
This signal is sent when an item in the
popup()
is highlighted by the user. It is also sent ifcomplete()
is called with thecompletionMode()
set toInlineCompletion
. The item’stext
is given.- maxVisibleItems()¶
- Return type:
int
See also
Getter of property
maxVisibleItemsᅟ
.- model()¶
- Return type:
Returns the model that provides completion strings.
See also
- modelSorting()¶
- Return type:
See also
Getter of property
modelSortingᅟ
.- pathFromIndex(index)¶
- Parameters:
index –
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 role of the item for list models. It returns the absolute file path if the model is a QFileSystemModel.
See also
- popup()¶
- Return type:
Returns the popup used to display completions.
See also
- setCaseSensitivity(caseSensitivity)¶
- Parameters:
caseSensitivity –
CaseSensitivity
See also
Setter of property
caseSensitivityᅟ
.- setCompletionColumn(column)¶
- Parameters:
column – int
See also
Setter of property
completionColumnᅟ
.- setCompletionMode(mode)¶
- Parameters:
mode –
CompletionMode
See also
Setter of property
completionModeᅟ
.- setCompletionPrefix(prefix)¶
- Parameters:
prefix – str
See also
Setter of property
completionPrefixᅟ
.- setCompletionRole(role)¶
- Parameters:
role – int
See also
Setter of property
completionRoleᅟ
.- setCurrentRow(row)¶
- Parameters:
row – int
- Return type:
bool
Sets the current row to the
row
specified. Returnstrue
if successful; otherwise returnsfalse
.This function may be used along with
currentCompletion()
to iterate through all the possible completions.Setter of property
filterModeᅟ
.- setMaxVisibleItems(maxItems)¶
- Parameters:
maxItems – int
See also
Setter of property
maxVisibleItemsᅟ
.- setModel(c)¶
- Parameters:
Sets the model which provides completions to
model
. Themodel
can be list model or a tree model. If a model has been already previously set and it has theQCompleter
as its parent, it is deleted.For convenience, if
model
is a QFileSystemModel,QCompleter
switches itscaseSensitivity
to Qt::CaseInsensitive on Windows and Qt::CaseSensitive on other platforms.See also
completionModel()
modelSorting
Handling Tree Models
- setModelSorting(sorting)¶
- Parameters:
sorting –
ModelSorting
See also
Setter of property
modelSortingᅟ
.- setPopup(popup)¶
- Parameters:
popup –
QAbstractItemView
Sets the popup used to display completions to
popup
.QCompleter
takes ownership of the view.A
QListView
is automatically created when thecompletionMode()
is set toPopupCompletion
orUnfilteredPopupCompletion
. The default popup displays thecompletionColumn()
.Ensure that this function is called before the view settings are modified. This is required since view’s 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
Sets the widget for which completion are provided for to
widget
. This function is automatically called when aQCompleter
is set on aQLineEdit
usingsetCompleter()
or on aQComboBox
usingsetCompleter()
. The widget needs to be set explicitly when providing completions for custom widgets.See also
- setWrapAround(wrap)¶
- Parameters:
wrap – bool
See also
Setter of property
wrapAroundᅟ
.- splitPath(path)¶
- Parameters:
path – str
- Return type:
list of strings
Splits the given
path
into strings that are used to match at each level in themodel()
.The default implementation of splitPath() splits a file system path based on QDir::separator() when the sourceModel() is a QFileSystemModel.
When used with list models, the first item in the returned list is used for matching.
See also
pathFromIndex()
Handling Tree Models
Returns the widget for which the completer object is providing completions.
See also
- wrapAround()¶
- Return type:
bool
See also
Getter of property
wrapAroundᅟ
.