You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QTreeWidget排序问题:固定‘Select All’项于顶部,其余项按字母排序

解决方案:固定"Select All"在QTreeWidget顶部并排序其余项

问题分析

你的核心需求是让"Select All"项始终固定在列表顶部,其余项按字母顺序排序,但当前重写的__lt__方法逻辑不完整,导致排序异常。主要问题包括:

  • 未处理**对方项(other)**是否需要跳过排序的情况
  • 比较逻辑方向错误(示例中使用>导致反向排序)
  • 未统一处理文本大小写的比较逻辑

修正后的代码

import sys
from typing import Optional

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QWidget

_SKIP_SORT_ROLE: int = (Qt.UserRole + 4)


class TreeWidgetItem(QTreeWidgetItem):
    """
    让"Select All"固定在顶部,其余项按字母排序
    """

    def __lt__(self, other):
        column = self.treeWidget().sortColumn()
        
        # 获取两项的跳过排序标记
        skip_self = self.data(column, _SKIP_SORT_ROLE)
        skip_other = other.data(column, _SKIP_SORT_ROLE)

        # 情况1:当前项是要固定的,对方不是 → 当前项排在前面,返回False(self < other 不成立)
        if skip_self and not skip_other:
            return False
        # 情况2:对方是要固定的,当前项不是 → 当前项排在后面,返回True(self < other 成立)
        if not skip_self and skip_other:
            return True
        # 情况3:两项都要固定或都不要 → 按文本小写字母排序
        return self.text(column).lower() < other.text(column).lower()


class TreeWidget(QTreeWidget):

    def __init__(self, parent: Optional[QWidget] = None):
        super(TreeWidget, self).__init__(parent)

        # 添加固定项
        item = TreeWidgetItem(self)
        item.setText(0, 'Select All')
        item.setData(0, _SKIP_SORT_ROLE, True)

        # 添加测试项
        for i in range(10):
            item = TreeWidgetItem(self)
            item.setText(0, f'Item {i}')
        for i in range(10):
            item = TreeWidgetItem(self)
            item.setText(0, f'Item {i}')

        self.setSortingEnabled(True)
        # 默认按第0列升序排序
        self.sortByColumn(0, Qt.AscendingOrder)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    tree = TreeWidget()
    tree.show()
    sys.exit(app.exec())

关键逻辑说明

  • 跳过排序的判断:通过_SKIP_SORT_ROLE标记区分固定项和普通项,覆盖两种交叉比较的场景
  • 排序优先级:固定项始终优先于普通项,普通项之间按文本小写字母升序排列(避免大小写影响排序结果)
  • 默认排序设置:显式调用sortByColumn确保初始排序方向正确

内容的提问来源于stack exchange,提问作者laurapons

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 11:45:03