6.0.0-rc4Source

QCompleter Class

The QCompleter class completes what the user types from a list of words. More…

Header#include <QCompleter>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQObject

List of all members, including inherited members

Public Types

Public Functions

QCompleter(QObject *parent = nullptr)
QCompleter(const QStringList &completions, QObject *parent = nullptr)
~QCompleter()
voidsetWidget(QWidget *widget)
QWidget *widget() const
voidsetCompletionMode(CompletionMode mode)
CompletionModecompletionMode() const
voidsetFilterMode(Qt::MatchFlags filterMode)
Qt::MatchFlagsfilterMode() const
voidsetCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
Qt::CaseSensitivitycaseSensitivity() const
voidsetModelSorting(ModelSorting sorting)
ModelSortingmodelSorting() const
voidsetMaxVisibleItems(int maxItems)
intmaxVisibleItems() const
voidsetWrapAround(bool wrap)
boolwrapAround() const
QStringcompletionPrefix() const
intcompletionCount() const
boolsetCurrentRow(int row)
intcurrentRow() const
QStringcurrentCompletion() const
QAbstractItemView *popup() const

Public Slots

voidsetCompletionPrefix(const QString &prefix)
voidcomplete(const QRect &rect = QRect())

Signals

voidactivated(const QString &text)
voidhighlighted(const QString &text)

Detailed Description

Give a line edit a completer, and as the user types it shows the words that start with the text in a list under the line edit. The arrow keys move in the list, and Return or a click takes the word:

auto *completer = new QCompleter(QStringList{"apple", "apricot", "banana"}, edit);
completer->setCaseSensitivity(Qt::CaseInsensitive);
edit->setCompleter(completer);

In InlineCompletion mode the rest of the first matching word is put after the text, selected, so that typing on replaces it. The filter mode chooses whether the words must start with the text, contain it, or end with it.

See also QLineEdit::setCompleter().

Differences from Qt

The words come from the QStringList given to the constructor: there is no setModel(), model(), completionModel(), setCompletionColumn() or setCompletionRole(), and setModelSorting() is recorded only. The popup is a list inside the window, not a window of its own, and setPopup() is missing. Only QLineEdit uses a completer by itself.

Member Type Documentation

enum QCompleter::CompletionMode

How the completions are offered.

ConstantDescription
QCompleter::PopupCompletionThe words that match are shown in a list.
QCompleter::UnfilteredPopupCompletionAll the words are shown in a list, with the first match current.
QCompleter::InlineCompletionThe rest of the first match is put after the text, selected.

enum QCompleter::ModelSorting

How the words are sorted; recorded only.

ConstantDescription
QCompleter::UnsortedModelNot sorted.
QCompleter::CaseSensitivelySortedModelSorted, case matters.
QCompleter::CaseInsensitivelySortedModelSorted, case does not matter.

Member Function Documentation

[explicit] QCompleter::QCompleter(QObject *parent = nullptr)

Constructs a completer without words, with the given parent.

[explicit] QCompleter::QCompleter(const QStringList &completions, QObject *parent = nullptr)

Constructs a completer of the words in completions, with the given parent.

[override] QCompleter::~QCompleter()

Destroys the completer and its popup.

[signal] void QCompleter::activated(const QString &text)

This signal is emitted when the user chooses the completion text, from the popup or with Return.

Qt::CaseSensitivity QCompleter::caseSensitivity() const

Returns whether case matters when the words are matched, Qt::CaseSensitive by default.

[slot] void QCompleter::complete(const QRect &rect = QRect())

Offers the completions of the prefix: shows the popup under the widget, or under rect if it is valid, in the coordinates of the widget. In InlineCompletion mode nothing is shown.

See also setCompletionPrefix().

int QCompleter::completionCount() const

Returns the number of words that match the prefix, or of all the words in UnfilteredPopupCompletion mode.

CompletionMode QCompleter::completionMode() const

Returns how the completions are offered, PopupCompletion by default.

QString QCompleter::completionPrefix() const

Returns the text that the words are matched against.

See also setCompletionPrefix().

QString QCompleter::currentCompletion() const

Returns the current completion, or an empty string.

int QCompleter::currentRow() const

Returns the row of the current completion, or -1.

Qt::MatchFlags QCompleter::filterMode() const

Returns how the words are matched, Qt::MatchStartsWith by default.

[signal] void QCompleter::highlighted(const QString &text)

This signal is emitted when the user moves to the completion text in the popup.

int QCompleter::maxVisibleItems() const

Returns how many words the popup shows before it scrolls, 7 by default.

ModelSorting QCompleter::modelSorting() const

Returns how the words are sorted, UnsortedModel by default.

Returns the list that shows the completions, creating it if there is none yet.

void QCompleter::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)

Sets whether case matters when the words are matched to caseSensitivity.

See also caseSensitivity().

void QCompleter::setCompletionMode(CompletionMode mode)

Sets how the completions are offered to mode.

[slot] void QCompleter::setCompletionPrefix(const QString &prefix)

Sets the text that the words are matched against to prefix. The first match becomes current.

See also complete().

bool QCompleter::setCurrentRow(int row)

Makes the word at row of the completions current and returns whether there is such a row.

See also currentRow() and currentCompletion().

void QCompleter::setFilterMode(Qt::MatchFlags filterMode)

Sets how the words are matched to filterMode: Qt::MatchStartsWith, Qt::MatchContains or Qt::MatchEndsWith. Other modes are refused.

See also filterMode().

void QCompleter::setMaxVisibleItems(int maxItems)

Sets how many words the popup shows before it scrolls to maxItems.

See also maxVisibleItems().

void QCompleter::setModelSorting(ModelSorting sorting)

Sets how the words are sorted to sorting; recorded only.

void QCompleter::setWidget(QWidget *widget)

Makes widget the widget that the completer completes and shows its popup under. QLineEdit::setCompleter() calls it.

See also widget().

void QCompleter::setWrapAround(bool wrap)

Sets whether moving past the end of the popup goes round to the start to wrap.

QWidget * QCompleter::widget() const

Returns the widget that the completer completes, or nullptr.

bool QCompleter::wrapAround() const

Returns whether moving past the end of the popup goes round to the start. The default is true.