PySide.QtGui.QCompleter class provides completions based on an item model.
可以使用 PySide.QtGui.QCompleter to provide auto completions in any Qt widget, such as PySide.QtGui.QLineEdit and PySide.QtGui.QComboBox . When the user starts typing a word, PySide.QtGui.QCompleter suggests possible ways of completing the word, based on a word list. The word list is provided as a PySide.QtCore.QAbstractItemModel 。(对于单词列表是静态的简单应用程序,可以传递 PySide.QtCore.QStringList to PySide.QtGui.QCompleter ‘s constructor.)
A PySide.QtGui.QCompleter is used typically with a PySide.QtGui.QLineEdit or PySide.QtGui.QComboBox 。例如,这里是如何从简单单词列表提供自动补全在 PySide.QtGui.QLineEdit :
wordList = ["alpha", "omega", "omicron", "zeta"]
lineEdit = QLineEdit(self)
completer = QCompleter(wordList, self)
completer.setCaseSensitivity(Qt.CaseInsensitive)
lineEdit.setCompleter(completer)
A PySide.QtGui.QFileSystemModel 可以被用于提供文件名自动补全。例如:
completer = QCompleter(self)
completer.setModel(QDirModel(completer))
lineEdit.setCompleter(completer)
To set the model on which PySide.QtGui.QCompleter should operate, call PySide.QtGui.QCompleter.setModel() . By default, PySide.QtGui.QCompleter will attempt to match the completion prefix (即:用户已开始键入的单词) 针对 Qt.EditRole 在区分大小写的模型中存储第 0 列数据。这可以被改变使用 PySide.QtGui.QCompleter.setCompletionRole() , PySide.QtGui.QCompleter.setCompletionColumn() ,和 PySide.QtGui.QCompleter.setCaseSensitivity() .
若模型按被用于补全的角色和列进行排序,可以调用 PySide.QtGui.QCompleter.setModelSorting() with either QCompleter.CaseSensitivelySortedModel or QCompleter.CaseInsensitivelySortedModel as the argument. On large models, this can lead to significant performance improvements, because PySide.QtGui.QCompleter can then use binary search instead of linear search.
模型可以是 list model , table model ,或 tree model 。树模型补全稍微更复杂,且涵盖于 Handling Tree 模型 以下章节。
PySide.QtGui.QCompleter.completionMode() determines the mode used to provide completions to the user.
要检索单候选字符串,调用 PySide.QtGui.QCompleter.setCompletionPrefix() with the text that needs to be completed and call PySide.QtGui.QCompleter.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
PySide.QtGui.QCompleter.completionCount() returns the total number of completions for the current prefix. PySide.QtGui.QCompleter.completionCount() should be avoided when possible, since it requires a scan of the entire model.
PySide.QtGui.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.
让我们以用户键入文件系统路径为例。模型是 (分层) PySide.QtGui.QFileSystemModel 。补全发生在每路径元素。例如,若当前文本是 C:\Wind , PySide.QtGui.QCompleter might suggest Windows 去补全当前路径元素。同样,若当前文本是 C:\Windows\Sy , PySide.QtGui.QCompleter might suggest 系统 .
For this kind of completion to work, PySide.QtGui.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 PySide.QtGui.QCompleter.splitPath() , splits the PySide.QtGui.QCompleter.completionPrefix() 使用 QDir.separator() if the model is a PySide.QtGui.QFileSystemModel .
To provide completions, PySide.QtGui.QCompleter needs to know the path from an index. This is provided by PySide.QtGui.QCompleter.pathFromIndex() . The default implementation of PySide.QtGui.QCompleter.pathFromIndex() , returns the data for the edit role 的列表模型和绝对文件路径,若模式为 PySide.QtGui.QFileSystemModel .
| 参数: |
|
|---|
构造补全器对象采用给定 parent 提供补全来自指定 model .
构造补全器对象采用给定 parent .
构造 PySide.QtGui.QCompleter 对象采用给定 parent 使用指定 list 作为可能的补全源。
此枚举指定如何对模型项进行排序。
| 常量 | 描述 |
|---|---|
| QCompleter.UnsortedModel | 模型不排序。 |
| QCompleter.CaseSensitivelySortedModel | 模型排序区分大小写。 |
| QCompleter.CaseInsensitivelySortedModel | 模型排序不区分大小写。 |
此枚举指定如何向用户提供补全。
| 常量 | 描述 |
|---|---|
| QCompleter.PopupCompletion | 当前补全被显示在弹出窗口中。 |
| QCompleter.InlineCompletion | 补全内联出现 (作为选中文本)。 |
| QCompleter.UnfilteredPopupCompletion | 所有可能的补全均被显示在弹出窗口中,采用最有可能建议指示作为当前。 |
| 参数: | text – unicode |
|---|
| 参数: | index – PySide.QtCore.QModelIndex |
|---|
| 返回类型: | PySide.QtCore.Qt.CaseSensitivity |
|---|
This property holds the case sensitivity of the matching.
默认为 Qt.CaseSensitive .
| 参数: | rect – PySide.QtCore.QRect |
|---|
For QCompleter.PopupCompletion 和 QCompletion::UnfilteredPopupCompletion 模式,调用此函数显示当前补全的弹出窗口。默认情况下,若 rect 未指定,弹出窗口显示在底部对于 PySide.QtGui.QCompleter.widget() 。若 rect 被指定,弹出窗口显示在矩形的左边缘。
For QCompleter.InlineCompletion 模式, PySide.QtGui.QCompleter.highlighted() signal is fired with the current completion.
| 返回类型: | PySide.QtCore.int |
|---|
This property holds the column in the model in which completions are searched for..
若 PySide.QtGui.QCompleter.popup() 是 PySide.QtGui.QListView , it is automatically setup to display this column.
默认情况下,匹配列为 0。
| 返回类型: | PySide.QtCore.int |
|---|
返回为当前前缀的补全数。对于具有大量项的未排序模型,这可能是昂贵的。使用 PySide.QtGui.QCompleter.setCurrentRow() and PySide.QtGui.QCompleter.currentCompletion() to iterate through all the completions.
| 返回类型: | PySide.QtGui.QCompleter.CompletionMode |
|---|
This property holds how the completions are provided to the user.
默认值为 QCompleter.PopupCompletion .
| 返回类型: | PySide.QtCore.QAbstractItemModel |
|---|
返回补全模型。补全模型是包含为当前补全前缀的所有可能匹配的只读列表模型。补全模型被自动更新以反映当前补全。
注意
此函数的返回值被定义为 PySide.QtCore.QAbstractItemModel 纯粹为通用性。这种实际返回的模型是实例对于 PySide.QtGui.QAbstractProxyModel 子类。
| 返回类型: | unicode |
|---|
This property holds the completion prefix used to provide completions..
PySide.QtGui.QCompleter.completionModel() is updated to reflect the list of possible matches for prefix .
| 返回类型: | PySide.QtCore.int |
|---|
This property holds the item role to be used to query the contents of items for matching..
默认角色为 Qt.EditRole .
| 返回类型: | unicode |
|---|
返回当前补全字符串。这包括 PySide.QtGui.QCompleter.completionPrefix() 。当同时使用 PySide.QtGui.QCompleter.setCurrentRow() , it can be used to iterate through all the matches.
| 返回类型: | PySide.QtCore.QModelIndex |
|---|
返回当前补全的模型索引在 PySide.QtGui.QCompleter.completionModel() .
| 返回类型: | PySide.QtCore.int |
|---|
返回当前行。
| 参数: | text – unicode |
|---|
| 参数: | index – PySide.QtCore.QModelIndex |
|---|
| 返回类型: | PySide.QtCore.int |
|---|
This property holds the maximum allowed size on screen of the completer, measured in items.
默认情况下,此属性拥有 7 值。
| 返回类型: | PySide.QtCore.QAbstractItemModel |
|---|
返回提供补全字符串的模型。
| 返回类型: | PySide.QtGui.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 PySide.QtGui.QCompleter.completionColumn() and PySide.QtGui.QCompleter.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.
注意
The performance improvements described above cannot take place when the completer's PySide.QtGui.QCompleter.caseSensitivity() is different to the case sensitivity used by the model's when sorting.
另请参阅
PySide.QtGui.QCompleter.setCaseSensitivity() QCompleter.ModelSorting
| 参数: | index – PySide.QtCore.QModelIndex |
|---|---|
| 返回类型: | unicode |
返回路径为给定 index 。补全器对象使用此,以从底层模型中获得补全文本。
默认实现返回 edit role 的列表模型项。它返回绝对文件路径,若模型是 PySide.QtGui.QFileSystemModel .
| 返回类型: | PySide.QtGui.QAbstractItemView |
|---|
返回用于显示补全的弹出窗口。
| 参数: | caseSensitivity – PySide.QtCore.Qt.CaseSensitivity |
|---|
This property holds the case sensitivity of the matching.
默认为 Qt.CaseSensitive .
| 参数: | column – PySide.QtCore.int |
|---|
This property holds the column in the model in which completions are searched for..
若 PySide.QtGui.QCompleter.popup() 是 PySide.QtGui.QListView , it is automatically setup to display this column.
默认情况下,匹配列为 0。
| 参数: | mode – PySide.QtGui.QCompleter.CompletionMode |
|---|
This property holds how the completions are provided to the user.
默认值为 QCompleter.PopupCompletion .
| 参数: | prefix – unicode |
|---|
This property holds the completion prefix used to provide completions..
PySide.QtGui.QCompleter.completionModel() is updated to reflect the list of possible matches for prefix .
| 参数: | role – PySide.QtCore.int |
|---|
This property holds the item role to be used to query the contents of items for matching..
默认角色为 Qt.EditRole .
| 参数: | row – PySide.QtCore.int |
|---|---|
| 返回类型: | PySide.QtCore.bool |
把当前行设为 row specified. Returns true if successful; otherwise returns false.
此函数可以被使用沿着 PySide.QtGui.QCompleter.currentCompletion() to iterate through all the possible completions.
| 参数: | maxItems – PySide.QtCore.int |
|---|
This property holds the maximum allowed size on screen of the completer, measured in items.
默认情况下,此属性拥有 7 值。
| 参数: | c – PySide.QtCore.QAbstractItemModel |
|---|
把提供补全的模型设为 model 。 model 可以是列表模型 (或树模型)。若模型事先已经被设置,且它有 PySide.QtGui.QCompleter 作为其父级,它将被删除。
为方便起见,若 model 是 PySide.QtGui.QFileSystemModel , PySide.QtGui.QCompleter 切换其 PySide.QtGui.QCompleter.caseSensitivity() to Qt.CaseInsensitive 在 Windows 和 Qt.CaseSensitive 在其它平台。
另请参阅
PySide.QtGui.QCompleter.completionModel() PySide.QtGui.QCompleter.modelSorting() Handling Tree 模型
| 参数: | sorting – PySide.QtGui.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 PySide.QtGui.QCompleter.completionColumn() and PySide.QtGui.QCompleter.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.
注意
The performance improvements described above cannot take place when the completer's PySide.QtGui.QCompleter.caseSensitivity() is different to the case sensitivity used by the model's when sorting.
另请参阅
PySide.QtGui.QCompleter.setCaseSensitivity() QCompleter.ModelSorting
| 参数: | popup – PySide.QtGui.QAbstractItemView |
|---|
把用于显示补全的弹出窗口设为 popup . PySide.QtGui.QCompleter 拥有视图的所有权。
A PySide.QtGui.QListView 被自动创建当 PySide.QtGui.QCompleter.completionMode() is set to QCompleter.PopupCompletion or QCompleter.UnfilteredPopupCompletion 。默认弹出窗口显示 PySide.QtGui.QCompleter.completionColumn() .
确保此函数被调用,在视图设置被修改之前。这是必需的,因为视图的特性可能要求在视图上设置模型 (例如:隐藏视图列要求在视图上设置模型)。
| 参数: | widget – PySide.QtGui.QWidget |
|---|
把为其提供补全的小部件设为 widget 。此函数被自动调用当 PySide.QtGui.QCompleter 被设置在 PySide.QtGui.QLineEdit 使用 QLineEdit.setCompleter() or on a PySide.QtGui.QComboBox 使用 QComboBox.setCompleter() . The widget needs to be set explicitly when providing completions for custom widgets.
| 参数: | wrap – PySide.QtCore.bool |
|---|
This property holds the completions wrap around when navigating through items.
默认为 true。
| 参数: | path – unicode |
|---|---|
| 返回类型: | 字符串列表 |
分割给定 path 成字符串,用于每级匹配在 PySide.QtGui.QCompleter.model() .
The default implementation of PySide.QtGui.QCompleter.splitPath() splits a file system path based on QDir.separator() when the sourceModel() is a PySide.QtGui.QFileSystemModel .
当用于列表模型时,返回列表中的第一项被用于匹配。
另请参阅
PySide.QtGui.QCompleter.pathFromIndex() Handling Tree 模型
| 返回类型: | PySide.QtGui.QWidget |
|---|
返回补全器对象正为其提供补全的 Widget。
| 返回类型: | PySide.QtCore.bool |
|---|
This property holds the completions wrap around when navigating through items.
默认为 true。