Qt 4.8
|
The QCompleter class provides completions based on an item model. More...
#include <qcompleter.h>
Public Types | |
enum | CompletionMode { PopupCompletion, UnfilteredPopupCompletion, InlineCompletion } |
This enum specifies how completions are provided to the user. More... | |
enum | ModelSorting { UnsortedModel = 0, CaseSensitivelySortedModel, CaseInsensitivelySortedModel } |
This enum specifies how the items in the model are sorted. More... | |
Public Slots | |
void | complete (const QRect &rect=QRect()) |
For QCompleter::PopupCompletion and QCompletion::UnfilteredPopupCompletion modes, calling this function displays the popup displaying the current completions. More... | |
void | setCompletionPrefix (const QString &prefix) |
void | setWrapAround (bool wrap) |
Public Slots inherited from QObject | |
void | deleteLater () |
Schedules this object for deletion. More... | |
Signals | |
void | activated (const QString &text) |
This signal is sent when an item in the popup() is activated by the user (by clicking or pressing return). More... | |
void | activated (const QModelIndex &index) |
This signal is sent when an item in the popup() is activated by the user. More... | |
void | highlighted (const QString &text) |
This signal is sent when an item in the popup() is highlighted by the user. More... | |
void | highlighted (const QModelIndex &index) |
This signal is sent when an item in the popup() is highlighted by the user. More... | |
Signals inherited from QObject | |
void | destroyed (QObject *=0) |
This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More... | |
Public Functions | |
Qt::CaseSensitivity | caseSensitivity () const |
int | completionColumn () const |
int | completionCount () const |
Returns the number of completions for the current prefix. More... | |
CompletionMode | completionMode () const |
QAbstractItemModel * | completionModel () const |
Returns the completion model. More... | |
QString | completionPrefix () const |
int | completionRole () const |
QString | currentCompletion () const |
Returns the current completion string. More... | |
QModelIndex | currentIndex () const |
Returns the model index of the current completion in the completionModel(). More... | |
int | currentRow () const |
Returns the current row. More... | |
int | maxVisibleItems () const |
QAbstractItemModel * | model () const |
Returns the model that provides completion strings. More... | |
ModelSorting | modelSorting () const |
virtual QString | pathFromIndex (const QModelIndex &index) const |
Returns the path for the given index. More... | |
QAbstractItemView * | popup () const |
Returns the popup used to display completions. More... | |
QCompleter (QObject *parent=0) | |
Constructs a completer object with the given parent. More... | |
QCompleter (QAbstractItemModel *model, QObject *parent=0) | |
Constructs a completer object with the given parent that provides completions from the specified model. More... | |
QCompleter (const QStringList &completions, QObject *parent=0) | |
Constructs a QCompleter object with the given parent that uses the specified list as a source of possible completions. More... | |
void | setCaseSensitivity (Qt::CaseSensitivity caseSensitivity) |
void | setCompletionColumn (int column) |
void | setCompletionMode (CompletionMode mode) |
void | setCompletionRole (int role) |
bool | setCurrentRow (int row) |
Sets the current row to the row specified. More... | |
void | setMaxVisibleItems (int maxItems) |
void | setModel (QAbstractItemModel *c) |
Sets the model which provides completions to model. More... | |
void | setModelSorting (ModelSorting sorting) |
void | setPopup (QAbstractItemView *popup) |
Sets the popup used to display completions to popup. More... | |
void | setWidget (QWidget *widget) |
Sets the widget for which completion are provided for to widget. More... | |
virtual QStringList | splitPath (const QString &path) const |
Splits the given path into strings that are used to match at each level in the model(). More... | |
QWidget * | widget () const |
Returns the widget for which the completer object is providing completions. More... | |
bool | wrapAround () const |
~QCompleter () | |
Destroys the completer object. More... | |
Public Functions inherited from QObject | |
bool | blockSignals (bool b) |
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More... | |
const QObjectList & | children () const |
Returns a list of child objects. More... | |
bool | connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const |
bool | disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0) |
bool | disconnect (const QObject *receiver, const char *member=0) |
void | dumpObjectInfo () |
Dumps information about signal connections, etc. More... | |
void | dumpObjectTree () |
Dumps a tree of children to the debug output. More... | |
QList< QByteArray > | dynamicPropertyNames () const |
Returns the names of all properties that were dynamically added to the object using setProperty(). More... | |
template<typename T > | |
T | findChild (const QString &aName=QString()) const |
Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More... | |
template<typename T > | |
QList< T > | findChildren (const QString &aName=QString()) const |
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More... | |
template<typename T > | |
QList< T > | findChildren (const QRegExp &re) const |
bool | inherits (const char *classname) const |
Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More... | |
void | installEventFilter (QObject *) |
Installs an event filter filterObj on this object. More... | |
bool | isWidgetType () const |
Returns true if the object is a widget; otherwise returns false. More... | |
void | killTimer (int id) |
Kills the timer with timer identifier, id. More... | |
virtual const QMetaObject * | metaObject () const |
Returns a pointer to the meta-object of this object. More... | |
void | moveToThread (QThread *thread) |
Changes the thread affinity for this object and its children. More... | |
QString | objectName () const |
QObject * | parent () const |
Returns a pointer to the parent object. More... | |
QVariant | property (const char *name) const |
Returns the value of the object's name property. More... | |
Q_INVOKABLE | QObject (QObject *parent=0) |
Constructs an object with parent object parent. More... | |
void | removeEventFilter (QObject *) |
Removes an event filter object obj from this object. More... | |
void | setObjectName (const QString &name) |
void | setParent (QObject *) |
Makes the object a child of parent. More... | |
bool | setProperty (const char *name, const QVariant &value) |
Sets the value of the object's name property to value. More... | |
void | setUserData (uint id, QObjectUserData *data) |
bool | signalsBlocked () const |
Returns true if signals are blocked; otherwise returns false. More... | |
int | startTimer (int interval) |
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More... | |
QThread * | thread () const |
Returns the thread in which the object lives. More... | |
QObjectUserData * | userData (uint id) const |
virtual | ~QObject () |
Destroys the object, deleting all its child objects. More... | |
Protected Functions | |
bool | event (QEvent *) |
Reimplemented Function More... | |
bool | eventFilter (QObject *o, QEvent *e) |
Reimplemented Function More... | |
Protected Functions inherited from QObject | |
virtual void | childEvent (QChildEvent *) |
This event handler can be reimplemented in a subclass to receive child events. More... | |
virtual void | connectNotify (const char *signal) |
This virtual function is called when something has been connected to signal in this object. More... | |
virtual void | customEvent (QEvent *) |
This event handler can be reimplemented in a subclass to receive custom events. More... | |
virtual void | disconnectNotify (const char *signal) |
This virtual function is called when something has been disconnected from signal in this object. More... | |
QObject (QObjectPrivate &dd, QObject *parent=0) | |
int | receivers (const char *signal) const |
Returns the number of receivers connected to the signal. More... | |
QObject * | sender () const |
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More... | |
int | senderSignalIndex () const |
virtual void | timerEvent (QTimerEvent *) |
This event handler can be reimplemented in a subclass to receive timer events for the object. More... | |
Properties | |
Qt::CaseSensitivity | caseSensitivity |
the case sensitivity of the matching More... | |
int | completionColumn |
the column in the model in which completions are searched for. More... | |
CompletionMode | completionMode |
how the completions are provided to the user More... | |
QString | completionPrefix |
the completion prefix used to provide completions. More... | |
int | completionRole |
the item role to be used to query the contents of items for matching. More... | |
int | maxVisibleItems |
the maximum allowed size on screen of the completer, measured in items More... | |
ModelSorting | modelSorting |
the way the model is sorted More... | |
bool | wrapAround |
the completions wrap around when navigating through items More... | |
Additional Inherited Members | |
Static Public Functions inherited from QObject | |
static bool | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection) |
Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More... | |
static bool | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member) |
Disconnects signal in object sender from method in object receiver. More... | |
static bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member) |
static uint | registerUserData () |
static QString | tr (const char *sourceText, const char *comment=0, int n=-1) |
static QString | trUtf8 (const char *sourceText, const char *comment=0, int n=-1) |
Static Public Variables inherited from QObject | |
static const QMetaObject | staticMetaObject |
This variable stores the meta-object for the class. More... | |
Protected Variables inherited from QObject | |
QScopedPointer< QObjectData > | d_ptr |
Static Protected Variables inherited from QObject | |
static const QMetaObject | staticQtMetaObject |
Related Functions inherited from QObject | |
T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QRegExp ®Exp)() |
T * | qobject_cast (QObject *object) |
QObjectList | |
void * | qt_find_obj_child (QObject *parent, const char *type, const QString &name) |
Returns a pointer to the object named name that inherits type and with a given parent. More... | |
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.)
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:
A QFileSystemModel can be used to provide auto completion of file names. For example:
To set the model on which QCompleter should operate, call setModel(). By default, QCompleter will attempt to match the completionPrefix {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.
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:
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 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.
Definition at line 64 of file qcompleter.h.
This enum specifies how completions are provided to the user.
Enumerator | |
---|---|
PopupCompletion | |
UnfilteredPopupCompletion | |
InlineCompletion |
Definition at line 77 of file qcompleter.h.
This enum specifies how the items in the model are sorted.
Enumerator | |
---|---|
UnsortedModel | |
CaseSensitivelySortedModel | |
CaseInsensitivelySortedModel |
Definition at line 83 of file qcompleter.h.
QCompleter::QCompleter | ( | QObject * | parent = 0 | ) |
Constructs a completer object with the given parent.
Definition at line 942 of file qcompleter.cpp.
QCompleter::QCompleter | ( | QAbstractItemModel * | model, |
QObject * | parent = 0 |
||
) |
Constructs a completer object with the given parent that provides completions from the specified model.
Definition at line 953 of file qcompleter.cpp.
QCompleter::QCompleter | ( | const QStringList & | completions, |
QObject * | parent = 0 |
||
) |
Constructs a QCompleter object with the given parent that uses the specified list as a source of possible completions.
Definition at line 965 of file qcompleter.cpp.
QCompleter::~QCompleter | ( | ) |
|
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.
Referenced by setPopup().
|
signal |
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.
Qt::CaseSensitivity QCompleter::caseSensitivity | ( | ) | const |
Referenced by setCaseSensitivity().
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.
Definition at line 1391 of file qcompleter.cpp.
Referenced by QLineControl::complete().
int QCompleter::completionColumn | ( | ) | const |
Referenced by setCompletionColumn().
int QCompleter::completionCount | ( | ) | const |
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.
Definition at line 1450 of file qcompleter.cpp.
Referenced by QLineControl::advanceToEnabledItem().
CompletionMode QCompleter::completionMode | ( | ) | const |
Referenced by popup(), and setCompletionMode().
QAbstractItemModel * QCompleter::completionModel | ( | ) | const |
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.
Definition at line 1707 of file qcompleter.cpp.
Referenced by QLineControl::advanceToEnabledItem().
QString QCompleter::completionPrefix | ( | ) | const |
Referenced by setCompletionPrefix(), and splitPath().
int QCompleter::completionRole | ( | ) | const |
Referenced by setCompletionRole().
QString QCompleter::currentCompletion | ( | ) | const |
Returns the current completion string.
This includes the completionPrefix . When used alongside setCurrentRow(), it can be used to iterate through all the matches.
Definition at line 1690 of file qcompleter.cpp.
Referenced by QLineControl::complete(), and QLineControl::processKeyEvent().
QModelIndex QCompleter::currentIndex | ( | ) | const |
Returns the model index of the current completion in the completionModel().
Definition at line 1677 of file qcompleter.cpp.
Referenced by QLineControl::advanceToEnabledItem().
int QCompleter::currentRow | ( | ) | const |
Returns the current row.
Definition at line 1439 of file qcompleter.cpp.
Referenced by QLineControl::advanceToEnabledItem().
|
protectedvirtual |
Reimplemented Function
Reimplemented from QObject.
Definition at line 1203 of file qcompleter.cpp.
Referenced by eventFilter().
Reimplemented Function
Reimplemented from QObject.
Definition at line 1211 of file qcompleter.cpp.
|
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.
|
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 index in the completionModel() is given.
int QCompleter::maxVisibleItems | ( | ) | const |
Referenced by setWrapAround().
QAbstractItemModel * QCompleter::model | ( | ) | const |
Returns the model that provides completion strings.
Definition at line 1062 of file qcompleter.cpp.
Referenced by setModel().
ModelSorting QCompleter::modelSorting | ( | ) | const |
Referenced by setModelSorting().
|
virtual |
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.
Reimplemented in QFSCompleter.
Definition at line 1724 of file qcompleter.cpp.
Referenced by currentCompletion().
QAbstractItemView * QCompleter::popup | ( | ) | const |
Returns the popup used to display completions.
Definition at line 1182 of file qcompleter.cpp.
Referenced by complete(), QLineControl::complete(), QLineControl::processKeyEvent(), and setPopup().
void QCompleter::setCaseSensitivity | ( | Qt::CaseSensitivity | caseSensitivity | ) |
Definition at line 1633 of file qcompleter.cpp.
Referenced by QFSCompleter::QFSCompleter(), and setModel().
void QCompleter::setCompletionColumn | ( | int | column | ) |
Definition at line 1526 of file qcompleter.cpp.
void QCompleter::setCompletionMode | ( | QCompleter::CompletionMode | mode | ) |
Definition at line 1092 of file qcompleter.cpp.
|
slot |
Definition at line 1659 of file qcompleter.cpp.
Referenced by QLineControl::complete().
void QCompleter::setCompletionRole | ( | int | role | ) |
Definition at line 1556 of file qcompleter.cpp.
Referenced by setModel(), and QScriptBreakpointsWidget::setScriptsModel().
bool QCompleter::setCurrentRow | ( | 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.
Definition at line 1428 of file qcompleter.cpp.
Referenced by QLineControl::advanceToEnabledItem().
void QCompleter::setMaxVisibleItems | ( | int | maxItems | ) |
Definition at line 1611 of file qcompleter.cpp.
void QCompleter::setModel | ( | QAbstractItemModel * | model | ) |
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.
Definition at line 1025 of file qcompleter.cpp.
void QCompleter::setModelSorting | ( | QCompleter::ModelSorting | sorting | ) |
Definition at line 1496 of file qcompleter.cpp.
void QCompleter::setPopup | ( | QAbstractItemView * | popup | ) |
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).
Definition at line 1132 of file qcompleter.cpp.
Referenced by popup(), and setModel().
void QCompleter::setWidget | ( | QWidget * | widget | ) |
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.
Definition at line 989 of file qcompleter.cpp.
Referenced by QLineEdit::setCompleter(), and QComboBox::setCompleter().
|
slot |
Definition at line 1581 of file qcompleter.cpp.
|
virtual |
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.
Reimplemented in QFSCompleter.
Definition at line 1780 of file qcompleter.cpp.
Referenced by setCompletionPrefix().
QWidget * QCompleter::widget | ( | ) | const |
Returns the widget for which the completer object is providing completions.
Definition at line 1008 of file qcompleter.cpp.
Referenced by QLineEdit::setCompleter(), and setWidget().
bool QCompleter::wrapAround | ( | ) | const |
Referenced by setWrapAround().
|
private |
the case sensitivity of the matching
The default is Qt::CaseSensitive.
Definition at line 73 of file qcompleter.h.
Referenced by QLineControl::complete(), and QComboBoxPrivate::matchFlags().
|
private |
the column in the model in which completions are searched for.
If the popup() is a QListView, it is automatically setup to display this column.
By default, the match column is 0.
Definition at line 70 of file qcompleter.h.
|
private |
how the completions are provided to the user
The default value is QCompleter::PopupCompletion.
Definition at line 69 of file qcompleter.h.
Referenced by QLineEditPrivate::_q_completionHighlighted(), QLineEditPrivate::_q_textEdited(), QLineControl::complete(), and QLineControl::processKeyEvent().
|
private |
the completion prefix used to provide completions.
The completionModel() is updated to reflect the list of possible matches for prefix.
Definition at line 67 of file qcompleter.h.
Referenced by QLineControl::complete().
|
private |
the item role to be used to query the contents of items for matching.
The default role is Qt::EditRole.
Definition at line 71 of file qcompleter.h.
|
private |
the maximum allowed size on screen of the completer, measured in items
By default, this property has a value of 7.
Definition at line 72 of file qcompleter.h.
|
private |
the way the model is sorted
By default, no assumptions are made about the order of the items in the model that provides the completions.
If the model's data for the completionColumn() and completionRole() is sorted in ascending order, you can set this property to CaseSensitivelySortedModel or CaseInsensitivelySortedModel . On large models, this can lead to significant performance improvements because the completer object can then use a binary search algorithm instead of linear search algorithm.
The sort order (i.e ascending or descending order) of the model is determined dynamically by inspecting the contents of the model.
Note: The performance improvements described above cannot take place when the completer's caseSensitivity is different to the case sensitivity used by the model's when sorting.
Definition at line 68 of file qcompleter.h.
|
private |
the completions wrap around when navigating through items
The default is true.
Definition at line 74 of file qcompleter.h.
Referenced by QLineControl::advanceToEnabledItem().