|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Created on 2018年12月27日 |
| 6 | +@author: Irony |
| 7 | +@site: https://pyqt5.com https://github.com/892768447 |
| 8 | +@email: 892768447@qq.com |
| 9 | +@file: QListView.SortItemByRole |
| 10 | +@description: |
| 11 | +""" |
| 12 | +from random import choice |
| 13 | + |
| 14 | +from PyQt5.QtCore import QSortFilterProxyModel, Qt, QTimer |
| 15 | +from PyQt5.QtGui import QStandardItem, QStandardItemModel |
| 16 | +from PyQt5.QtWidgets import QWidget, QVBoxLayout, QListView, QPushButton |
| 17 | + |
| 18 | + |
| 19 | +__Author__ = """By: Irony |
| 20 | +QQ: 892768447 |
| 21 | +Email: 892768447@qq.com""" |
| 22 | +__Copyright__ = "Copyright (c) 2018 Irony" |
| 23 | +__Version__ = "Version 1.0" |
| 24 | + |
| 25 | + |
| 26 | +class SortFilterProxyModel(QSortFilterProxyModel): |
| 27 | + |
| 28 | + def __init__(self, *args, **kwargs): |
| 29 | + super(SortFilterProxyModel, self).__init__(*args, **kwargs) |
| 30 | + self._topIndex = 0 |
| 31 | + |
| 32 | + def setSortIndex(self, index): |
| 33 | + self._topIndex = index |
| 34 | + print('在最前面的序号为:', index) |
| 35 | + |
| 36 | + def lessThan(self, source_left, source_right): |
| 37 | + if not source_left.isValid() or not source_right.isValid(): |
| 38 | + return False |
| 39 | + |
| 40 | + if self.sortRole() == ClassifyRole and \ |
| 41 | + source_left.column() == self.sortColumn() and \ |
| 42 | + source_right.column() == self.sortColumn(): |
| 43 | + # 获取左右两个的分类 |
| 44 | + leftIndex = source_left.data(ClassifyRole) |
| 45 | + rightIndex = source_right.data(ClassifyRole) |
| 46 | + |
| 47 | + # 升序 |
| 48 | + if self.sortOrder() == Qt.AscendingOrder: |
| 49 | + # 保持在最前面 |
| 50 | + if leftIndex == self._topIndex: |
| 51 | + leftIndex = -1 |
| 52 | + elif rightIndex == self._topIndex: |
| 53 | + rightIndex = -1 |
| 54 | + |
| 55 | + return leftIndex < rightIndex |
| 56 | + |
| 57 | + return super(SortFilterProxyModel, self).lessThan(source_left, source_right) |
| 58 | + |
| 59 | + |
| 60 | +NameDict = { |
| 61 | + '唐': ['Tang', 0], |
| 62 | + '宋': ['Song', 1], |
| 63 | + '元': ['Yuan', 2], |
| 64 | + '明': ['Ming', 3], |
| 65 | + '清': ['Qing', 4], |
| 66 | +} |
| 67 | +IndexDict = { |
| 68 | + 0: '唐', |
| 69 | + 1: '宋', |
| 70 | + 2: '元', |
| 71 | + 3: '明', |
| 72 | + 4: '清', |
| 73 | +} |
| 74 | +BigIndex = 100 |
| 75 | + |
| 76 | +IdRole = Qt.UserRole + 1 # 用于恢复排序 |
| 77 | +ClassifyRole = Qt.UserRole + 2 # 用于按照分类序号排序 |
| 78 | + |
| 79 | + |
| 80 | +class Window(QWidget): |
| 81 | + |
| 82 | + def __init__(self, *args, **kwargs): |
| 83 | + super(Window, self).__init__(*args, **kwargs) |
| 84 | + self.resize(600, 400) |
| 85 | + layout = QVBoxLayout(self) |
| 86 | + self.listView = QListView(self) |
| 87 | + self.listView.setEditTriggers(QListView.NoEditTriggers) |
| 88 | + layout.addWidget(self.listView) |
| 89 | + layout.addWidget(QPushButton('恢复默认顺序', self, clicked=self.restoreSort)) |
| 90 | + layout.addWidget(QPushButton('唐', self, clicked=self.sortByClassify)) |
| 91 | + layout.addWidget(QPushButton('宋', self, clicked=self.sortByClassify)) |
| 92 | + layout.addWidget(QPushButton('元', self, clicked=self.sortByClassify)) |
| 93 | + layout.addWidget(QPushButton('明', self, clicked=self.sortByClassify)) |
| 94 | + layout.addWidget(QPushButton('清', self, clicked=self.sortByClassify)) |
| 95 | + |
| 96 | + self._initItems() |
| 97 | + |
| 98 | + def restoreSort(self): |
| 99 | + # 恢复默认排序 |
| 100 | + self.fmodel.setSortRole(IdRole) # 必须设置排序角色为ID |
| 101 | + self.fmodel.sort(0) # 排序第一列按照ID升序 |
| 102 | + |
| 103 | + def sortByClassify(self): |
| 104 | + self.fmodel.setSortIndex(NameDict.get( |
| 105 | + self.sender().text(), ['', BigIndex])[1]) |
| 106 | + self.restoreSort() |
| 107 | + # 按照给定的分类排序(这里注意还要按照把给定分类的放在最前面) |
| 108 | + self.fmodel.setSortRole(ClassifyRole) |
| 109 | + QTimer.singleShot(100, lambda: self.fmodel.sort(0)) |
| 110 | + |
| 111 | + def _initItems(self): |
| 112 | + # 初始化Items |
| 113 | + self.dmodel = QStandardItemModel(self.listView) |
| 114 | + self.fmodel = SortFilterProxyModel(self.listView) |
| 115 | + self.fmodel.setSourceModel(self.dmodel) |
| 116 | + self.listView.setModel(self.fmodel) |
| 117 | + |
| 118 | + keys = list(NameDict.keys()) |
| 119 | + print(keys) # ['清', '元', '唐', '明', '宋'] |
| 120 | + classifies = [v[1] for v in NameDict.values()] |
| 121 | + for i in range(5): |
| 122 | + # 添加5个100, 用于模拟没有分类, 排序的时候就显示在最后面 |
| 123 | + classifies.append(BigIndex) |
| 124 | + print(classifies) # [4, 2, 0, 3, 1, 100, 100, 100, 100, 100] |
| 125 | + |
| 126 | + # 生成50个Item |
| 127 | + for i in range(50): |
| 128 | + # name = keys[i % 4] # 随机取一个朝代 |
| 129 | + item = QStandardItem() |
| 130 | + # 设置ID角色 |
| 131 | + item.setData(i, IdRole) |
| 132 | + # 设置分类角色 |
| 133 | + c = choice(classifies) |
| 134 | + item.setData(c, ClassifyRole) |
| 135 | + # 设置显示内容 |
| 136 | + item.setText('Name: {}\t\tId: {}\t\tClassify: {}'.format( |
| 137 | + IndexDict.get(c, '其它'), i, c)) |
| 138 | + self.dmodel.appendRow(item) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + import sys |
| 143 | + import cgitb |
| 144 | + sys.excepthook = cgitb.enable(1, None, 5, '') |
| 145 | + from PyQt5.QtWidgets import QApplication |
| 146 | + app = QApplication(sys.argv) |
| 147 | + w = Window() |
| 148 | + w.show() |
| 149 | + sys.exit(app.exec_()) |
0 commit comments