Home · All Classes · Modules

QCompleter Class Reference
[QtGui module]

The QCompleter class provides completions based on an item model. More...

Inherits QObject.

Types

Methods

Qt Signals


Detailed Description

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

Basic Usage

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:

 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 can be used to provide auto completion of file names. For example:

 QCompleter *completer = new QCompleter(this);
 completer->setModel(new 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 (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 using setCompletionRole(), setCompletionColumn(), and setCaseSensitivity().

If the model is sorted on the column and role that are used for completion, you can call setModelSorting() with 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.

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

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. Calling setCompletionPrefix() 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 suggest Windows to complete the current path element. Similarly, if the current text is C:\Windows\Sy, QCompleter might suggest System.

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 using 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 edit role for list models and the absolute file path if the mode is a QFileSystemModel.


Type Documentation

QCompleter.CompletionMode

This enum specifies how completions are provided to the user.

Constant Value Description
QCompleter.PopupCompletion 0 Current completions are displayed in a popup window.
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.

See also setCompletionMode().

QCompleter.ModelSorting

This enum specifies how the items in the model are sorted.

Constant Value Description
QCompleter.UnsortedModel 0 The model is unsorted.
QCompleter.CaseSensitivelySortedModel 1 The model is sorted case sensitively.
QCompleter.CaseInsensitivelySortedModel 2 The model is sorted case insensitively.

See also setModelSorting().


Method Documentation

QCompleter.__init__ (self, QObject parent = None)

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

Constructs a completer object with the given parent.

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

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

Constructs a completer object with the given parent that provides completions from the specified model.

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

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

Constructs a QCompleter object with the given parent that uses the specified 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(). If rect is specified the popup is displayed on the left edge of the rectangle.

For QCompleter.InlineCompletion mode, the 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. Use setCurrentRow() and 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.

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 and model().

QString QCompleter.completionPrefix (self)

int QCompleter.completionRole (self)

QString QCompleter.currentCompletion (self)

Returns the current completion string. This includes the completionPrefix. When used alongside setCurrentRow(), it can be used to iterate through all the matches.

See also setCurrentRow() and currentIndex().

QModelIndex QCompleter.currentIndex (self)

Returns the model index of the current completion in the completionModel().

See also setCurrentRow(), currentCompletion(), and model().

int QCompleter.currentRow (self)

Returns the current row.

See also setCurrentRow().

bool QCompleter.event (self, QEvent)

Reimplemented from QObject.event().

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

Reimplemented from QObject.eventFilter().

int QCompleter.maxVisibleItems (self)

QAbstractItemModel QCompleter.model (self)

Returns the model that provides completion strings.

See also setModel() and completionModel().

ModelSorting QCompleter.modelSorting (self)

QString QCompleter.pathFromIndex (self, QModelIndex index)

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

QAbstractItemView QCompleter.popup (self)

Returns the popup used to display completions.

See also 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)

Sets the current row to the row specified. Returns true if successful; otherwise returns false.

This function may be used along with currentCompletion() to iterate through all the possible completions.

See also currentRow(), currentCompletion(), and completionCount().

QCompleter.setMaxVisibleItems (self, int maxItems)

QCompleter.setModel (self, QAbstractItemModel c)

Sets the model which provides completions to model. The model can be list model or a tree model. If a model has been already previously set and it has the QCompleter as its parent, it is deleted.

For convenience, if model is a QFileSystemModel, QCompleter switches its caseSensitivity to Qt.CaseInsensitive on Windows and Qt.CaseSensitive on other platforms.

See also completionModel(), modelSorting, and Handling Tree Models.

QCompleter.setModelSorting (self, ModelSorting sorting)

QCompleter.setPopup (self, QAbstractItemView popup)

The popup argument has it's ownership transferred to Qt.

Sets the popup used to display completions to popup. QCompleter takes ownership of the 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).

See also popup().

QCompleter.setWidget (self, QWidget widget)

The widget argument has it's ownership transferred to Qt.

Sets the widget for which completion are provided for to widget. This function is automatically called when a QCompleter is set on a QLineEdit using QLineEdit.setCompleter() or on a QComboBox using QComboBox.setCompleter(). The widget needs to be set explicitly when providing completions for custom widgets.

See also widget(), setModel(), and 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)

Splits the given 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.

See also pathFromIndex() and Handling Tree Models.

QWidget QCompleter.widget (self)

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

See also setWidget().

bool QCompleter.wrapAround (self)


Qt Signal Documentation

void activated (const QString&)

This is the default overload of this signal.

This signal is sent when an item in the popup() is activated by the user (by clicking or pressing return). The item's text is given.

void activated (const ::QModelIndex&)

This signal is sent when an item in the popup() is activated by the user. (by clicking or pressing return). The item's index in the completionModel() is given.

void highlighted (const QString&)

This is the default overload of this signal.

This signal is sent when an item in the popup() is highlighted by the user. It is also sent if complete() is called with the completionMode() set to QCompleter.InlineCompletion. The item's text is given.

void highlighted (const ::QModelIndex&)

This signal is sent when an item in the popup() is highlighted by the user. It is also sent if complete() is called with the completionMode() set to QCompleter.InlineCompletion. The item's index in the completionModel() is given.


PyQt 4.12.1 for X11Copyright © Riverbank Computing Ltd and The Qt Company 2015Qt 4.8.7