QCompleter Class
The QCompleter class completes what the user types from a list of words. More…
| Header | #include <QCompleter> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Types
| enum | CompletionMode |
| enum | ModelSorting |
Public Functions
| QCompleter(QObject *parent = nullptr) | |
| QCompleter(const QStringList &completions, QObject *parent = nullptr) | |
| ~QCompleter() | |
| void | setWidget(QWidget *widget) |
| QWidget * | widget() const |
| void | setCompletionMode(CompletionMode mode) |
| CompletionMode | completionMode() const |
| void | setFilterMode(Qt::MatchFlags filterMode) |
| Qt::MatchFlags | filterMode() const |
| void | setCaseSensitivity(Qt::CaseSensitivity caseSensitivity) |
| Qt::CaseSensitivity | caseSensitivity() const |
| void | setModelSorting(ModelSorting sorting) |
| ModelSorting | modelSorting() const |
| void | setMaxVisibleItems(int maxItems) |
| int | maxVisibleItems() const |
| void | setWrapAround(bool wrap) |
| bool | wrapAround() const |
| QString | completionPrefix() const |
| int | completionCount() const |
| bool | setCurrentRow(int row) |
| int | currentRow() const |
| QString | currentCompletion() const |
| QAbstractItemView * | popup() const |
Public Slots
| void | setCompletionPrefix(const QString &prefix) |
| void | complete(const QRect &rect = QRect()) |
Signals
| void | activated(const QString &text) |
| void | highlighted(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.
| Constant | Description |
|---|---|
QCompleter::PopupCompletion | The words that match are shown in a list. |
QCompleter::UnfilteredPopupCompletion | All the words are shown in a list, with the first match current. |
QCompleter::InlineCompletion | The rest of the first match is put after the text, selected. |
enum QCompleter::ModelSorting
How the words are sorted; recorded only.
| Constant | Description |
|---|---|
QCompleter::UnsortedModel | Not sorted. |
QCompleter::CaseSensitivelySortedModel | Sorted, case matters. |
QCompleter::CaseInsensitivelySortedModel | Sorted, case does not matter. |
Member Function Documentation
QCompleter::QCompleter(QObject *parent = nullptr)
Constructs a completer without words, with the given parent.
QCompleter::QCompleter(const QStringList &completions, QObject *parent = nullptr)
Constructs a completer of the words in completions, with the given parent.
QCompleter::~QCompleter()
Destroys the completer and its popup.
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.
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.
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.
QAbstractItemView * QCompleter::popup() const
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.
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.