Qt World Summit: Register Today!

Using QCompleter with a custom Model.

  • Hi there!

    I having problems with a QCompleter. That I have is a QLineEdit in witch the user must to put a Customer name and I want to autocomplete with the names of the customers on my system. I have the Customer class witch has the name method and a class CustomerCollection to handle a list of customers.

    To use the QCompleter, I choose for create an Item Model, well, I create the class CustomerItemModel and it's content is:

    class CustomerItemModel : public QAbstractListModel

    CustomerItem(const CustomerCollection &col, QObject *parent = 0)
    : QAbstractListModel(parent), collection(col) {}

    int rowCount(const QModelIndex &parent = QModelIndex()) const{
    return this->collection.count();
    QVariant data(const QModelIndex &index, int role) const{
    return collection.at(index.row()).name();

    CustomerCollection collection;

    Well... I create a QCompleter with a collection of all the customers on my system, but the autocomplete does not works...

    Any suggestion?

  • Consider following statements in Qt document

    bq. When subclassing QAbstractItemModel, at the very least you must implement index(), parent(), rowCount(), columnCount(), and data(). These functions are used in all read-only models, and form the basis of editable models.

    In your case, your class derived from QAbstractListModel which is override index() function, so you should implement remained virtual functions. First check whether your gui code is correct by using just simple QStringList (using QCompleter(QStringList&...) constructor) is also good starting point I think.

    Good luck

Log in to reply