0

I want to implement some kind of search in ComboBox with lots of items. It contains ~ 500 lines, ordered by name. So I want to do it in manner when user press A key it shows only lines begin with A... , when then press s it shows lines begin with As... etc. But Key events only work if ComboBox is closed. If dropdown list is shown no key event occurs, except some inner events like arrows and Esc/Enter.

Any ideas how to do that? Any advices and suggestions will be greatly appreciated!

folibis
  • 12,048
  • 6
  • 54
  • 97

1 Answers1

2

You don't need so hard code, there is extremely easy and fast solution. There is special class for autocompletion - QCompleter. So answer is simple, create QCompleter with needed data and set it, to comboBox using setCompleter() setter.

QCompleter *completer = new QCompleter(mdl, this);//mdl is a model with some data
completer->setCaseSensitivity(Qt::CaseInsensitive);
ui->comboBox->setCompleter(completer);

Result without completer(original comboBox):

enter image description here

Result with completer:

enter image description here

Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • 1
    You can simply install the completer directly on the QCombobox, no reason to do the const_cast dance and access the lineEdit. `comboBox->setCompleter( completer )` http://doc.qt.io/qt-5/qcombobox.html#setCompleter – Matthias Kuhn Feb 27 '15 at 06:34
  • @MatthiasKuhn Yes, you are absolutely right, didn't notice this method, noticed only lineEdit(), so I should do const_cast dance... Anyways, thank you very much, I edited my answer – Jablonski Feb 27 '15 at 06:40
  • 2
    OP asked about ComboBox in QtQuick, not QComboBox in QtWitdgets – GrecKo Feb 27 '15 at 08:32
  • @GrecKo Hmm, it seems that you are right...With QtQuick it seems to be impossible, with c++ it can be done much and much easier. Unfortunately I have no option but to wait for OP reaction. – Jablonski Feb 27 '15 at 08:53
  • There is autocompletion on ComboBox in qml too, just make it editable and assign it a model, but it doesn't work like OP asked (not editable while the list is open and doesn't show the filtered results) – GrecKo Feb 27 '15 at 09:00
  • Uhm...definitely right! I was just testing it. Put it as an answer @GrecKo. Still you don't have the list, as a `QCompleter` has. – BaCaRoZzo Feb 27 '15 at 09:02
  • Thanks guys for your answers! Yes I know about `QCompleter`. But as @GrecKo said I need it in `QML`. It would be great feature in `QML` but now it's impossible. – folibis Feb 27 '15 at 09:43
  • 1
    You can go for the `QComboBox` embedding...even though I think it's not worth the effort. A custom approach would be the solution (and should be quite easy to implement). – BaCaRoZzo Feb 27 '15 at 10:12
  • Yes @BaCaRoZzo, you are right, I also desided to implement my own item - just Button + ListView. – folibis Feb 27 '15 at 21:21
  • I would say `TextField` + bottom-anchored `Flickable` with inside a `Column`. That sounds better to be. Well, I guess it's just a matter of taste. :) – BaCaRoZzo Feb 27 '15 at 21:49