QSqlTableModel Filter
-
I have a SQLite data base that I am displaying with the help of "QSqlTableModel" and "QTabelView". Now I am trying to set a filter for my model.
Here is the code:
void DataBase::method_set_filter(const QString &filterArgument) { QString command( "'%1' LIKE '%%2%'" ); command = command.arg( // 1 DataBase::COLUMN_FIRSTNAME, // Stored as static const in my class // 2 filterArgument ); qDebug() << command; qDebug() << tableModel->lastError(); tableModel->setFilter(command); tableModel->select(); }
Here is how my table looks before setting filter:
If I try to search for the first name of "Jacob", then it will show me nothing:
What my debug statements show:
"'First name' LIKE '%Jacob%'" QSqlError("", "", "")
Why is this happening?
-
-
bool QSqlTableModel::select() returns a
bool
so that you can test it. -
tableModel->lastError()
won't return anything immediately after you have setQString command
, that is just a string variable assignment. Examine it after theselect()
returns false. -
Don't name database columns with spaces in them, it's just asking for trouble, because...
-
...Most importantly:
'First name' LIKE '%Jacob%'
where do you ever get that SQLite uses'...'
(single quotes) to enclose a column name with space in it? It doesn't. Consequently filtering with most strings, includeJacob
, will always return no rows; if you try filtering by, say,rst
it will return every row :)
-