QSqlQueryModel + QTableView + QCompleter = filter



  • hi,

    I have a lineEdit and a tableView in my form. I would like to create a filter that narrows down the data existing in my database with the text entered in lineEdit; the data is shown in the tableView.

    the connection to the database is working and tested
    i have to following code:

    @
    QSqlQuery q1(g); // g is my database connection
    q1.prepare("SELECT * FROM name where stname like ':lEstname'");
    q1.bindValue(":lEstname", ui->lEstname->text());
    QSqlQueryModel *filter = new QSqlQueryModel();
    q1.exec();
    filter->setQuery(q1);
    QCompleter *completerfilter = new QCompleter (filter);
    ui->lEstname->setCompleter(completerfilter);
    ui->tableView->setModel(filter);
    @

    when I run, the result is the model columns headers with no data resulted from the query, just the headers, because the query is executed before I enter the text, basically with lEstname empty.

    expected result would be: have all possible text entry shown in lEstname, with the QCompleter
    and query results entered in tableView.

    what I'm doing wrong?
    thanks



  • my biggest challenge is to understand and find a way to execute the query after the lEstname input. how this should be done?



  • hi,

    let's say my above approach is complete wrong; for the purpose of filtering I must use QSortFilterProxyModel.

    I modified my code to use the above class into:
    @
    void stfilter::on_lEgroup_textEdited(const QString &arg1)
    {
    QSqlDatabase g2 = QSqlDatabase::database("connDb"); //g2 is a temporary database
    if (g2.open())
    {
    QSqlQuery qgroup(g2);
    qgroup.prepare("SELECT * FROM stfilter where group like ':lEgroup'");
    qgroup.bindValue(":lEgroup", ui->lEgroup->text());
    qgroup.exec();
    QSqlQueryModel *stfilter2 = new QSqlQueryModel();
    stfilter2->setQuery(qgroup);
    QSortFilterProxyModel *stproxym = new QSortFilterProxyModel(this);
    stproxym->setSourceModel(stfilter2);
    ui->tblv_students->setModel(stproxym);
    }else
    {
    qDebug() << "connection g2 not opened" << g2.lastError(); // for test
    g2.close();
    }
    }
    @

    as soon as I edit the text in lEgroup, the signal is lauched, but the result shown in table view is the same: headers are shown, but no entry reported in the table view

    once again, the db connection is open and working, the table in database has text entries in column 'group' starting with the the letters I'm inserting in the lEgroup, but the result is missing from table view.

    result expected: report all the student from 'stfilter' view which are enrolled in group typed in 'lEgroup'

    what's wrong?
    thank you.



  • Perhaps you should add a * behind your :lEgroup argument in your query. That would allow searching with an empty string, as well as getting inexact matches (match "smith" if the input is just "smi").


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.