禁用 QTreeView 中子项的排序
我正在使用 pyQt。如何禁用 QTreeView/StandardItemModel 中的子项排序?
I am using pyQt. How can I disable child items sorting in QTreeView/StandardItemModel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 QSortFilterProxyModel 并重新实现其 lessThan 方法。
或者,创建 QStandardItem 的子类并重新实现其 小于运算符。
这是一个演示后一种方法的简单示例:
You could use a QSortFilterProxyModel and reimplement its lessThan method.
Alternatively, create a subclass of QStandardItem and reimplement its less than operator.
Here's a simple example that demonstrates the latter approach:
在 QTreeView 实例上调用 setSortingEnabled(bool) 。 这里是c++和这里是链接到此函数的 pyqt api 文档
Call
setSortingEnabled(bool)
on your QTreeView instance. Here is the corresponding docu for c++ and here is the link to pyqt api docu for this function如果您希望使用 SortFilterProxyModel,这也适用。
This works as well if you wish to use a SortFilterProxyModel.