folibis
folibis

Reputation: 12874

Is it possible to detect key event in opened ComboBox?

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!

Upvotes: 0

Views: 907

Answers (1)

Jablonski
Jablonski

Reputation: 18524

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

Upvotes: 2

Related Questions