1

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"?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
MR.JD
  • 45
  • 1
  • 8
  • If you want to show database content, the right way is to use `QTableView` with a model. Then you can search on the model using `QAbstractItemModel::match` or `QSortFilterProxyModel`. – Nejat Jan 20 '15 at 04:29

1 Answers1

3

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.

Community
  • 1
  • 1
sashoalm
  • 75,001
  • 122
  • 434
  • 781