在 PyQt 模型中按多列排序 [英] Sort by Multiple Columns in PyQt Model

查看:85
本文介绍了在 PyQt 模型中按多列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在基于模型/视图的 PyQt 应用程序中使用与以下 SQL 语句等效的 PyQt:

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

如何在 QSqlTableModel 中按多列排序,尤其是因为 setSort() 接受单个 column 参数?

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

推荐答案

似乎有一种替代 setSort() 的方法,称为 setFilter().来自 PyQt 文档:

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

QSqlTableModel.setFilter (self, QString filter)

QSqlTableModel.setFilter (self, QString filter)

将当前过滤器设置为过滤器.

Sets the current filter to filter.

过滤器是一个没有关键字 WHERE 的 SQL WHERE 子句(对于例如,name='Josephine').

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

因此,这解决了问题:

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

never_zero 字段的位置(惊喜,惊喜)永远不会为零.

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

这篇关于在 PyQt 模型中按多列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆