QSortFilterProxyModel不应用SortR

2024-10-03 09:16:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个QTableView,其中有一个子类QSqlQueryModel和一个qsortfilterproxy模型。底层数据库中的某些列包含浮点数,它们按字符串的形式进行排序。示例:

-0.06
-1.45
 0.02
 0.05
 ...

使用Python/PyQt,我从this accepted answer实现了概述的解决方案,但它不起作用。在

我的子类QSqlQueryModel:

^{pr2}$

设置代理:

proxy = QSortFilterProxyModel(self)
proxy.setSortRole(CatalogModel.SortRole)

我自己的SortRole没有出现在模型中,带有浮点数的列仍然像字符串一样排序。 为什么这个解决方案不起作用?我错过什么了吗?在


Tags: 字符串模型数据库示例排序解决方案子类形式
2条回答

latest official documentation

Behind the scene, the view calls the sort() virtual function on the model to reorder the data in the model. To make your data sortable, you can either implement sort() in your model, or use a QSortFilterProxyModel to wrap your model QSortFilterProxyModel provides a generic sort() reimplementation that operates on the sortRole() (Qt::DisplayRole by default) of the items and that understands several data types, including int, QString, and QDateTime. For hierarchical models, sorting is applied recursively to all child items. String comparisons are case sensitive by default; this can be changed by setting the sortCaseSensitivity property.

实现您自己的sort()也可以是一个选项,但是:

An alternative approach to sorting is to disable sorting on the view and to impose a certain order to the user. This is done by explicitly calling sort() with the desired column and order as arguments on the QSortFilterProxyModel (or on the original model if it implements sort()). For example:

    proxyModel->sort(2, Qt::AscendingOrder);

QSortFilterProxyModel can be sorted by column -1, in which case it returns to the sort order of the underlying source model.

可以按以下方式将-1传递给该方法:

^{pr2}$

请注意,您在那里粘贴的两行没有分号。由于语法错误,代码甚至不能像那样编译。最好粘贴正确的代码。在

如果它不起作用,就实现你的排序。在

我想你需要打开分类开关。比如:

proxy.sort(-1, Qt.AscendingOrder);

根据docshttp://doc.qt.io/qt-4.8/qsortfilterproxymodel.html-1将打开底层模型的排序。这可能是你需要的。在

相关问题 更多 >