[Solved] Check if value is part of the list in qcombobox
-
Hi,
I have got an editable qcombobox that gets the listbox from a related QSqlRelationalTableModel.
Now I want to check if the entered value is part of the list after the combobox looses the focus.So I installed the eventfilter like this:
ui->cbVoucherPosItemKey->installEventFilter(this);
And then I catch the focusout-event of the combobox:
bool BVoucherPosForm::eventFilter(QObject *object, QEvent *event) { if (event->type() == QEvent::FocusOut) { if (object == ui->cbVoucherPosItemKey) { qDebug() << "ComboIndex: "<<ui->cbVoucherPosItemKey->findText(ui->cbVoucherPosItemKey->currentText()); } } return false; }
I want to get -1 if the value is not part of the list. But I get also -1 if I write the first letters of a string that is in the list (because it is an editable combobox, the value of the list is autocomplete) and push the TAB-button to move the focus (e.g. after typing "abc", the combobox is selecting the string "abcde" in the list, but I get -1 in my example above).
If I type the correct String in the combobox (e.g. "abcde"), I get the right index in my qDebug message. So I want to get the right index when I type "abc" too.
My question:
-- Is there a possibility to wait in the focusout-event for the finished selection in the combobox ??
or
-- Is there a smarter way???
Thank you!
-
I found out, that
qDebug()<<"item: "<<ui->cbVoucherPosItemKey->currentText();
retourns me the right string, when I copy the code to the place in the eventFilter-function where the other qDebug code is standing.
The problem in my example is, that the values of the list are beginning with capital letters (like "Abcde"). So, how do I use combobox::findtext without case-sensitiv??Thank you!
-
Hi,
as you already found out, QComboBox has a method called findText , The second parameter holds the Flags for matching. Default is Qt::MatchExactly | Qt::MatchCaseSensitive. Flags that can be set are defined here:
http://doc.qt.io/qt-5.5/qt.html#MatchFlag-enumSo for example if you have a ComboBox that contains "Level" as Text
qDebug() << combobox->findText("level"); //prints -1, not found qDebug() << combobox->findText("level",Qt::MatchContains); //prints the first index that contains "level"
Hope that helps
-
Hi,
Isn't QCompleter what you are looking for ?
-
Thank you to the_ and SGaist!
I realised it like the_ explained. In my case it is the easiest way and it works (in a combobox with a list of values from a related table, I want to know if the entered string is part of the list and if not (index = -1) I open a dialog to add a new value in the related table).
But I admit, QCompleter is also a smart solution. Maybe in the future...