Lost in all the Qt 4.2 publicity for Widget Style Sheets and Graphics View is the auto completion framework which makes it easy to provide auto completions in just about any widget. The framework is really just one class – QCompleter. The completer provides support for three completion modes – Inline, Popup and Unfiltered Popup. It can provide completion from any role and any column of an QAbstractItemModel. You can iterate and query the completions. What more, it is super optimized for large sorted and unsorted models.
It is not hard to imagine where the QCompleter can be first put to use. Practically just about every application has the line edit and browse button combination. Using QCompleter, providing auto completions for file names from the file system is straightforward,
QCompleter *completer = new QCompleter(fileNameEdit);
QDirModel *dirModel = new QDirModel(completer);
completer->setModel(dirModel);
fileNameEdit->setCompleter(completer); // new in 4.2!
QCompleter can even be used in a QComboBox. By default, a QComboBox provides inline completions from the items in its drop down list. With the introduction of QCompleter, you can set up QComboBox to provide auto completions from a model independent of the items in the drop down list. The user can use the drop down list to view items previously entered.
QCompleter *completer = new QCompleter(fileNameCombo);
QDirModel *dirModel = new QDirModel(completer);
completer->setModel(dirModel);
completer->setCompletionMode(QCompleter::PopupCompletion); // check out other completion modes!
fileNameCombo->setCompleter(completer);
Providing completions in a custom widget is all about calling complete() and setCompletionPrefix() in the right places. Check out the custom completer example for details.