QCompleter Class Reference

[ QtGui module]

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

继承 QObject .

类型

方法

Qt Signals


详细描述

The QCompleter class provides completions based on an item model.

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

基本用法

QCompleter 通常用于 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 可以是 used to provide auto completion of file names. For example:

 QCompleter *completer = new QCompleter(this);
 completer->setModel(new QDirModel(completer));
 lineEdit->setCompleter(completer);
			

要设置 QCompleter 应该操作的模型,调用 setModel ()。默认情况下, QCompleter will attempt to match 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 () 采用 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.

模型可以是 list model 表模型 ,或 a 树模型 . Completion on tree models is slightly more involved and is covered in the 处理树模型 section below.

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:

 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.

补全模型

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 . The completion occurs for every element in the path. For example, if the current text is C:\Wind ,QCompleter 可能建议 Windows to complete the current path element. Similarly, if the current text is C:\Windows\Sy ,QCompleter 可能建议 系统 .

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 (),分割 completionPrefix 使用 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 编辑角色 for list models and the absolute file path if the mode is a QFileSystemModel .


类型文档编制

QCompleter.CompletionMode

This enum specifies how completions are provided to the user.

常量 描述
QCompleter.PopupCompletion 0 Current completions are displayed in a popup 窗口。
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.

另请参阅 setCompletionMode ().

QCompleter.ModelSorting

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

常量 描述
QCompleter.UnsortedModel 0 模型不排序。
QCompleter.CaseSensitivelySortedModel 1 模型排序区分大小写。
QCompleter.CaseInsensitivelySortedModel 2 模型排序不区分大小写。

另请参阅 setModelSorting ().


方法文档编制

QCompleter.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

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

QCompleter.__init__ ( self , QAbstractItemModel   model , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

构造补全器对象采用给定 parent that provides completions from the specified model .

QCompleter.__init__ ( self , QStringList  list , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

构造 QCompleter object with the given parent 使用指定 list as a source of possible completions.

Qt.CaseSensitivity QCompleter.caseSensitivity ( self )

QCompleter.complete ( self , QRect   rect  = QRect())

This method is also a Qt slot with the C++ signature void complete(const QRect& = QRect()) .

For QCompleter.PopupCompletion and QCompletion.UnfilteredPopupCompletion modes, calling this function displays the popup displaying the current completions. By default, if rect is not specified, the popup is displayed on the bottom of the widget ()。若 rect is specified the popup is displayed on the left edge of the rectangle.

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

int QCompleter.completionColumn ( self )

int QCompleter.completionCount ( self )

Returns the number of completions for the current prefix. For an unsorted model with a large number of items this can be expensive. 使用 setCurrentRow () 和 currentCompletion () to iterate through all the completions.

CompletionMode QCompleter.completionMode ( self )

QAbstractItemModel QCompleter.completionModel ( self )

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.

注意: 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 子类。

另请参阅 completionPrefix and model ().

QString QCompleter.completionPrefix ( self )

int QCompleter.completionRole ( self )

QString QCompleter.currentCompletion ( self )

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

另请参阅 setCurrentRow () 和 currentIndex ().

QModelIndex QCompleter.currentIndex ( self )

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

另请参阅 setCurrentRow (), currentCompletion (),和 model ().

int QCompleter.currentRow ( self )

返回当前行。

另请参阅 setCurrentRow ().

bool QCompleter.event ( self , QEvent )

重实现自 QObject.event ().

bool QCompleter.eventFilter ( self , QObject   o , QEvent   e )

重实现自 QObject.eventFilter ().

int QCompleter.maxVisibleItems ( self )

QAbstractItemModel QCompleter.model ( self )

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

另请参阅 setModel () 和 completionModel ().

ModelSorting QCompleter.modelSorting ( self )

QString QCompleter.pathFromIndex ( self , QModelIndex   index )

返回路径为给定 index . The completer object uses this to obtain the completion text from the underlying model.

默认实现返回 编辑角色 of the item for list models. It returns the absolute file path if the model is a QFileSystemModel .

另请参阅 splitPath ().

QAbstractItemView QCompleter.popup ( self )

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

另请参阅 setPopup ().

QCompleter.setCaseSensitivity ( self , Qt.CaseSensitivity   caseSensitivity )

QCompleter.setCompletionColumn ( self , int  column )

QCompleter.setCompletionMode ( self , CompletionMode   mode )

QCompleter.setCompletionPrefix ( self , QString  prefix )

This method is also a Qt slot with the C++ signature void setCompletionPrefix(const QString&) .

QCompleter.setCompletionRole ( self , int  role )

bool QCompleter.setCurrentRow ( self , int  row )

把当前行设为 row specified. Returns true if successful; otherwise returns false.

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

另请参阅 currentRow (), currentCompletion (),和 completionCount ().

QCompleter.setMaxVisibleItems ( self , int  maxItems )

QCompleter.setModel ( self , QAbstractItemModel   c )

把提供补全的模型设为 model model can be list model or a tree model. If a model has been already previously set and it has the QCompleter 作为其父级,它将被删除。

为方便起见,若 model QFileSystemModel , QCompleter 切换其 caseSensitivity to Qt.CaseInsensitive on Windows and Qt.CaseSensitive on other 平台。

另请参阅 completionModel (), modelSorting ,和 Handling Tree 模型 .

QCompleter.setModelSorting ( self , ModelSorting   sorting )

QCompleter.setPopup ( self , QAbstractItemView   popup )

popup argument has it's ownership transferred to Qt.

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

A QListView is automatically created when the completionMode () is set to QCompleter.PopupCompletion or QCompleter.UnfilteredPopupCompletion . The default popup displays the 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 ().

QCompleter.setWidget ( self , QWidget   widget )

widget argument has it's ownership transferred to Qt.

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

另请参阅 widget (), setModel (),和 setPopup ().

QCompleter.setWrapAround ( self , bool  wrap )

This method is also a Qt slot with the C++ signature void setWrapAround(bool) .

QStringList QCompleter.splitPath ( self , QString  path )

分割给定 path into strings that are used to match at each level in the model ().

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.

另请参阅 pathFromIndex () 和 Handling Tree 模型 .

QWidget QCompleter.widget ( self )

Returns the widget for which the completer object is providing completions.

另请参阅 setWidget ().

bool QCompleter.wrapAround ( self )


Qt Signal Documentation

void activated (const QString&)

This is the default overload of this signal.

此信号被发送当项在 popup () is activated by the user (by clicking or pressing return). The item's text 已给出。

void activated (const QModelIndex&)

此信号被发送当项在 popup () is activated by the user. (by clicking or pressing return). The item's index completionModel () 是 给定。

void highlighted (const QString&)

This is the default overload of this signal.

此信号被发送当项在 popup () is highlighted by the user. It is also sent if complete () is called with the completionMode () 设为 QCompleter.InlineCompletion . The item's text 已给出。

void highlighted (const QModelIndex&)

此信号被发送当项在 popup () is highlighted by the user. It is also sent if complete () is called with the completionMode () 设为 QCompleter.InlineCompletion . The item's index completionModel () 是 给定。