Sorry to bump this, but I've found another solution and I thought that someone may find it helpful :
I've built a class inheriting from QSqlTableModel rather than QSqlRelationalTableModel and chose to override the selectStatement and orderByClause methods.
Everything works well except that you won't be able to use setSort() if you want to order by a joined column. But since it allows you to provide your own SELECT query, it's not a real problem. setFilter and all other methods work as expected, which is very convenient 
QString MyClass
::QUERY = QString("SELECT t1.col1, t1.col2, t1.col3, t2.col1, t2.col2 FROM table1 t1 JOIN table2 t2 ON t1.col2 = t2.col1");
QString MyClass::QUERY = QString("SELECT t1.col1, t1.col2, t1.col3, t2.col1, t2.col2 FROM table1 t1 JOIN table2 t2 ON t1.col2 = t2.col1");
QString MyClass::ORDER_BY = QString("ORDER BY t2.col2 ASC");
To copy to clipboard, switch view to plain text mode
QString MyClass
::selectStatement() const {
// Sets filter (WHERE clause) :
if(!this->filter().isEmpty())
.append(this->filter());
// Sets sort (ORDER BY clause) :
.append(this->orderByClause());
return stmt;
}
QString MyClass
::orderByClause() const {
return MyClass::ORDER_BY;
}
QString MyClass::selectStatement() const
{
QString stmt = MyClass::QUERY;
// Sets filter (WHERE clause) :
if(!this->filter().isEmpty())
stmt.append(QLatin1String(" "))
.append(QLatin1String("WHERE "))
.append(this->filter());
// Sets sort (ORDER BY clause) :
stmt.append(QLatin1String(" "))
.append(this->orderByClause());
return stmt;
}
QString MyClass::orderByClause() const
{
return MyClass::ORDER_BY;
}
To copy to clipboard, switch view to plain text mode
Bookmarks