Qt 4.8
Public Types | Public Slots | Signals | Public Functions | Protected Functions | Properties | List of all members
QCompleter Class Reference

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

#include <qcompleter.h>

Inheritance diagram for QCompleter:
QObject QFSCompleter

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
 
QAbstractItemModelcompletionModel () 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
 
QAbstractItemModelmodel () 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...
 
QAbstractItemViewpopup () 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...
 
QWidgetwidget () 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 QObjectListchildren () 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< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
template<typename 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 QMetaObjectmetaObject () 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
 
QObjectparent () 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...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (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...
 
QObjectsender () 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< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QCompleter class provides completions based on an item model.

Since
4.2

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

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.

See also
QAbstractItemModel, QLineEdit, QComboBox, {Completer Example}

Definition at line 64 of file qcompleter.h.

Enumerations

◆ CompletionMode

This enum specifies how completions are provided to the user.

  • PopupCompletion Current completions are displayed in a popup window.
  • InlineCompletion Completions appear inline (as selected text).
  • UnfilteredPopupCompletion All possible completions are displayed in a popup window with the most likely suggestion indicated as current.
See also
setCompletionMode()
Enumerator
PopupCompletion 
UnfilteredPopupCompletion 
InlineCompletion 

Definition at line 77 of file qcompleter.h.

◆ ModelSorting

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

  • UnsortedModel The model is unsorted.
  • CaseSensitivelySortedModel The model is sorted case sensitively.
  • CaseInsensitivelySortedModel The model is sorted case insensitively.
See also
setModelSorting()
Enumerator
UnsortedModel 
CaseSensitivelySortedModel 
CaseInsensitivelySortedModel 

Definition at line 83 of file qcompleter.h.

Constructors and Destructors

◆ QCompleter() [1/3]

QCompleter::QCompleter ( QObject parent = 0)

Constructs a completer object with the given parent.

Definition at line 942 of file qcompleter.cpp.

943 : QObject(*new QCompleterPrivate(), parent)
944 {
945  Q_D(QCompleter);
946  d->init();
947 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753

◆ QCompleter() [2/3]

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.

954  : QObject(*new QCompleterPrivate(), parent)
955 {
956  Q_D(QCompleter);
957  d->init(model);
958 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753

◆ QCompleter() [3/3]

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.

966 : QObject(*new QCompleterPrivate(), parent)
967 {
968  Q_D(QCompleter);
969  d->init(new QStringListModel(list, this));
970 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
The QStringListModel class provides a model that supplies strings to views.
#define Q_D(Class)
Definition: qglobal.h:2482
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753

◆ ~QCompleter()

QCompleter::~QCompleter ( )

Destroys the completer object.

Definition at line 976 of file qcompleter.cpp.

977 {
978 }

Functions

◆ activated [1/2]

void QCompleter::activated ( const QString text)
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().

◆ activated [2/2]

void QCompleter::activated ( const QModelIndex index)
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.

◆ caseSensitivity()

Qt::CaseSensitivity QCompleter::caseSensitivity ( ) const

Referenced by setCaseSensitivity().

◆ complete

void QCompleter::complete ( const QRect rect = QRect())
slot

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

1392 {
1393  Q_D(QCompleter);
1394  QModelIndex idx = d->proxy->currentIndex(false);
1395  d->hiddenBecauseNoMatch = false;
1396  if (d->mode == QCompleter::InlineCompletion) {
1397  if (idx.isValid())
1398  d->_q_complete(idx, true);
1399  return;
1400  }
1401 
1402  Q_ASSERT(d->widget != 0);
1403  if ((d->mode == QCompleter::PopupCompletion && !idx.isValid())
1404  || (d->mode == QCompleter::UnfilteredPopupCompletion && d->proxy->rowCount() == 0)) {
1405  if (d->popup)
1406  d->popup->hide(); // no suggestion, hide
1407  d->hiddenBecauseNoMatch = true;
1408  return;
1409  }
1410 
1411  popup();
1413  d->setCurrentIndex(idx, false);
1414 
1415  d->showPopup(rect);
1416  d->popupRect = rect;
1417 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QModelIndex class is used to locate data in a data model.
QAbstractItemView * popup() const
Returns the popup used to display completions.

◆ completionColumn()

int QCompleter::completionColumn ( ) const

Referenced by setCompletionColumn().

◆ completionCount()

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

1451 {
1452  Q_D(const QCompleter);
1453  return d->proxy->completionCount();
1454 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ completionMode()

CompletionMode QCompleter::completionMode ( ) const

Referenced by popup(), and setCompletionMode().

◆ completionModel()

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.

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, model()

Definition at line 1707 of file qcompleter.cpp.

Referenced by QLineControl::advanceToEnabledItem().

1708 {
1709  Q_D(const QCompleter);
1710  return d->proxy;
1711 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ completionPrefix()

QString QCompleter::completionPrefix ( ) const

Referenced by setCompletionPrefix(), and splitPath().

◆ completionRole()

int QCompleter::completionRole ( ) const

Referenced by setCompletionRole().

◆ currentCompletion()

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.

See also
setCurrentRow(), currentIndex()

Definition at line 1690 of file qcompleter.cpp.

Referenced by QLineControl::complete(), and QLineControl::processKeyEvent().

1691 {
1692  Q_D(const QCompleter);
1693  return pathFromIndex(d->proxy->currentIndex(true));
1694 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
virtual QString pathFromIndex(const QModelIndex &index) const
Returns the path for the given index.

◆ currentIndex()

QModelIndex QCompleter::currentIndex ( ) const

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

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

Definition at line 1677 of file qcompleter.cpp.

Referenced by QLineControl::advanceToEnabledItem().

1678 {
1679  Q_D(const QCompleter);
1680  return d->proxy->currentIndex(false);
1681 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ currentRow()

int QCompleter::currentRow ( ) const

Returns the current row.

See also
setCurrentRow()

Definition at line 1439 of file qcompleter.cpp.

Referenced by QLineControl::advanceToEnabledItem().

1440 {
1441  Q_D(const QCompleter);
1442  return d->proxy->currentRow();
1443 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ event()

bool QCompleter::event ( QEvent ev)
protectedvirtual

Reimplemented Function

Reimplemented from QObject.

Definition at line 1203 of file qcompleter.cpp.

Referenced by eventFilter().

1204 {
1205  return QObject::event(ev);
1206 }
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200

◆ eventFilter()

bool QCompleter::eventFilter ( QObject o,
QEvent e 
)
protectedvirtual

Reimplemented Function

Reimplemented from QObject.

Definition at line 1211 of file qcompleter.cpp.

1212 {
1213  Q_D(QCompleter);
1214 
1215  if (d->eatFocusOut && o == d->widget && e->type() == QEvent::FocusOut) {
1216  d->hiddenBecauseNoMatch = false;
1217  if (d->popup && d->popup->isVisible())
1218  return true;
1219  }
1220 
1221  if (o != d->popup)
1222  return QObject::eventFilter(o, e);
1223 
1224  switch (e->type()) {
1225  case QEvent::KeyPress: {
1226  QKeyEvent *ke = static_cast<QKeyEvent *>(e);
1227 
1228  QModelIndex curIndex = d->popup->currentIndex();
1229  QModelIndexList selList = d->popup->selectionModel()->selectedIndexes();
1230 
1231  const int key = ke->key();
1232  // In UnFilteredPopup mode, select the current item
1233  if ((key == Qt::Key_Up || key == Qt::Key_Down) && selList.isEmpty() && curIndex.isValid()
1235  d->setCurrentIndex(curIndex);
1236  return true;
1237  }
1238 
1239  // Handle popup navigation keys. These are hardcoded because up/down might make the
1240  // widget do something else (lineedit cursor moves to home/end on mac, for instance)
1241  switch (key) {
1242  case Qt::Key_End:
1243  case Qt::Key_Home:
1244  if (ke->modifiers() & Qt::ControlModifier)
1245  return false;
1246  break;
1247 
1248  case Qt::Key_Up:
1249  if (!curIndex.isValid()) {
1250  int rowCount = d->proxy->rowCount();
1251  QModelIndex lastIndex = d->proxy->index(rowCount - 1, d->column);
1252  d->setCurrentIndex(lastIndex);
1253  return true;
1254  } else if (curIndex.row() == 0) {
1255  if (d->wrap)
1256  d->setCurrentIndex(QModelIndex());
1257  return true;
1258  }
1259  return false;
1260 
1261  case Qt::Key_Down:
1262  if (!curIndex.isValid()) {
1263  QModelIndex firstIndex = d->proxy->index(0, d->column);
1264  d->setCurrentIndex(firstIndex);
1265  return true;
1266  } else if (curIndex.row() == d->proxy->rowCount() - 1) {
1267  if (d->wrap)
1268  d->setCurrentIndex(QModelIndex());
1269  return true;
1270  }
1271  return false;
1272 
1273  case Qt::Key_PageUp:
1274  case Qt::Key_PageDown:
1275  return false;
1276  }
1277 
1278  // Send the event to the widget. If the widget accepted the event, do nothing
1279  // If the widget did not accept the event, provide a default implementation
1280  d->eatFocusOut = false;
1281  (static_cast<QObject *>(d->widget))->event(ke);
1282  d->eatFocusOut = true;
1283  if (!d->widget || e->isAccepted() || !d->popup->isVisible()) {
1284  // widget lost focus, hide the popup
1285  if (d->widget && (!d->widget->hasFocus()
1286 #ifdef QT_KEYPAD_NAVIGATION
1287  || (QApplication::keypadNavigationEnabled() && !d->widget->hasEditFocus())
1288 #endif
1289  ))
1290  d->popup->hide();
1291  if (e->isAccepted())
1292  return true;
1293  }
1294 
1295  // default implementation for keys not handled by the widget when popup is open
1296  switch (key) {
1297 #ifdef QT_KEYPAD_NAVIGATION
1298  case Qt::Key_Select:
1299  if (!QApplication::keypadNavigationEnabled())
1300  break;
1301 #endif
1302  case Qt::Key_Return:
1303  case Qt::Key_Enter:
1304  case Qt::Key_Tab:
1305  d->popup->hide();
1306  if (curIndex.isValid())
1307  d->_q_complete(curIndex);
1308  break;
1309 
1310  case Qt::Key_F4:
1311  if (ke->modifiers() & Qt::AltModifier)
1312  d->popup->hide();
1313  break;
1314 
1315  case Qt::Key_Backtab:
1316  case Qt::Key_Escape:
1317  d->popup->hide();
1318  break;
1319 
1320  default:
1321  break;
1322  }
1323 
1324  return true;
1325  }
1326 
1327 #ifdef QT_KEYPAD_NAVIGATION
1328  case QEvent::KeyRelease: {
1329  QKeyEvent *ke = static_cast<QKeyEvent *>(e);
1330  if (QApplication::keypadNavigationEnabled() && ke->key() == Qt::Key_Back) {
1331  // Send the event to the 'widget'. This is what we did for KeyPress, so we need
1332  // to do the same for KeyRelease, in case the widget's KeyPress event set
1333  // up something (such as a timer) that is relying on also receiving the
1334  // key release. I see this as a bug in Qt, and should really set it up for all
1335  // the affected keys. However, it is difficult to tell how this will affect
1336  // existing code, and I can't test for every combination!
1337  d->eatFocusOut = false;
1338  static_cast<QObject *>(d->widget)->event(ke);
1339  d->eatFocusOut = true;
1340  }
1341  break;
1342  }
1343 #endif
1344 
1345  case QEvent::MouseButtonPress: {
1346 #ifdef QT_KEYPAD_NAVIGATION
1347  if (QApplication::keypadNavigationEnabled()) {
1348  // if we've clicked in the widget (or its descendant), let it handle the click
1349  QWidget *source = qobject_cast<QWidget *>(o);
1350  if (source) {
1351  QPoint pos = source->mapToGlobal((static_cast<QMouseEvent *>(e))->pos());
1352  QWidget *target = QApplication::widgetAt(pos);
1353  if (target && (d->widget->isAncestorOf(target) ||
1354  target == d->widget)) {
1355  d->eatFocusOut = false;
1356  static_cast<QObject *>(target)->event(e);
1357  d->eatFocusOut = true;
1358  return true;
1359  }
1360  }
1361  }
1362 #endif
1363  if (!d->popup->underMouse()) {
1364  d->popup->hide();
1365  return true;
1366  }
1367  }
1368  return false;
1369 
1370  case QEvent::InputMethod:
1372  QApplication::sendEvent(d->widget, e);
1373  break;
1374 
1375  default:
1376  return false;
1377  }
1378  return false;
1379 }
double d
Definition: qnumeric_p.h:62
The QKeyEvent class describes a key event.
Definition: qevent.h:224
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
T * qobject_cast(QObject *object)
Definition: qobject.h:375
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
static QWidget * widgetAt(const QPoint &p)
Returns the widget at global screen position point, or 0 if there is no Qt widget there...
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifier flags that existed immediately after the event occurred.
Definition: qevent.cpp:999
int key() const
Returns the code of the key that was pressed or released.
Definition: qevent.h:231
bool isAccepted() const
Definition: qcoreevent.h:307
int row() const
Returns the row this model index refers to.
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
virtual bool eventFilter(QObject *, QEvent *)
Filters events if this object has been installed as an event filter for the watched object...
Definition: qobject.cpp:1375
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QModelIndex class is used to locate data in a data model.
bool event(QEvent *)
Reimplemented Function
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
QPoint mapToGlobal(const QPoint &) const
Translates the widget coordinate pos to global screen coordinates.

◆ highlighted [1/2]

void QCompleter::highlighted ( const QString text)
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.

◆ highlighted [2/2]

void QCompleter::highlighted ( const QModelIndex index)
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.

◆ maxVisibleItems()

int QCompleter::maxVisibleItems ( ) const

Referenced by setWrapAround().

◆ model()

QAbstractItemModel * QCompleter::model ( ) const

Returns the model that provides completion strings.

See also
completionModel()

Definition at line 1062 of file qcompleter.cpp.

Referenced by setModel().

1063 {
1064  Q_D(const QCompleter);
1065  return d->proxy->sourceModel();
1066 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ modelSorting()

ModelSorting QCompleter::modelSorting ( ) const

Referenced by setModelSorting().

◆ pathFromIndex()

QString QCompleter::pathFromIndex ( const QModelIndex index) const
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.

See also
splitPath()

Reimplemented in QFSCompleter.

Definition at line 1724 of file qcompleter.cpp.

Referenced by currentCompletion().

1725 {
1726  Q_D(const QCompleter);
1727  if (!index.isValid())
1728  return QString();
1729 
1730  QAbstractItemModel *sourceModel = d->proxy->sourceModel();
1731  if (!sourceModel)
1732  return QString();
1733  bool isDirModel = false;
1734  bool isFsModel = false;
1735 #ifndef QT_NO_DIRMODEL
1736  isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0;
1737 #endif
1738 #ifndef QT_NO_FILESYSTEMMODEL
1739  isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) != 0;
1740 #endif
1741  if (!isDirModel && !isFsModel)
1742  return sourceModel->data(index, d->role).toString();
1743 
1744  QModelIndex idx = index;
1745  QStringList list;
1746  do {
1747  QString t;
1748  if (isDirModel)
1749  t = sourceModel->data(idx, Qt::EditRole).toString();
1750 #ifndef QT_NO_FILESYSTEMMODEL
1751  else
1752  t = sourceModel->data(idx, QFileSystemModel::FileNameRole).toString();
1753 #endif
1754  list.prepend(t);
1755  QModelIndex parent = idx.parent();
1756  idx = parent.sibling(parent.row(), index.column());
1757  } while (idx.isValid());
1758 
1759 #if (!defined(Q_OS_WIN) || defined(Q_OS_WINCE)) && !defined(Q_OS_SYMBIAN)
1760  if (list.count() == 1) // only the separator or some other text
1761  return list[0];
1762  list[0].clear() ; // the join below will provide the separator
1763 #endif
1764 
1765  return list.join(QDir::separator());
1766 }
double d
Definition: qnumeric_p.h:62
The QFileSystemModel class provides a data model for the local filesystem.
QModelIndex sibling(int row, int column) const
Returns the sibling at row and column.
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
#define Q_D(Class)
Definition: qglobal.h:2482
static QChar separator()
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows...
Definition: qdir.cpp:1831
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
int row() const
Returns the row this model index refers to.
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const =0
Returns the data stored under the given role for the item referred to by the index.
void clear()
Removes all items from the list.
Definition: qlist.h:764
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QAbstractItemModel class provides the abstract interface for item model classes.
QString join(const QString &sep) const
Joins all the string list&#39;s strings into a single string with each element separated by the given sep...
Definition: qstringlist.h:162
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
The QModelIndex class is used to locate data in a data model.
quint16 index
The QDirModel class provides a data model for the local filesystem.
Definition: qdirmodel.h:59
int column() const
Returns the column this model index refers to.

◆ popup()

QAbstractItemView * QCompleter::popup ( ) const

Returns the popup used to display completions.

See also
setPopup()

Definition at line 1182 of file qcompleter.cpp.

Referenced by complete(), QLineControl::complete(), QLineControl::processKeyEvent(), and setPopup().

1183 {
1184  Q_D(const QCompleter);
1185 #ifndef QT_NO_LISTVIEW
1186  if (!d->popup && completionMode() != QCompleter::InlineCompletion) {
1187  QListView *listView = new QListView;
1192  listView->setModelColumn(d->column);
1193  QCompleter *that = const_cast<QCompleter*>(this);
1194  that->setPopup(listView);
1195  }
1196 #endif // QT_NO_LISTVIEW
1197  return d->popup;
1198 }
double d
Definition: qnumeric_p.h:62
void setSelectionMode(QAbstractItemView::SelectionMode mode)
void setPopup(QAbstractItemView *popup)
Sets the popup used to display completions to popup.
void setEditTriggers(EditTriggers triggers)
void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy)
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
void setModelColumn(int column)
Definition: qlistview.cpp:1586
CompletionMode completionMode() const
The QListView class provides a list or icon view onto a model.
Definition: qlistview.h:57
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)

◆ setCaseSensitivity()

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

Definition at line 1633 of file qcompleter.cpp.

Referenced by QFSCompleter::QFSCompleter(), and setModel().

1634 {
1635  Q_D(QCompleter);
1636  if (d->cs == cs)
1637  return;
1638  d->cs = cs;
1639  d->proxy->createEngine();
1640  d->proxy->invalidate();
1641 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setCompletionColumn()

void QCompleter::setCompletionColumn ( int  column)

Definition at line 1526 of file qcompleter.cpp.

1527 {
1528  Q_D(QCompleter);
1529  if (d->column == column)
1530  return;
1531 #ifndef QT_NO_LISTVIEW
1532  if (QListView *listView = qobject_cast<QListView *>(d->popup))
1533  listView->setModelColumn(column);
1534 #endif
1535  d->column = column;
1536  d->proxy->invalidate();
1537 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
The QListView class provides a list or icon view onto a model.
Definition: qlistview.h:57

◆ setCompletionMode()

void QCompleter::setCompletionMode ( QCompleter::CompletionMode  mode)

Definition at line 1092 of file qcompleter.cpp.

1093 {
1094  Q_D(QCompleter);
1095  d->mode = mode;
1096  d->proxy->setFiltered(mode != QCompleter::UnfilteredPopupCompletion);
1097 
1098  if (mode == QCompleter::InlineCompletion) {
1099  if (d->widget)
1100  d->widget->removeEventFilter(this);
1101  if (d->popup) {
1102  d->popup->deleteLater();
1103  d->popup = 0;
1104  }
1105  } else {
1106  if (d->widget)
1107  d->widget->installEventFilter(this);
1108  }
1109 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setCompletionPrefix

void QCompleter::setCompletionPrefix ( const QString prefix)
slot

Definition at line 1659 of file qcompleter.cpp.

Referenced by QLineControl::complete().

1660 {
1661  Q_D(QCompleter);
1662  d->prefix = prefix;
1663  d->proxy->filter(splitPath(prefix));
1664 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
virtual QStringList splitPath(const QString &path) const
Splits the given path into strings that are used to match at each level in the model().

◆ setCompletionRole()

void QCompleter::setCompletionRole ( int  role)

Definition at line 1556 of file qcompleter.cpp.

Referenced by setModel(), and QScriptBreakpointsWidget::setScriptsModel().

1557 {
1558  Q_D(QCompleter);
1559  if (d->role == role)
1560  return;
1561  d->role = role;
1562  d->proxy->invalidate();
1563 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setCurrentRow()

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.

See also
currentCompletion(), completionCount()

Definition at line 1428 of file qcompleter.cpp.

Referenced by QLineControl::advanceToEnabledItem().

1429 {
1430  Q_D(QCompleter);
1431  return d->proxy->setCurrentRow(row);
1432 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setMaxVisibleItems()

void QCompleter::setMaxVisibleItems ( int  maxItems)

Definition at line 1611 of file qcompleter.cpp.

1612 {
1613  Q_D(QCompleter);
1614  if (maxItems < 0) {
1615  qWarning("QCompleter::setMaxVisibleItems: "
1616  "Invalid max visible items (%d) must be >= 0", maxItems);
1617  return;
1618  }
1619  d->maxVisibleItems = maxItems;
1620 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)

◆ setModel()

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.

See also
completionModel(), modelSorting, {Handling Tree Models}

Definition at line 1025 of file qcompleter.cpp.

1026 {
1027  Q_D(QCompleter);
1028  QAbstractItemModel *oldModel = d->proxy->sourceModel();
1029  d->proxy->setSourceModel(model);
1030  if (d->popup)
1031  setPopup(d->popup); // set the model and make new connections
1032  if (oldModel && oldModel->QObject::parent() == this)
1033  delete oldModel;
1034 #ifndef QT_NO_DIRMODEL
1035  if (qobject_cast<QDirModel *>(model)) {
1036 #if (defined(Q_OS_WIN) && !defined(Q_OS_WINCE)) || defined(Q_OS_SYMBIAN)
1038 #else
1040 #endif
1041  }
1042 #endif // QT_NO_DIRMODEL
1043 #ifndef QT_NO_FILESYSTEMMODEL
1045  if (fsModel) {
1046 #if (defined(Q_OS_WIN) && !defined(Q_OS_WINCE)) || defined(Q_OS_SYMBIAN)
1048 #else
1050 #endif
1052  connect(fsModel, SIGNAL(directoryLoaded(QString)), this, SLOT(_q_fileSystemModelDirectoryLoaded(QString)));
1053  }
1054 #endif // QT_NO_FILESYSTEMMODEL
1055 }
double d
Definition: qnumeric_p.h:62
The QFileSystemModel class provides a data model for the local filesystem.
void setPopup(QAbstractItemView *popup)
Sets the popup used to display completions to popup.
#define SLOT(a)
Definition: qobjectdefs.h:226
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
#define Q_D(Class)
Definition: qglobal.h:2482
#define SIGNAL(a)
Definition: qobjectdefs.h:227
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 rece...
Definition: qobject.cpp:2580
QAbstractItemModel * model() const
Returns the model that provides completion strings.
The QAbstractItemModel class provides the abstract interface for item model classes.
void setCompletionRole(int role)
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)

◆ setModelSorting()

void QCompleter::setModelSorting ( QCompleter::ModelSorting  sorting)

Definition at line 1496 of file qcompleter.cpp.

1497 {
1498  Q_D(QCompleter);
1499  if (d->sorting == sorting)
1500  return;
1501  d->sorting = sorting;
1502  d->proxy->createEngine();
1503  d->proxy->invalidate();
1504 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setPopup()

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

See also
popup()

Definition at line 1132 of file qcompleter.cpp.

Referenced by popup(), and setModel().

1133 {
1134  Q_D(QCompleter);
1135  Q_ASSERT(popup != 0);
1136  if (d->popup) {
1137  QObject::disconnect(d->popup->selectionModel(), 0, this, 0);
1138  QObject::disconnect(d->popup, 0, this, 0);
1139  }
1140  if (d->popup != popup)
1141  delete d->popup;
1142  if (popup->model() != d->proxy)
1143  popup->setModel(d->proxy);
1144 #if defined(Q_OS_MAC) && !defined(QT_MAC_USE_COCOA)
1145  popup->show();
1146 #else
1147  popup->hide();
1148 #endif
1149 
1150  Qt::FocusPolicy origPolicy = Qt::NoFocus;
1151  if (d->widget)
1152  origPolicy = d->widget->focusPolicy();
1153  popup->setParent(0, Qt::Popup);
1154  popup->setFocusPolicy(Qt::NoFocus);
1155  if (d->widget)
1156  d->widget->setFocusPolicy(origPolicy);
1157 
1158  popup->setFocusProxy(d->widget);
1159  popup->installEventFilter(this);
1160  popup->setItemDelegate(new QCompleterItemDelegate(popup));
1161 #ifndef QT_NO_LISTVIEW
1162  if (QListView *listView = qobject_cast<QListView *>(popup)) {
1163  listView->setModelColumn(d->column);
1164  }
1165 #endif
1166 
1167  QObject::connect(popup, SIGNAL(clicked(QModelIndex)),
1168  this, SLOT(_q_complete(QModelIndex)));
1170  popup, SLOT(hide()));
1171 
1173  this, SLOT(_q_completionSelected(QItemSelection)));
1174  d->popup = popup;
1175 }
double d
Definition: qnumeric_p.h:62
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
#define SLOT(a)
Definition: qobjectdefs.h:226
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void setModel(QAbstractItemModel *model)
Sets the model for the view to present.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
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 rece...
Definition: qobject.cpp:2580
QItemSelectionModel * selectionModel() const
Returns the current selection model.
void show()
Shows the widget and its child widgets.
void hide()
Hides the widget.
Definition: qwidget.h:501
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.
Definition: qobject.cpp:2895
void setFocusProxy(QWidget *)
Sets the widget&#39;s focus proxy to widget w.
Definition: qwidget.cpp:6537
The QItemSelection class manages information about selected items in a model.
The QListView class provides a list or icon view onto a model.
Definition: qlistview.h:57
void activated(const QString &text)
This signal is sent when an item in the popup() is activated by the user (by clicking or pressing ret...
void installEventFilter(QObject *)
Installs an event filter filterObj on this object.
Definition: qobject.cpp:2070
The QModelIndex class is used to locate data in a data model.
FocusPolicy
Definition: qnamespace.h:181
void setItemDelegate(QAbstractItemDelegate *delegate)
Sets the item delegate for this view and its model to delegate.
QAbstractItemModel * model() const
Returns the model that this view is presenting.
QAbstractItemView * popup() const
Returns the popup used to display completions.
void setFocusPolicy(Qt::FocusPolicy policy)
Definition: qwidget.cpp:7631

◆ setWidget()

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.

See also
widget(), setModel(), setPopup()

Definition at line 989 of file qcompleter.cpp.

Referenced by QLineEdit::setCompleter(), and QComboBox::setCompleter().

990 {
991  Q_D(QCompleter);
992  if (d->widget)
993  d->widget->removeEventFilter(this);
994  d->widget = widget;
995  if (d->widget)
996  d->widget->installEventFilter(this);
997  if (d->popup) {
998  d->popup->hide();
999  d->popup->setFocusProxy(d->widget);
1000  }
1001 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
QWidget * widget() const
Returns the widget for which the completer object is providing completions.

◆ setWrapAround

void QCompleter::setWrapAround ( bool  wrap)
slot

Definition at line 1581 of file qcompleter.cpp.

1582 {
1583  Q_D(QCompleter);
1584  if (d->wrap == wrap)
1585  return;
1586  d->wrap = wrap;
1587 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ splitPath()

QStringList QCompleter::splitPath ( const QString path) const
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.

See also
pathFromIndex(), {Handling Tree Models}

Reimplemented in QFSCompleter.

Definition at line 1780 of file qcompleter.cpp.

Referenced by setCompletionPrefix().

1781 {
1782  bool isDirModel = false;
1783  bool isFsModel = false;
1784 #ifndef QT_NO_DIRMODEL
1785  Q_D(const QCompleter);
1786  isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0;
1787 #endif
1788 #ifndef QT_NO_FILESYSTEMMODEL
1789 #ifdef QT_NO_DIRMODEL
1790  Q_D(const QCompleter);
1791 #endif
1792  isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) != 0;
1793 #endif
1794 
1795  if ((!isDirModel && !isFsModel) || path.isEmpty())
1796  return QStringList(completionPrefix());
1797 
1798  QString pathCopy = QDir::toNativeSeparators(path);
1799  QString sep = QDir::separator();
1800 #if defined(Q_OS_SYMBIAN)
1801  if (pathCopy == QLatin1String("\\"))
1802  return QStringList(pathCopy);
1803 #elif defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
1804  if (pathCopy == QLatin1String("\\") || pathCopy == QLatin1String("\\\\"))
1805  return QStringList(pathCopy);
1806  QString doubleSlash(QLatin1String("\\\\"));
1807  if (pathCopy.startsWith(doubleSlash))
1808  pathCopy = pathCopy.mid(2);
1809  else
1810  doubleSlash.clear();
1811 #endif
1812 
1813  QRegExp re(QLatin1Char('[') + QRegExp::escape(sep) + QLatin1Char(']'));
1814  QStringList parts = pathCopy.split(re);
1815 
1816 #if defined(Q_OS_SYMBIAN)
1817  // Do nothing
1818 #elif defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
1819  if (!doubleSlash.isEmpty())
1820  parts[0].prepend(doubleSlash);
1821 #else
1822  if (pathCopy[0] == sep[0]) // readd the "/" at the beginning as the split removed it
1823  parts[0] = QDir::fromNativeSeparators(QString(sep[0]));
1824 #endif
1825 
1826  return parts;
1827 }
double d
Definition: qnumeric_p.h:62
The QFileSystemModel class provides a data model for the local filesystem.
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
QString completionPrefix() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
#define Q_D(Class)
Definition: qglobal.h:2482
static QChar separator()
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows...
Definition: qdir.cpp:1831
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
static QString fromNativeSeparators(const QString &pathName)
Returns pathName using &#39;/&#39; as file separator.
Definition: qdir.cpp:848
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812
The QDirModel class provides a data model for the local filesystem.
Definition: qdirmodel.h:59
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static QString escape(const QString &str)
Returns the string str with every regexp special character escaped with a backslash.
Definition: qregexp.cpp:4392

◆ widget()

QWidget * QCompleter::widget ( ) const

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

See also
setWidget()

Definition at line 1008 of file qcompleter.cpp.

Referenced by QLineEdit::setCompleter(), and setWidget().

1009 {
1010  Q_D(const QCompleter);
1011  return d->widget;
1012 }
double d
Definition: qnumeric_p.h:62
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
#define Q_D(Class)
Definition: qglobal.h:2482

◆ wrapAround()

bool QCompleter::wrapAround ( ) const

Referenced by setWrapAround().

Properties

◆ caseSensitivity

Qt::CaseSensitivity QCompleter::caseSensitivity
private

the case sensitivity of the matching

The default is Qt::CaseSensitive.

See also
completionColumn, completionRole, modelSorting

Definition at line 73 of file qcompleter.h.

Referenced by QLineControl::complete(), and QComboBoxPrivate::matchFlags().

◆ completionColumn

int QCompleter::completionColumn
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.

See also
completionRole, caseSensitivity

Definition at line 70 of file qcompleter.h.

◆ completionMode

QCompleter::CompletionMode QCompleter::completionMode
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().

◆ completionPrefix

QString QCompleter::completionPrefix
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().

◆ completionRole

int QCompleter::completionRole
private

the item role to be used to query the contents of items for matching.

The default role is Qt::EditRole.

See also
completionColumn, caseSensitivity

Definition at line 71 of file qcompleter.h.

◆ maxVisibleItems

int QCompleter::maxVisibleItems
private

the maximum allowed size on screen of the completer, measured in items

Since
4.6

By default, this property has a value of 7.

Definition at line 72 of file qcompleter.h.

◆ modelSorting

QCompleter::ModelSorting QCompleter::modelSorting
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.

See also
setCaseSensitivity(), QCompleter::ModelSorting

Definition at line 68 of file qcompleter.h.

◆ wrapAround

bool QCompleter::wrapAround
private

the completions wrap around when navigating through items

Since
4.3

The default is true.

Definition at line 74 of file qcompleter.h.

Referenced by QLineControl::advanceToEnabledItem().


The documentation for this class was generated from the following files: