0

I am using QSqlTableModel *modelcompleter, and then using QCompleter to load the data into the qLineEdit *search

    connect(ui->btnSearch, SIGNAL(clicked()), SLOT(search()));
    modelcompleter->setTable("cust");
    modelcompleter->setEditStrategy(QSqlTableModel::OnManualSubmit);
    modelcompleter->select();
    QCompleter *searchCompleter = new QCompleter(modelcompleter);
    searchCompleter->setCompletionColumn(1);
    searchCompleter->setCaseSensitivity(Qt::CaseInsensitive);
    ui->search->setCompleter(searchCompleter);

I am using qlineEdit to hit enter on the item that I want to choose:

 connect(ui->search, SIGNAL(returnPressed()), SLOT(search()));

the function

void search(){
cust_name = ui->search->text();
    modelsearch->setTable("cust");
    modelsearch->setEditStrategy(QSqlTableModel::OnManualSubmit);
    modelsearch->setFilter(QString("cust_name like '%%1%'").arg(cust_name));
    modelsearch->select();
    qDebug() << modelsearch->record(0).value("id").toInt(); }

I don't understand why but the modelsearch which currently sets to only 1 record that it gets when I hit enter (QCompleter makes sure I submit the full 'cust_name'). QDebug() outputs 0. Is there another way to get the id of the 'cust_name' that matches the search query 'cust_name'

awaisharoon
  • 463
  • 1
  • 3
  • 16

1 Answers1

1
qDebug() << modelsearch->record(0).value("id").toInt();

instead of this. I used

QSqlQueryModel *query = new QSqlQueryModel;
query->setQuery(QString("SELECT id from cust WHERE cust_name like '%%1%'").arg(cust_name));
qDebug() << query->record(0).value("id").toInt();

and its now fetching the id of the record!

awaisharoon
  • 463
  • 1
  • 3
  • 16