MR.JD
MR.JD

Reputation: 45

Filter contents of a QTableView connected to SQLite database via QSqlTableModel

How can I filter the contents of a QTableView that is connected to a SQLite database via QSqlTableModel?

For example, if the database contains a "name" column, I want to show only the rows where the name is "Jack"?

Upvotes: 1

Views: 1041

Answers (1)

sashoalm
sashoalm

Reputation: 79457

You can use QSqlTableModel::setFilter(). The filtering will be done by the model, and the view will automatically update itself to show only the filtered items.

The filter is a SQL WHERE clause without the keyword WHERE (for example, name='Josephine').

If the model is already populated with data from a database, the model re-selects it with the new filter. Otherwise, the filter will be applied the next time select() is called.

Upvotes: 3

Related Questions