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. QSqlQueryModel + QTableView + QCompleter = filter
QtWS25 Last Chance

QSqlQueryModel + QTableView + QCompleter = filter

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.1k Views
  • 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.
  • A Offline
    A Offline
    angelicaP
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      angelicaP
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        angelicaP
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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").

          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