I have an application where people can enter names of places in a form. This being Europe, we have to deal with names that includes diacritics like Orléans, Köln, Liège, Châteauroux. When people enter names I want them to be able to type characters without diacritics but still come up with a list of the names that include them so they can select the properly accented name. The program has a long but non-exhaustive list of names (people can always enter any name they like).
I already have a function that finds names based on a non-diacritic match. So 'orle' will return 'Orléans', 'kol' finds 'Köln', etc.
I tried two things:
1: A QLineEdit with a QCompleter that fills the list in the completer with matches using a QStringListModel. Unfortunately this does not work since the list will contain the accented version of the name, which does not match the value entered by the user, so QLineEdit does not show the name in the popup (if at all).
I also played with a QAbstractItemModel until I realized QCompleter does a string match on the data returned by the Model, so again 'orle' != 'orlé'.
2: An editable QComboBox which list gets filled dynamically depending on the text that has been entered so far. The following code is connected()ed from QComboBox::editTextChanged(QString):
void TripFormCargoHelper::fromEdited (const QString &str)
{
if (str.length () >= 3)
{
QStringList flist = m_database->findLocationStrings (str);
flist.push_front (str); // add the text we're editing first
bool b = box->blockSignals (true); // prevent recursive signals
box->clear ();
box->addItems (flist);
box->blockSignals (b);
box->showPopup ();
}
else
{
box->clear ();
box->hidePopup ();
}
}
This works, but only half... I want the popup to appear when some characters have been entered [1] but this removes the focus from the line-edit. Clicking the line-edit closes the popup, so I end up with a catch-22 (people should be able to continue typing characters, narrowing the search).
Any suggestions on how to make this work would be appreciated. I prefer a solution with QLineEdit. Version is Qt 5.4.
[1] Should be when I find some matches, but alas.