Sort by Multiple Columns in PyQt Model

大城市里の小女人 提交于 2019-12-13 19:13:15

问题


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:


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.



来源:https://stackoverflow.com/questions/9557394/sort-by-multiple-columns-in-pyqt-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!