Qt World Summit: Submit your Presentation

Implement smart search in qcombobox

  • Hi to all,

    I use to load the name and the surname of a list of users into a qcombobox.

    model.setQuery("SELECT * FROM utente ");
       for (int i = 0; i < model.rowCount(); ++i) {
         ui->listautenti->addItem(model.record(i).value("nome").toString() + " " + model.record(i).value("cognome").toString() + " #" + model.record(i).value("id").toString());

    the qcombobox is editable and I can select items by typing the first letter of the user's name.
    However, I would like to be able to implement some kind of smart search in order to select all the items that match what I write inside the combobox.

    For example, if I write:


    the following items should be proposed:
    Jack Moss
    Michael Jackson
    Pierce Jackssen

    and so on.

    It is possible to do something like that in Qt?

  • @Marcus-Barnet
    I don't know whether you can use a standalone QComboBox for this --- doubtless you can write some dedicated code perhaps to accomplish it --- but are you aware of Qt's QCompleter Class for just this?

    You can use QCompleter to provide auto completions in any Qt widget, such as QLineEdit and QComboBox. When the user starts typing a word, QCompleter suggests possible ways of completing the word, based on a word list. The word list is provided as a QAbstractItemModel. (For simple applications, where the word list is static, you can pass a QStringList to QCompleter's constructor.)

Log in to reply