QCompleter

QCompleter class provides completions based on an item model. 更多

Inheritance diagram of PySide2.QtWidgets.QCompleter

概要

虚函数

信号

详细描述

可以使用 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 。(对于单词列表是静态的简单应用程序,可以传递 QStringList to QCompleter ‘s constructor.)

基本用法

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 :

wordList = ["alpha", "omega", "omicron", "zeta"]
lineEdit = QLineEdit(self)
completer = QCompleter(wordList, self)
completer.setCaseSensitivity(Qt.CaseInsensitive)
lineEdit.setCompleter(completer)
												

A QFileSystemModel 可以被用于提供文件名自动补全。例如:

completer = QCompleter(self)
completer.setModel(QDirModel(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 (即:用户已开始键入的单词) 针对 EditRole 在区分大小写的模型中存储第 0 列数据。这可以被改变使用 setCompletionRole() , setCompletionColumn() ,和 setCaseSensitivity() .

若模型按被用于补全的角色和列进行排序,可以调用 setModelSorting() with either CaseSensitivelySortedModel or 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 MatchStartsWith .

模型可以是 list model table model ,或 tree model 。树模型补全稍微更复杂,且涵盖于 Handling Tree 模型 以下章节。

completionMode() determines the mode used to provide completions to the user.

遍历补全

要检索单候选字符串,调用 setCompletionPrefix() with the text that needs to be completed and call currentCompletion() . 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.

补全模型

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.

处理树模型

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 。补全发生在每路径元素。例如,若当前文本是 C:\Wind , QCompleter might suggest Windows 去补全当前路径元素。同样,若当前文本是 C:\Windows\Sy , QCompleter might suggest 系统 .

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 使用 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 的列表模型和绝对文件路径,若模式为 QFileSystemModel .

class QCompleter ( model [ , parent=None ] )

QCompleter([parent=None])

QCompleter(completions[, parent=None])

param parent

QObject

param model

QAbstractItemModel

param completions

字符串列表

构造补全器对象采用给定 parent 提供补全来自指定 model .

构造补全器对象采用给定 parent .

PySide2.QtWidgets.QCompleter. CompletionMode

此枚举指定如何向用户提供补全。

常量

描述

QCompleter.PopupCompletion

当前补全被显示在弹出窗口中。

QCompleter.InlineCompletion

补全内联出现 (作为选中文本)。

QCompleter.UnfilteredPopupCompletion

所有可能的补全均被显示在弹出窗口中,采用最有可能建议指示作为当前。

PySide2.QtWidgets.QCompleter. ModelSorting

此枚举指定如何对模型项进行排序。

常量

描述

QCompleter.UnsortedModel

模型不排序。

QCompleter.CaseSensitivelySortedModel

模型排序区分大小写。

QCompleter.CaseInsensitivelySortedModel

模型排序不区分大小写。

另请参阅

setModelSorting()

PySide2.QtWidgets.QCompleter. activated ( index )
参数

index QModelIndex

PySide2.QtWidgets.QCompleter. activated ( text )
参数

text – unicode

PySide2.QtWidgets.QCompleter. caseSensitivity ( )
返回类型

CaseSensitivity

PySide2.QtWidgets.QCompleter. complete ( [ rect=QRect() ] )
参数

rect QRect

For PopupCompletion 和 QCompletion::UnfilteredPopupCompletion 模式,调用此函数显示当前补全的弹出窗口。默认情况下,若 rect 未指定,弹出窗口显示在底部对于 widget() 。若 rect 被指定,弹出窗口显示在矩形的左边缘。

For InlineCompletion 模式, highlighted() signal is fired with the current completion.

PySide2.QtWidgets.QCompleter. completionColumn ( )
返回类型

int

PySide2.QtWidgets.QCompleter. completionCount ( )
返回类型

int

返回为当前前缀的补全数。对于具有大量项的未排序模型,这可能是昂贵的。使用 setCurrentRow() and currentCompletion() to iterate through all the completions.

PySide2.QtWidgets.QCompleter. completionMode ( )
返回类型

CompletionMode

PySide2.QtWidgets.QCompleter. completionModel ( )
返回类型

QAbstractItemModel

返回补全模型。补全模型是包含为当前补全前缀的所有可能匹配的只读列表模型。补全模型被自动更新以反映当前补全。

注意

此函数的返回值被定义为 QAbstractItemModel 纯粹为通用性。这种实际返回的模型是实例对于 QAbstractProxyModel 子类。

PySide2.QtWidgets.QCompleter. completionPrefix ( )
返回类型

unicode

PySide2.QtWidgets.QCompleter. completionRole ( )
返回类型

int

PySide2.QtWidgets.QCompleter. currentCompletion ( )
返回类型

unicode

返回当前补全字符串。这包括 completionPrefix 。当同时使用 setCurrentRow() , it can be used to iterate through all the matches.

PySide2.QtWidgets.QCompleter. currentIndex ( )
返回类型

QModelIndex

返回当前补全的模型索引在 completionModel() .

PySide2.QtWidgets.QCompleter. currentRow ( )
返回类型

int

返回当前行。

另请参阅

setCurrentRow()

PySide2.QtWidgets.QCompleter. filterMode ( )
返回类型

MatchFlags

另请参阅

setFilterMode()

PySide2.QtWidgets.QCompleter. highlighted ( index )
参数

index QModelIndex

PySide2.QtWidgets.QCompleter. highlighted ( text )
参数

text – unicode

PySide2.QtWidgets.QCompleter. maxVisibleItems ( )
返回类型

int

PySide2.QtWidgets.QCompleter. model ( )
返回类型

QAbstractItemModel

返回提供补全字符串的模型。

PySide2.QtWidgets.QCompleter. modelSorting ( )
返回类型

ModelSorting

另请参阅

setModelSorting()

PySide2.QtWidgets.QCompleter. pathFromIndex ( index )
参数

index QModelIndex

返回类型

unicode

返回路径为给定 index 。补全器对象使用此,以从底层模型中获得补全文本。

默认实现返回 edit role 的列表模型项。它返回绝对文件路径,若模型是 QFileSystemModel .

另请参阅

splitPath()

PySide2.QtWidgets.QCompleter. popup ( )
返回类型

QAbstractItemView

返回用于显示补全的弹出窗口。

另请参阅

setPopup()

PySide2.QtWidgets.QCompleter. setCaseSensitivity ( caseSensitivity )
参数

caseSensitivity CaseSensitivity

另请参阅

caseSensitivity()

PySide2.QtWidgets.QCompleter. setCompletionColumn ( column )
参数

column int

PySide2.QtWidgets.QCompleter. setCompletionMode ( mode )
参数

mode CompletionMode

另请参阅

completionMode()

PySide2.QtWidgets.QCompleter. setCompletionPrefix ( prefix )
参数

prefix – unicode

PySide2.QtWidgets.QCompleter. setCompletionRole ( role )
参数

role int

另请参阅

completionRole()

PySide2.QtWidgets.QCompleter. setCurrentRow ( row )
参数

row int

返回类型

bool

把当前行设为 row 指定。返回 true 若成功;否则返回 false .

此函数可以被使用沿着 currentCompletion() to iterate through all the possible completions.

PySide2.QtWidgets.QCompleter. setFilterMode ( filterMode )
参数

filterMode MatchFlags

另请参阅

filterMode()

PySide2.QtWidgets.QCompleter. setMaxVisibleItems ( maxItems )
参数

maxItems int

另请参阅

maxVisibleItems()

PySide2.QtWidgets.QCompleter. setModel ( c )
参数

c QAbstractItemModel

把提供补全的模型设为 model model 可以是列表模型 (或树模型)。若模型事先已经被设置,且它有 QCompleter 作为其父级,它将被删除。

为方便起见,若 model QFileSystemModel , QCompleter 切换其 caseSensitivity to CaseInsensitive 在 Windows 和 CaseSensitive 在其它平台。

另请参阅

completionModel() modelSorting Handling Tree 模型

PySide2.QtWidgets.QCompleter. setModelSorting ( sorting )
参数

sorting ModelSorting

另请参阅

modelSorting()

PySide2.QtWidgets.QCompleter. setPopup ( popup )
参数

popup QAbstractItemView

把用于显示补全的弹出窗口设为 popup . QCompleter 拥有视图的所有权。

A QListView 被自动创建当 completionMode() is set to PopupCompletion or UnfilteredPopupCompletion 。默认弹出窗口显示 completionColumn() .

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).

另请参阅

popup()

PySide2.QtWidgets.QCompleter. setWidget ( widget )
参数

widget QWidget

把为其提供补全的小部件设为 widget 。此函数被自动调用当 QCompleter 被设置在 QLineEdit 使用 setCompleter() or on a QComboBox 使用 setCompleter() . The widget needs to be set explicitly when providing completions for custom widgets.

PySide2.QtWidgets.QCompleter. setWrapAround ( wrap )
参数

wrap bool

另请参阅

wrapAround()

PySide2.QtWidgets.QCompleter. splitPath ( path )
参数

path – unicode

返回类型

字符串列表

分割给定 path 成字符串,用于每级匹配在 model() .

The default implementation of splits a file system path based on separator() when the sourceModel() is a QFileSystemModel .

当用于列表模型时,返回列表中的第一项被用于匹配。

另请参阅

pathFromIndex() Handling Tree 模型

PySide2.QtWidgets.QCompleter. widget ( )
返回类型

QWidget

返回补全器对象正为其提供补全的 Widget。

另请参阅

setWidget()

PySide2.QtWidgets.QCompleter. wrapAround ( )
返回类型

bool

另请参阅

setWrapAround()