Skip to content

PythonQt.Qt.QCompleter

QCompleter

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

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 QFileSystemModel(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 binary search only works when the filterMode 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 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.

Source: https://doc.qt.io/archives/qt-5.15/qcompleter.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
CaseInsensitivelySortedModel attribute Any (shared captured text)
CaseSensitivelySortedModel attribute Any (shared captured text)
CompletionMode attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
InlineCompletion attribute Any (shared captured text)
ModelSorting attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
PopupCompletion attribute Any (shared captured text)
UnfilteredPopupCompletion attribute Any (shared captured text)
UnsortedModel attribute Any (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
activated function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.Qt.QCompleter.activated; argument details are not documented.
blockSignals function (self, b: Any) -> bool X.blockSignals(b) -> bool (shared captured text)
caseSensitivity attribute Any This property holds the case sensitivity of the matching (shared captured text)
childEvent function (self, event: Any) -> Any (shared captured text)
children function (self) -> tuple X.children() -> tuple (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
complete function (self, rect: Any) -> None For [QCompleter::PopupCompletion](https://doc.qt.io/archives/qt-5.15/qcompleter.html#CompletionMode-enum) and QCompletion::UnfilteredPopupCompletion modes, calling this function displays the popup… (shared captured text)
completionColumn attribute int This property holds the column in the model in which completions are searched for. (shared captured text)
completionCount function (self) -> int X.completionCount() -> int (shared captured text)
completionMode attribute Any How the completions are provided to the user (shared captured text)
completionModel function (self) -> Any X.completionModel() -> PythonQt.QtCore.QAbstractItemModel (shared captured text)
completionPrefix attribute str This property holds the completion prefix used to provide completions. (shared captured text)
completionRole attribute int This property holds the item role to be used to query the contents of items for matching. (shared captured text)
connect function (self, signal: Any, receiver: Any, slot: Any, type: Any) -> bool X.connect(signal, receiver, slot, type) -> bool (shared captured text)
currentCompletion function (self) -> str X.currentCompletion() -> str (shared captured text)
currentIndex function (self) -> Any X.currentIndex() -> PythonQt.QtCore.QModelIndex (shared captured text)
currentRow function (self) -> int X.currentRow() -> int (shared captured text)
customEvent function (self, event: Any) -> Any (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
deleteLater function (self) -> Any (shared captured text)
destroyed function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.Qt.QCompleter.destroyed; argument details are not documented.
disconnect function (self, signal: Any, receiver: Any, slot: Any) -> bool X.disconnect(signal, receiver, slot) -> bool (shared captured text)
dumpObjectInfo function (self) -> Any (shared captured text)
dumpObjectTree function (self) -> Any (shared captured text)
dynamicPropertyNames function (self) -> Any (shared captured text)
event function (self, arg__1: Any) -> bool X.event(arg__1) -> bool (shared captured text)
eventFilter function (self, o: Any, e: Any) -> bool X.eventFilter(o, e) -> bool (shared captured text)
filterMode attribute Any This property controls how filtering is performed. (shared captured text)
findChild function (self, type: Any, name: Any) -> Any X.findChild(type, name) -> PythonQt.private.QObject (shared captured text)
findChildren function (self, type: Any, name: Any) -> tuple X.findChildren(type, name) -> tuple (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
highlighted function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.Qt.QCompleter.highlighted; argument details are not documented.
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
installEventFilter function (self, filterObj: Any) -> Any (shared captured text)
isSignalConnected function (self, signal: Any) -> bool X.isSignalConnected(signal) -> bool (shared captured text)
isWidgetType function (self) -> bool X.isWidgetType() -> bool (shared captured text)
isWindowType function (self) -> bool X.isWindowType() -> bool (shared captured text)
killTimer function (self, id: Any) -> Any (shared captured text)
maxVisibleItems attribute int This property holds the maximum allowed size on screen of the completer, measured in items (shared captured text)
metaObject function (self) -> Any X.metaObject() -> PythonQt.QtCore.QMetaObject (shared captured text)
model function (self) -> Any X.model() -> PythonQt.QtCore.QAbstractItemModel (shared captured text)
modelSorting attribute Any This property holds the way the model is sorted (shared captured text)
moveToThread function (self, thread: Any) -> Any (shared captured text)
objectName attribute Any (shared captured text)
objectNameChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.Qt.QCompleter.objectNameChanged; argument details are not documented.
parent function (self) -> Any X.parent() -> PythonQt.private.QObject (shared captured text)
pathFromIndex function (self, index: Any) -> str X.pathFromIndex(index) -> str (shared captured text)
popup function (self) -> Any X.popup() -> PythonQt.QtGui.QAbstractItemView (shared captured text)
property function (self, name: Any) -> Any X.property(name) -> object (shared captured text)
removeEventFilter function (self, obj: Any) -> Any (shared captured text)
sender function (self) -> Any X.sender() -> PythonQt.private.QObject (shared captured text)
senderSignalIndex function (self) -> int X.senderSignalIndex() -> int (shared captured text)
setCaseSensitivity function (self, caseSensitivity: Any) -> None (shared captured text)
setCompletionColumn function (self, column: int) -> None (shared captured text)
setCompletionMode function (self, mode: Any) -> None (shared captured text)
setCompletionPrefix function (self, prefix: str) -> None (shared captured text)
setCompletionRole function (self, role: int) -> None (shared captured text)
setCurrentRow function (self, row: int) -> bool X.setCurrentRow(row) -> bool (shared captured text)
setFilterMode function (self, filterMode: Any) -> None (shared captured text)
setMaxVisibleItems function (self, maxItems: int) -> None (shared captured text)
setModel function (self, c: Any) -> None Sets the model which provides completions to model. (shared captured text)
setModelSorting function (self, sorting: Any) -> None (shared captured text)
setObjectName function (self, name: Any) -> Any (shared captured text)
setParent function (self, parent: Any) -> Any (shared captured text)
setPopup function (self, popup: Any) -> None Sets the popup used to display completions to popup. (shared captured text)
setProperty function (self, name: Any, value: Any) -> bool X.setProperty(name, value) -> bool (shared captured text)
setWidget function (self, widget: Any) -> None Sets the widget for which completion are provided for to widget. (shared captured text)
setWrapAround function (self, wrap: bool) -> None (shared captured text)
signalsBlocked function (self) -> bool X.signalsBlocked() -> bool (shared captured text)
splitPath function (self, path: str) -> tuple X.splitPath(path) -> tuple (shared captured text)
startTimer function (self, interval: Any, timerType: Any) -> int X.startTimer(interval, timerType) -> int (shared captured text)
thread function (self) -> Any (shared captured text)
timerEvent function (self, event: Any) -> Any (shared captured text)
tr function (self, text: Any, ambig: Any, n: Any) -> str X.tr(text, ambig, n) -> str (shared captured text)
widget function (self) -> Any X.widget() -> PythonQt.QtGui.QWidget (shared captured text)
wrapAround attribute bool This property holds the completions wrap around when navigating through items (shared captured text)