Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSqlQueryModel + QTableView + QCompleter = filter
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'");
QSqlQueryModel *filter = new QSqlQueryModel();
QCompleter *completerfilter = new QCompleter (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?
my biggest challenge is to understand and find a way to execute the query after the lEstname input. how this should be done?
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
qgroup.prepare("SELECT * FROM stfilter where group like ':lEgroup'");
QSqlQueryModel *stfilter2 = new QSqlQueryModel();
QSortFilterProxyModel *stproxym = new QSortFilterProxyModel(this);
qDebug() << "connection g2 not opened" << g2.lastError(); // for test
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'
andre last edited by
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").