11

Say I have 10 names in a QListWidget (which is hidden) and an a QLineEdit. Now if I type the letter "a" in the line Edit it should display a drop down of all those name in the list widget that begin with the letter "A". the user could select using a mouse or a keyboard (since there will be a vertical scroll-bar). I am not sure if a QLineEdit could do this. But I would like to know what is out there to accomplish this.

Nejat
  • 31,784
  • 12
  • 106
  • 138
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158

1 Answers1

24

You can use QCompleter which provides a way for autocompletion in widgets like QLineEdit and QComboBox. When the user starts typing a word, QCompleter suggests possible ways of completing the word, based on a word list.

An example from the Qt documentation:

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);
djvg
  • 11,722
  • 5
  • 72
  • 103
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Thanks Ill give this a try and post back – Rajeshwar Jun 16 '14 at 17:24
  • 2
    You don't even need to populate the `QCompleter` with data manually, you can just pass the model that your `QListWidget` is already using by calling the `QAbstractItemView::model()` function. – RobbieE Jun 16 '14 at 18:48
  • I lot more straightforward and to the point than the QtProject example. Thanks. – bmahf Mar 03 '15 at 14:27
  • Nix that. The QCompleter page actually gives this very example. My bad. – bmahf Mar 03 '15 at 19:07