Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QCompleter custom model not working
Forum Updated to NodeBB v4.3 + New Features

QCompleter custom model not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 264 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haris123
    wrote on last edited by haris123
    #1

    I have to use Qcompleter with custom data one is text and other is some id.

        QStringList cities;
        cities << "Moscow" << "London" << "Las Vegas" << "New York"<< "New York";
        QStringList ids;
        ids << "0" << "1" << "2" << "3"<< "4";
    
        completer_model *cModel = new completer_model();
        cModel->populateData(ids,cities);
    
        QCompleter *completer1 = new QCompleter(this);
        ui->lineEdit->setCompleter(completer1);
       completer1->setModel(cModel);
    

    And I have implemented custom model like below.

    completer_model::completer_model(QObject *parent)
        : QAbstractListModel{parent}
    {
    
    }
    
    int completer_model::rowCount(const QModelIndex &parent) const{
        return data_list.count();
    }
    
    
    QVariant completer_model::data(const QModelIndex &index, int role) const{
        int r = index.row();
    
        if (!index.isValid()) return QVariant();
         if (role == Qt::DisplayRole) {
                   return  data_list[r].display_str;
         }
       return QVariant();
    
    
    }
    
    
    void completer_model::populateData(QStringList display_str, QStringList ids){
    
        for(int i=0;i<display_str.length();i++){
            completer_model::beginResetModel();
    
            algo_creation_search_box_model_struct tmp;
            tmp.display_str =display_str[i];
            tmp.id=ids[i];
            data_list.append(tmp);
            completer_model::endResetModel();
    
        }
    }
    

    But when I type no popup shows.

    M 1 Reply Last reply
    0
    • H haris123

      I have to use Qcompleter with custom data one is text and other is some id.

          QStringList cities;
          cities << "Moscow" << "London" << "Las Vegas" << "New York"<< "New York";
          QStringList ids;
          ids << "0" << "1" << "2" << "3"<< "4";
      
          completer_model *cModel = new completer_model();
          cModel->populateData(ids,cities);
      
          QCompleter *completer1 = new QCompleter(this);
          ui->lineEdit->setCompleter(completer1);
         completer1->setModel(cModel);
      

      And I have implemented custom model like below.

      completer_model::completer_model(QObject *parent)
          : QAbstractListModel{parent}
      {
      
      }
      
      int completer_model::rowCount(const QModelIndex &parent) const{
          return data_list.count();
      }
      
      
      QVariant completer_model::data(const QModelIndex &index, int role) const{
          int r = index.row();
      
          if (!index.isValid()) return QVariant();
           if (role == Qt::DisplayRole) {
                     return  data_list[r].display_str;
           }
         return QVariant();
      
      
      }
      
      
      void completer_model::populateData(QStringList display_str, QStringList ids){
      
          for(int i=0;i<display_str.length();i++){
              completer_model::beginResetModel();
      
              algo_creation_search_box_model_struct tmp;
              tmp.display_str =display_str[i];
              tmp.id=ids[i];
              data_list.append(tmp);
              completer_model::endResetModel();
      
          }
      }
      

      But when I type no popup shows.

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @haris123 said in QCompleter custom model not working:

      QCompleter *completer1 = new QCompleter(this);
      ui->lineEdit->setCompleter(completer1);

      I think you need to set the model as well:
      completer1->setModel(cModel);

      H 1 Reply Last reply
      2
      • M mpergand

        @haris123 said in QCompleter custom model not working:

        QCompleter *completer1 = new QCompleter(this);
        ui->lineEdit->setCompleter(completer1);

        I think you need to set the model as well:
        completer1->setModel(cModel);

        H Offline
        H Offline
        haris123
        wrote on last edited by
        #3

        @mpergand Sorry to not included the full code, I already done that. But the result is same, no popup came while start typing.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved