PDA

View Full Version : QSqlTableModel::setQuery



leknarf
4th September 2009, 16:14
Hello, this is my first post to QtCentre.

What's the proper handling for QSqlTableModel::setQuery()? My attempts return all rows extant in the db table. Yet the query (tested on the cmd-line) should have returned only a few rows.

lastError() yields nothing.
How does one get text from a model object's query() function?

I have a situation where I need more control over the rows displayed from a QSqlRelationTableModel object than can be achieved with a "where" clause filter. The documentation states that setQuery() is normally not called directly on a model. Am I finding out why now? ;)

Thanks in advance!
Scott

caduel
5th September 2009, 10:08
try using QSqlQueryModel or call QSqlTableModel::setFilter(), only passing the where clause

leknarf
7th September 2009, 07:03
try using QSqlQueryModel or call QSqlTableModel::setFilter(), only passing the where clause

Thanks, but I don't think either will work for my purposes.

I'm looking for a solution that provides read&write access to the table view (that nixes QSqlQueryModel). And I'd like to use an SQL query that joins more than just one table. I think that eliminates setFilter(). eg:

SELECT foo.name FROM foo, bar WHERE foo_id = bar_id;

I was hoping that I could pass a query like this to the table model's setQuery() method, "filtering" the records displayed in the table view and maintaining read&write access. My attempts so far are erroneously displaying all rows, rather than those that satisfy the where clause. (Note that my test query yields correct results from the cmd-line.)

ChrisW67
7th September 2009, 08:57
Could you achieve the filtering using a QSortFilterProxyModel on top of your QSqlTableModel ?