I suppose this post is both a comment and a question. I spent a few hours this evening trying to figure out how to add multiple values to setFilter(). After realizing that executing the setFilter() command multiple times results in the final setFilter() being the only one included in the resulting SQL query, I came up with a workaround: use the SQL AND operator to include all filters (see code block below). That's my comment. Now my question: is this the correct workaround to implement multiple filters in a relational table model, or is there a more elegant solution?

Qt Code:
  1. model->setTable("TestSuite");
  2. model->setRelation(model->fieldIndex("testID"), QSqlRelation("Tests", "id", "file"));
  3. model->setRelation(model->fieldIndex("algorithmID"), QSqlRelation("Algorithms", "id", "Name"));
  4. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
  5. ui->tableView_TestSuite->setModel(model);
  6.  
  7. //
  8. // relTblAl_4 is the Tests table; relTblAl_5 is the Algorithms table
  9. //
  10. // following code does not work and results in only relTblal_5.active='TRUE' as a WHERE clause in the SQL query
  11. // model->setFilter("TestSuite.active='TRUE');
  12. // model->setFilter("relTblAl_4.active='TRUE');
  13. // model->setFilter("relTblal_5.active='TRUE');
  14. //
  15. // following code block works as expected: all filters are part of the SQL WHERE clause
  16. model->setFilter("TestSuite.active='TRUE' AND relTblAl_4.active='TRUE' AND relTblal_5.active='TRUE'");
  17.  
  18. model->select();
  19. ui->tableView_TestSuite->setItemDelegate(new QSqlRelationalDelegate(ui->tableView_TestSuite));
  20. ui->tableView_TestSuite->show();
To copy to clipboard, switch view to plain text mode