QCompleter Class Reference

#include <qcompleter.h>

Inheritance diagram for QCompleter:

Inheritance graph
[legend]
Collaboration diagram for QCompleter:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QCompleter class provides completions based on a 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.)

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: @code QStringList wordList; wordList << "alpha" << "omega" << "omicron" << "zeta"; QLineEdit *lineEdit = new QLineEdit(this); QCompleter *completer = new QCompleter(wordList, this); completer->setCaseSensitivity(Qt::CaseInsensitive); lineEdit->setCompleter(completer); \endcode A QDirModel can be used to provide auto completion of file names. For example: @code QCompleter *completer = new QCompleter(this); completer->setModel(new QDirModel(completer)); lineEdit->setCompleter(completer); \endcode To set the model on which QCompleter should operate, call setModel(). By default, QCompleter will attempt to match the \l {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 \l{QAbstractListModel}{list model}, a \l{QAbstractTableModel}{table model}, or a \l{QAbstractItemModel}{tree model}. Completion on tree models is slightly more involved and is covered in the \l{Handling Tree Models} section below. The completionMode() determines the mode used to provide completions to the user. @section 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: @code for (int i = 0; completer->setCurrentRow(i); i++) qDebug() << completer->currentCompletion() << " is match number " << i; \endcode 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. @section 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. @section 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) QDirModel. The completion occurs for every element in the path. For example, if the current text is \c C:\Wind, QCompleter might suggest \c Windows to complete the current path element. Similarly, if the current text is \c C:\Windows\Sy, QCompleter might suggest \c 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 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 QDirModel. 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 completionRole() for list models and the absolute file path if the mode is a QDirModel. \sa QAbstractItemModel, QLineEdit, QComboBox, {Completer Example} Definition at line 44 of file qcompleter.h.

Public Types

enum  CompletionMode
enum  ModelSorting

Public Slots

void setCompletionPrefix (const QString &prefix)
void complete (const QRect &rect=QRect())

Signals

void activated (const QString &text)
void activated (const QModelIndex &index)
void highlighted (const QString &text)
void highlighted (const QModelIndex &index)

Public Member Functions

 QCompleter (QObject *parent=0)
 QCompleter (QAbstractItemModel *model, QObject *parent=0)
 QCompleter (const QStringList &completions, QObject *parent=0)
 ~QCompleter ()
void setWidget (QWidget *widget)
QWidgetwidget () const
void setModel (QAbstractItemModel *c)
QAbstractItemModelmodel () const
void setCompletionMode (CompletionMode mode)
CompletionMode completionMode () const
QAbstractItemViewpopup () const
void setPopup (QAbstractItemView *popup)
void setCaseSensitivity (Qt::CaseSensitivity caseSensitivity)
Qt::CaseSensitivity caseSensitivity () const
void setModelSorting (ModelSorting sorting)
ModelSorting modelSorting () const
void setCompletionColumn (int column)
int completionColumn () const
void setCompletionRole (int role)
int completionRole () const
int completionCount () const
bool setCurrentRow (int row)
int currentRow () const
QModelIndex currentIndex () const
QString currentCompletion () const
QAbstractItemModelcompletionModel () const
QString completionPrefix () const
virtual QString pathFromIndex (const QModelIndex &index) const
virtual QStringList splitPath (const QString &path) const

Protected Member Functions

bool eventFilter (QObject *o, QEvent *e)
bool event (QEvent *)

Private Member Functions

 Q_PRIVATE_SLOT (d_func(), void _q_complete(QModelIndex)) Q_PRIVATE_SLOT(d_func()


Member Enumeration Documentation

enum QCompleter::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 selected.

See also:
setCompletionMode()

Definition at line 55 of file qcompleter.h.

00055                         {
00056         PopupCompletion,
00057         UnfilteredPopupCompletion,
00058         InlineCompletion
00059     };

enum QCompleter::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()

Definition at line 61 of file qcompleter.h.


Constructor & Destructor Documentation

QCompleter::QCompleter ( QObject parent = 0  ) 

Constructs a completer object with the given parent.

Definition at line 802 of file qcompleter.cpp.

References d.

00803 : QObject(*new QCompleterPrivate(), parent)
00804 {
00805     Q_D(QCompleter);
00806     d->init();
00807 }

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 813 of file qcompleter.cpp.

References d, and model().

00814     : QObject(*new QCompleterPrivate(), parent)
00815 {
00816     Q_D(QCompleter);
00817     d->init(model);
00818 }

Here is the call graph for this function:

QCompleter::QCompleter ( const QStringList list,
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 825 of file qcompleter.cpp.

References d.

00826 : QObject(*new QCompleterPrivate(), parent)
00827 {
00828     Q_D(QCompleter);
00829     d->init(new QStringListModel(list, this));
00830 }

QCompleter::~QCompleter (  ) 

Destroys the completer object.

Definition at line 836 of file qcompleter.cpp.

00837 {
00838 }


Member Function Documentation

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 849 of file qcompleter.cpp.

References d, setCompletionMode(), and widget().

Referenced by TextEdit::setCompleter().

00850 {
00851     Q_D(QCompleter);
00852     d->widget = widget;
00853     if (d->popup)
00854         d->popup->setFocusProxy(d->widget);
00855     setCompletionMode(d->mode); // will install event filter depending on mode
00856 }

Here is the call graph for this function:

QWidget * QCompleter::widget (  )  const

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

See also:
setWidget()

Definition at line 863 of file qcompleter.cpp.

References d.

Referenced by setWidget().

00864 {
00865     Q_D(const QCompleter);
00866     return d->widget;
00867 }

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 QDirModel, 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 880 of file qcompleter.cpp.

References Qt::CaseInsensitive, Qt::CaseSensitive, d, model(), setCaseSensitivity(), and setPopup().

00881 {
00882     Q_D(QCompleter);
00883     QAbstractItemModel *oldModel = d->proxy->model;
00884     d->proxy->setSourceModel(model);
00885     if (d->popup)
00886         setPopup(d->popup); // set the model and make new connections
00887     if (oldModel && oldModel->QObject::parent() == this)
00888         delete oldModel;
00889 #ifndef QT_NO_DIRMODEL
00890     if (qobject_cast<QDirModel *>(model)) {
00891 #ifdef Q_OS_WIN
00892         setCaseSensitivity(Qt::CaseInsensitive);
00893 #else
00894         setCaseSensitivity(Qt::CaseSensitive);
00895 #endif
00896     }
00897 #endif // QT_NO_DIRMODEL
00898 }

Here is the call graph for this function:

QAbstractItemModel * QCompleter::model (  )  const

Returns the model that provides completion strings.

See also:
completionModel()

Definition at line 905 of file qcompleter.cpp.

References d.

Referenced by QCompleter(), and setModel().

00906 {
00907     Q_D(const QCompleter);
00908     return d->proxy->sourceModel();
00909 }

void QCompleter::setCompletionMode ( CompletionMode  mode  ) 

Definition at line 929 of file qcompleter.cpp.

References d, InlineCompletion, and UnfilteredPopupCompletion.

Referenced by MainWindow::changeMode(), and setWidget().

00930 {
00931     Q_D(QCompleter);
00932 
00933     d->mode = mode;
00934     d->proxy->setFiltered(mode != QCompleter::UnfilteredPopupCompletion);
00935 
00936     if (mode == QCompleter::InlineCompletion) {
00937         if (d->widget)
00938             d->widget->removeEventFilter(this);
00939         return;
00940     }
00941 
00942     if (d->widget)
00943         d->widget->installEventFilter(this);
00944 }

QCompleter::CompletionMode QCompleter::completionMode (  )  const

Definition at line 946 of file qcompleter.cpp.

References d.

00947 {
00948     Q_D(const QCompleter);
00949     return d->mode;
00950 }

QAbstractItemView * QCompleter::popup (  )  const

Returns the popup used to display completions.

See also:
setPopup()

Definition at line 1000 of file qcompleter.cpp.

References d.

Referenced by TextEdit::keyPressEvent(), and setPopup().

01001 {
01002     Q_D(const QCompleter);
01003     return d->popup;
01004 }

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 967 of file qcompleter.cpp.

References QObject::connect(), d, QObject::disconnect(), QWidget::hide(), QObject::installEventFilter(), QAbstractItemView::model(), Qt::NoFocus, popup(), Qt::Popup, QWidget::setFocusPolicy(), QWidget::setFocusProxy(), QAbstractItemView::setItemDelegate(), QAbstractItemView::setModel(), QWidget::setParent(), SIGNAL, and SLOT.

Referenced by setModel().

00968 {
00969     Q_D(QCompleter);
00970     Q_ASSERT(popup != 0);
00971     if (d->popup) {
00972         QObject::disconnect(d->popup->selectionModel(), 0, this, 0);
00973         QObject::disconnect(d->popup, 0, this, 0);
00974     }
00975     if (d->popup != popup)
00976         delete d->popup;
00977     if (popup->model() != d->proxy)
00978         popup->setModel(d->proxy);
00979     popup->hide();
00980     popup->setParent(0, Qt::Popup);
00981     popup->setFocusPolicy(Qt::NoFocus);
00982     popup->setFocusProxy(d->widget);
00983     popup->installEventFilter(this);
00984     popup->setItemDelegate(new QCompleterItemDelegate(popup));
00985 
00986     QObject::connect(popup, SIGNAL(clicked(QModelIndex)),
00987                      this, SLOT(_q_complete(QModelIndex)));
00988     QObject::connect(popup, SIGNAL(clicked(QModelIndex)), popup, SLOT(hide()));
00989 
00990     QObject::connect(popup->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
00991                      this, SLOT(_q_completionSelected(QItemSelection)));
00992     d->popup = popup;
00993 }

Here is the call graph for this function:

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

Definition at line 1310 of file qcompleter.cpp.

References d.

Referenced by MainWindow::changeCase(), and setModel().

01311 {
01312     Q_D(QCompleter);
01313     if (d->cs == cs)
01314         return;
01315     d->cs = cs;
01316     d->proxy->createEngine();
01317     d->proxy->invalidate();
01318 }

Qt::CaseSensitivity QCompleter::caseSensitivity (  )  const

Definition at line 1320 of file qcompleter.cpp.

References d.

01321 {
01322     Q_D(const QCompleter);
01323     return d->cs;
01324 }

void QCompleter::setModelSorting ( ModelSorting  sorting  ) 

Definition at line 1233 of file qcompleter.cpp.

References d.

01234 {
01235     Q_D(QCompleter);
01236     if (d->sorting == sorting)
01237         return;
01238     d->sorting = sorting;
01239     d->proxy->createEngine();
01240     d->proxy->invalidate();
01241 }

QCompleter::ModelSorting QCompleter::modelSorting (  )  const

Definition at line 1243 of file qcompleter.cpp.

References d.

01244 {
01245     Q_D(const QCompleter);
01246     return d->sorting;
01247 }

void QCompleter::setCompletionColumn ( int  column  ) 

Definition at line 1260 of file qcompleter.cpp.

References d.

01261 {
01262     Q_D(QCompleter);
01263     if (d->column == column)
01264         return;
01265 #ifndef QT_NO_LISTVIEW
01266     if (QListView *listView = qobject_cast<QListView *>(d->popup))
01267         listView->setModelColumn(column);
01268 #endif
01269     d->column = column;
01270     d->proxy->invalidate();
01271 }

int QCompleter::completionColumn (  )  const

Definition at line 1273 of file qcompleter.cpp.

References d.

01274 {
01275     Q_D(const QCompleter);
01276     return d->column;
01277 }

void QCompleter::setCompletionRole ( int  role  ) 

Definition at line 1287 of file qcompleter.cpp.

References d.

01288 {
01289     Q_D(QCompleter);
01290     if (d->role == role)
01291         return;
01292     d->role = role;
01293     d->proxy->invalidate();
01294 }

int QCompleter::completionRole (  )  const

Definition at line 1296 of file qcompleter.cpp.

References d.

01297 {
01298     Q_D(const QCompleter);
01299     return d->role;
01300 }

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 1196 of file qcompleter.cpp.

References d.

01197 {
01198     Q_D(const QCompleter);
01199     return d->proxy->completionCount();
01200 }

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 1174 of file qcompleter.cpp.

References d.

01175 {
01176     Q_D(QCompleter);
01177     return d->proxy->setCurrentRow(row);
01178 }

int QCompleter::currentRow (  )  const

Returns the current row.

See also:
setCurrentRow()

Definition at line 1185 of file qcompleter.cpp.

References d.

01186 {
01187     Q_D(const QCompleter);
01188     return d->proxy->currentRow();
01189 }

QModelIndex QCompleter::currentIndex (  )  const

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

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

Definition at line 1351 of file qcompleter.cpp.

References d.

01352 {
01353     Q_D(const QCompleter);
01354     return d->proxy->currentIndex(false);
01355 }

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 1364 of file qcompleter.cpp.

References d, and pathFromIndex().

01365 {
01366     Q_D(const QCompleter);
01367     return pathFromIndex(d->proxy->currentIndex(true));
01368 }

Here is the call graph for this function:

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.

See also:
completionPrefix, model()

Definition at line 1377 of file qcompleter.cpp.

References d.

Referenced by TextEdit::keyPressEvent().

01378 {
01379     Q_D(const QCompleter);
01380     return d->proxy;
01381 }

QString QCompleter::completionPrefix (  )  const

Definition at line 1340 of file qcompleter.cpp.

References d.

Referenced by TextEdit::insertCompletion(), TextEdit::keyPressEvent(), and splitPath().

01341 {
01342     Q_D(const QCompleter);
01343     return d->prefix;
01344 }

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

Definition at line 1333 of file qcompleter.cpp.

References d, and splitPath().

Referenced by TextEdit::keyPressEvent().

01334 {
01335     Q_D(QCompleter);
01336     d->prefix = prefix;
01337     d->proxy->filter(splitPath(prefix));
01338 }

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 1142 of file qcompleter.cpp.

References d, InlineCompletion, QModelIndex::isValid(), PopupCompletion, and UnfilteredPopupCompletion.

Referenced by TextEdit::keyPressEvent().

01143 {
01144     Q_D(QCompleter);
01145     QModelIndex idx = d->proxy->currentIndex(false);
01146     if (d->mode == QCompleter::InlineCompletion) {
01147         if (idx.isValid())
01148             d->_q_complete(idx, true);
01149         return;
01150     }
01151 
01152     Q_ASSERT(d->widget != 0);
01153     if ((d->mode == QCompleter::PopupCompletion && !idx.isValid())
01154         || (d->mode == QCompleter::UnfilteredPopupCompletion && d->proxy->rowCount() == 0)) {
01155         d->popup->hide(); // no suggestion, hide
01156         return;
01157     }
01158 
01159     if (d->mode == QCompleter::UnfilteredPopupCompletion)
01160         d->setCurrentIndex(idx, false);
01161 
01162     d->showPopup(rect);
01163 }

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 {Qt::EditRole}{edit role} of the item for list models. It returns the absolute file path if the model is a QDirModel.

See also:
splitPath()

Definition at line 1393 of file qcompleter.cpp.

References QList< T >::clear(), QList< T >::count(), d, QAbstractItemModel::data(), Qt::EditRole, index, QModelIndex::isValid(), QStringList::join(), QModelIndex::parent(), QObject::parent(), QList< T >::prepend(), QDir::separator(), t, and QVariant::toString().

Referenced by currentCompletion().

01394 {
01395     Q_D(const QCompleter);
01396     if (!index.isValid())
01397         return QString();
01398 
01399     QAbstractItemModel *sourceModel = d->proxy->sourceModel();
01400 #ifndef QT_NO_DIRMODEL
01401     QDirModel *dirModel = qobject_cast<QDirModel *>(sourceModel);
01402     if (!dirModel)
01403 #endif
01404         return sourceModel->data(index, d->role).toString();
01405 
01406     QModelIndex idx = index;
01407     QStringList list;
01408     do {
01409         QString t = sourceModel->data(idx, Qt::EditRole).toString();
01410         list.prepend(t);
01411         QModelIndex parent = idx.parent();
01412         idx = parent.sibling(parent.row(), index.column());
01413     } while (idx.isValid());
01414 
01415 #ifndef Q_OS_WIN
01416     if (list.count() == 1) // only the separator or some other text
01417         return list[0];
01418     list[0].clear() ; // the join below will provide the separator
01419 #endif
01420 
01421     return list.join(QDir::separator());
01422 }

Here is the call graph for this function:

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

When used with list models, the first item in the returned list is used for matching.

See also:
pathFromIndex(), {Handling Tree Models}

Definition at line 1436 of file qcompleter.cpp.

References QString::clear(), completionPrefix(), d, QRegExp::escape(), QString::isEmpty(), QString::mid(), path, QDir::separator(), QString::split(), QString::startsWith(), and QDir::toNativeSeparators().

Referenced by setCompletionPrefix().

01437 {
01438     Q_D(const QCompleter);
01439     bool isDirModel = false;
01440 #ifndef QT_NO_DIRMODEL
01441     isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0;
01442 #endif
01443 
01444     if (!isDirModel || path.isEmpty())
01445         return QStringList(completionPrefix());
01446 
01447     QString pathCopy = QDir::toNativeSeparators(path);
01448     QString sep = QDir::separator();
01449 #ifdef Q_OS_WIN
01450     if (pathCopy == QLatin1String("\\") || pathCopy == QLatin1String("\\\\"))
01451         return QStringList(pathCopy);
01452     QString doubleSlash(QLatin1String("\\\\"));
01453     if (pathCopy.startsWith(doubleSlash))
01454         pathCopy = pathCopy.mid(2);
01455     else
01456         doubleSlash.clear();
01457 #endif
01458 
01459     QRegExp re(QLatin1String("[") + QRegExp::escape(sep) + QLatin1String("]"));
01460     QStringList parts = pathCopy.split(re);
01461 
01462 #ifdef Q_OS_WIN
01463     if (!doubleSlash.isEmpty())
01464         parts[0].prepend(doubleSlash);
01465 #else
01466     if (path[0] == sep[0]) // readd the "/" at the beginning as the split removed it
01467         parts[0] = sep[0];
01468 #endif
01469 
01470     return parts;
01471 }

Here is the call graph for this function:

bool QCompleter::eventFilter ( QObject o,
QEvent e 
) [protected, virtual]

Reimplemented from QObject.

Definition at line 1017 of file qcompleter.cpp.

References Qt::AltModifier, Qt::ControlModifier, d, QObject::eventFilter(), QEvent::FocusOut, QList< T >::isEmpty(), QModelIndex::isValid(), key, Qt::Key_Backtab, Qt::Key_Down, Qt::Key_End, Qt::Key_Enter, Qt::Key_Escape, Qt::Key_F4, Qt::Key_Home, Qt::Key_PageDown, Qt::Key_PageUp, Qt::Key_Return, Qt::Key_Tab, Qt::Key_Up, QEvent::KeyPress, QEvent::MouseButtonPress, o, QModelIndex::row(), QEvent::type(), and UnfilteredPopupCompletion.

01018 {
01019     Q_D(QCompleter);
01020 
01021     if (d->eatFocusOut && o == d->widget && e->type() == QEvent::FocusOut) {
01022         if (d->popup && d->popup->isVisible())
01023             return true;
01024     }
01025 
01026     if (o != d->popup)
01027         return QObject::eventFilter(o, e);
01028 
01029     switch (e->type()) {
01030     case QEvent::KeyPress: {
01031         QKeyEvent *ke = static_cast<QKeyEvent *>(e);
01032 
01033         QModelIndex curIndex = d->popup->currentIndex();
01034         QModelIndexList selList = d->popup->selectionModel()->selectedIndexes();
01035 
01036         const int key = ke->key();
01037         // In UnFilteredPopup mode, select the current item
01038         if ((key == Qt::Key_Up || key == Qt::Key_Down) && selList.isEmpty() && curIndex.isValid()
01039             && d->mode == QCompleter::UnfilteredPopupCompletion) {
01040               d->setCurrentIndex(curIndex);
01041               return true;
01042         }
01043 
01044         // Handle popup navigation keys. These are hardcoded because up/down might make the
01045         // widget do something else (lineedit cursor moves to home/end on mac, for instance)
01046         switch (key) {
01047         case Qt::Key_End:
01048         case Qt::Key_Home:
01049             if (ke->modifiers() & Qt::ControlModifier)
01050                 return false;
01051             break;
01052 
01053         case Qt::Key_Up:
01054             if (!curIndex.isValid()) {
01055                 int rowCount = d->proxy->rowCount();
01056                 QModelIndex lastIndex = d->proxy->index(rowCount - 1, 0);
01057                 d->setCurrentIndex(lastIndex);
01058                 return true;
01059             } else if (curIndex.row() == 0) {
01060                 d->setCurrentIndex(QModelIndex());
01061                 return true;
01062             }
01063             return false;
01064 
01065         case Qt::Key_Down:
01066             if (!curIndex.isValid()) {
01067                 QModelIndex firstIndex = d->proxy->index(0, 0);
01068                 d->setCurrentIndex(firstIndex);
01069                 return true;
01070             } else if (curIndex.row() == d->proxy->rowCount() - 1) {
01071                 d->setCurrentIndex(QModelIndex());
01072                 return true;
01073             }
01074             return false;
01075 
01076         case Qt::Key_PageUp:
01077         case Qt::Key_PageDown:
01078             return false;
01079         }
01080 
01081         // Send the event to the widget. If the widget accepted the event, do nothing
01082         // If the widget did not accept the event, provide a default implementation
01083         d->eatFocusOut = false;
01084         (static_cast<QObject *>(d->widget))->event(ke);
01085         d->eatFocusOut = true;
01086         if (!d->widget || e->isAccepted() || !d->popup->isVisible()) {
01087             // widget lost focus, hide the popup
01088             if (d->widget && !d->widget->hasFocus())
01089                 d->popup->hide();
01090             return true;
01091         }
01092 
01093         // default implementation for keys not handled by the widget when popup is open
01094         switch (key) {
01095         case Qt::Key_Return:
01096         case Qt::Key_Enter:
01097         case Qt::Key_Tab:
01098             d->popup->hide();
01099             if (curIndex.isValid())
01100                 d->_q_complete(curIndex);
01101             break;
01102 
01103         case Qt::Key_F4:
01104             if (ke->modifiers() & Qt::AltModifier)
01105                 d->popup->hide();
01106             break;
01107 
01108         case Qt::Key_Backtab:
01109         case Qt::Key_Escape:
01110             d->popup->hide();
01111             break;
01112 
01113         default:
01114             break;
01115         }
01116 
01117         return true;
01118     }
01119 
01120     case QEvent::MouseButtonPress:
01121         if (!d->popup->underMouse()) {
01122             d->popup->hide();
01123             return true;
01124         }
01125         return false;
01126 
01127     default:
01128         return false;
01129     }
01130 }

Here is the call graph for this function:

bool QCompleter::event ( QEvent ev  )  [protected, virtual]

Reimplemented from QObject.

Definition at line 1009 of file qcompleter.cpp.

References QObject::event().

01010 {
01011     return QObject::event(ev);
01012 }

Here is the call graph for this function:

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.

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.

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.

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.

QCompleter::Q_PRIVATE_SLOT ( d_func()  ,
void   _q_complete(QModelIndex) 
) [private]


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 17:04:04 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1