Sort by Multiple Columns in PyQt Model

2019-07-31 08:38发布

I want to use the PyQt equivalent of the following SQL statement in my model/view-based PyQt application:

SELECT * FROM table ORDER BY foo, bar

How do I sort by multiple columns in a QSqlTableModel, especially since setSort() accepts a single column argument?

1条回答
不美不萌又怎样
2楼-- · 2019-07-31 09:06

It seems there's an alternative to setSort(), called setFilter(). From the PyQt docs:

QSqlTableModel.setFilter (self, QString filter)

Sets the current filter to filter.

The filter is a SQL WHERE clause without the keyword WHERE (for example, name='Josephine').

Ergo, this solves the problem:

fooModel.setFilter("never_zero != 0 ORDER BY foo, bar")

where the never_zero field is (surprise, surprise) never zero.

查看更多
登录 后发表回答