PDA

View Full Version : Multiple setFilter values for QSqlRelationalTableModel



skaura
9th August 2011, 03:32
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?



QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this, db);
model->setTable("TestSuite");
model->setRelation(model->fieldIndex("testID"), QSqlRelation("Tests", "id", "file"));
model->setRelation(model->fieldIndex("algorithmID"), QSqlRelation("Algorithms", "id", "Name"));
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
ui->tableView_TestSuite->setModel(model);

//
// relTblAl_4 is the Tests table; relTblAl_5 is the Algorithms table
//
// following code does not work and results in only relTblal_5.active='TRUE' as a WHERE clause in the SQL query
// model->setFilter("TestSuite.active='TRUE');
// model->setFilter("relTblAl_4.active='TRUE');
// model->setFilter("relTblal_5.active='TRUE');
//
// following code block works as expected: all filters are part of the SQL WHERE clause
model->setFilter("TestSuite.active='TRUE' AND relTblAl_4.active='TRUE' AND relTblal_5.active='TRUE'");

model->select();
ui->tableView_TestSuite->setItemDelegate(new QSqlRelationalDelegate(ui->tableView_TestSuite));
ui->tableView_TestSuite->show();