PDA

View Full Version : qsqltablemodel "order by" clause in setfilter?



jtdavidson
7th August 2010, 07:07
I've been using an "Order By" clause in qsqltablemodel::setfilter, because i need to sort a table by two columns, and as far as I can tell qsqltablemodel::setSort only allows you to sort by one column.
Order By *does* work in setFilter, but is it supported? I've never seen it in the documentation. I ask because, if I assign to a qtableview a qsqltablemodel model that has an order by clause in its filter, and if that view has sorting enabled, then resorting using the view's sort function produces an empty view.
So I'm guessing that I shouldn't be using Order By in setFilter? Is there some other way to sort a tablemodel by 2 columns? I can't use a querymodel because they are read only, and I need to write to the sorted table.

thanks

John

saa7_go
7th August 2010, 10:14
maybe, you can do like following


model->setTable("tablename");
model->setFilter("1=1 ORDER BY col1 ASC, col2 DESC");
model->select();

ChrisW67
8th August 2010, 00:38
You could create your own writeable model derived from QSqlQueryModel. An example is in the "Query Model Example" you can find in Assistant (and the QSqlQueryModel docs).

You don't say why you want the rows returned in sorted order or how big the data set is. If the size is manageable and the sort is for display purposes then a customised QSortFilterProxyModel might be of use.

jtdavidson
10th August 2010, 01:51
That's what I am doing, but that breaks if it's on a table view that performs its own sort when the user clicks on a sort arrow in the horizontal header row: the model returns an empty set, rather than a sorted set.

jtdavidson
10th August 2010, 01:53
Sorry, that previous post was in reference to:


maybe, you can do like following

model->setTable("tablename");
model->setFilter("1=1 ORDER BY col1 ASC, col2 DESC");
model->select();

waynew
10th August 2010, 01:58
You can always do this: view->setSortingEnabled(FALSE);

To disable user sorting.