Skip to content

Commit a49a423

Browse files
committed
sort
1 parent b7b998d commit a49a423

File tree

4 files changed

+162
-10
lines changed

4 files changed

+162
-10
lines changed

Diff for: QListView/README.en.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,85 @@
33
## 1. Custom Widget Item
44
[Run CustomWidgetItem.py](CustomWidgetItem.py)
55

6+
use `setIndexWidget(QModelIndex, QWidget)` to set custom `QWidget`
7+
68
![CustomWidgetItem](ScreenShot/CustomWidgetItem.png)
79

810
## 2. Custom Widget Sort Item
911
[Run CustomWidgetSortItem.py](CustomWidgetSortItem.py)
1012

11-
![CustomWidgetSortItem](ScreenShot/CustomWidgetSortItem.gif)
13+
1. set QListView proxy model, use `QSortFilterProxyModel`
14+
2. rewrite `lessThan` method to sort
15+
16+
![CustomWidgetSortItem](ScreenShot/CustomWidgetSortItem.gif)
17+
18+
## 3, Custom Role And Sort
19+
[Run SortItemByRole.py](SortItemByRole.py)
20+
21+
Demand:
22+
1. 5 categories(Tang, Song, Yuan, Ming, Qing) and Unclassified
23+
2. selected Tang, the result is Tang, Song, Yuan, Ming, Qing, Unclassified
24+
3. selected Song, the result is Song, Tang, Yuan, Ming, Qing, Unclassified
25+
4. selected Yuan, the result is Yuan, Tang, Song, Ming, Qing, Unclassified
26+
5. Cancel sorting then Restore, like: Unclassified, Tang, Tang, Ming, Qing, Unclassified, Song, Yuan, Unclassified
27+
28+
Method:
29+
1. define `IdRole = Qt.UserRole + 1` Used to restore default sort
30+
2. define `ClassifyRole = Qt.UserRole + 2` Used for sorting by sorting number
31+
3. define 5 classify id
32+
```python
33+
NameDict = {
34+
'Tang': ['Tang', 0],
35+
'Song': ['Song', 1],
36+
'Yuan': ['Yuan', 2],
37+
'Ming': ['Ming', 3],
38+
'Qing': ['Qing', 4],
39+
}
40+
IndexDict = {
41+
0: 'Tang',
42+
1: 'Song',
43+
2: 'Yuan',
44+
3: 'Ming',
45+
4: 'Qing',
46+
}
47+
```
48+
4. item `setData(id, IdRole)` Used to restore default sort
49+
5. item `setData(cid, ClassifyRole)` Used to record classification
50+
6. inherit `QSortFilterProxyModel` and add `setSortIndex(self, index)` mthod, The purpose is to keep some classify always top
51+
```python
52+
def setSortIndex(self, index):
53+
self._topIndex = index
54+
```
55+
7. inherit `QSortFilterProxyModel` and rewrite `lessThan` mthod, if classify id is equal to top id, then modify it -1
56+
```python
57+
if self.sortRole() == ClassifyRole and \
58+
source_left.column() == self.sortColumn() and \
59+
source_right.column() == self.sortColumn():
60+
# get classify id
61+
leftIndex = source_left.data(ClassifyRole)
62+
rightIndex = source_right.data(ClassifyRole)
63+
64+
# AscendingOrder
65+
if self.sortOrder() == Qt.AscendingOrder:
66+
# keep always top
67+
if leftIndex == self._topIndex:
68+
leftIndex = -1
69+
if rightIndex == self._topIndex:
70+
rightIndex = -1
71+
72+
return leftIndex < rightIndex
73+
```
74+
8. restore default sort
75+
```python
76+
self.fmodel.setSortRole(IdRole)
77+
self.fmodel.sort(0)
78+
```
79+
9. sort by classify id, must reset `setSortRole` to other
80+
```python
81+
self.fmodel.setSortIndex(1)
82+
self.fmodel.setSortRole(IdRole)
83+
self.fmodel.setSortRole(ClassifyRole)
84+
self.fmodel.sort(0)
85+
```
86+
87+
![SortItemByRole](ScreenShot/SortItemByRole.gif)

Diff for: QListView/README.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,85 @@
33
## 1、显示自定义Widget
44
[运行 CustomWidgetItem.py](CustomWidgetItem.py)
55

6+
通过设置 `setIndexWidget(QModelIndex, QWidget)` 可以设置自定义 `QWidget`
7+
68
![CustomWidgetItem](ScreenShot/CustomWidgetItem.png)
79

810
## 2、显示自定义Widget并排序
911
[运行 CustomWidgetSortItem.py](CustomWidgetSortItem.py)
1012

11-
![CustomWidgetSortItem](ScreenShot/CustomWidgetSortItem.gif)
13+
1. 对QListView设置代理 `QSortFilterProxyModel`
14+
2. 重写model的 `lessThan` 方法进行排序
15+
16+
![CustomWidgetSortItem](ScreenShot/CustomWidgetSortItem.gif)
17+
18+
## 3、自定义角色排序
19+
[运行 SortItemByRole.py](SortItemByRole.py)
20+
21+
需求:
22+
1. 5种分类(唐、宋、元、明、清) 和 未分类
23+
2. 选中唐则按照 唐、宋、元、明、清、未分类排序
24+
3. 选中宋则按照 宋、唐、元、明、清、未分类排序
25+
4. 选中元则按照 元、唐、宋、明、清、未分类排序
26+
5. 取消排序则恢复到加载时候顺序,如:未分类、唐、唐、明、清、未分类、宋、元、未分类
27+
28+
思路:
29+
1. 定义`IdRole = Qt.UserRole + 1` 用于恢复默认排序
30+
2. 定义`ClassifyRole = Qt.UserRole + 2` 用于按照分类序号排序
31+
3. 定义5种分类的id
32+
```python
33+
NameDict = {
34+
'': ['Tang', 0],
35+
'': ['Song', 1],
36+
'': ['Yuan', 2],
37+
'': ['Ming', 3],
38+
'': ['Qing', 4],
39+
}
40+
IndexDict = {
41+
0: '',
42+
1: '',
43+
2: '',
44+
3: '',
45+
4: '',
46+
}
47+
```
48+
4. item设置 `setData(id, IdRole)` 用于恢复默认排序
49+
5. item设置 `setData(cid, ClassifyRole)` 用于标识该item的分类
50+
6. 继承 `QSortFilterProxyModel` 增加 `setSortIndex(self, index)` 方法, 目的在于记录要置顶(不参与排序)的分类ID
51+
```python
52+
def setSortIndex(self, index):
53+
self._topIndex = index
54+
```
55+
7. 继承 `QSortFilterProxyModel` 重写 `lessThan` 方法, 判断分类ID是否等于要置顶的ID, 如果是则修改为-1, 这样就永远在最前面
56+
```python
57+
if self.sortRole() == ClassifyRole and \
58+
source_left.column() == self.sortColumn() and \
59+
source_right.column() == self.sortColumn():
60+
# 获取左右两个的分类
61+
leftIndex = source_left.data(ClassifyRole)
62+
rightIndex = source_right.data(ClassifyRole)
63+
64+
# 升序
65+
if self.sortOrder() == Qt.AscendingOrder:
66+
# 保持在最前面
67+
if leftIndex == self._topIndex:
68+
leftIndex = -1
69+
if rightIndex == self._topIndex:
70+
rightIndex = -1
71+
72+
return leftIndex < rightIndex
73+
```
74+
8. 恢复默认排序
75+
```python
76+
self.fmodel.setSortRole(IdRole) # 必须设置排序角色为ID
77+
self.fmodel.sort(0) # 排序第一列按照ID升序
78+
```
79+
9. 根据分类排序, 这里要注意要先通过 `setSortRole` 设置其它角色再设置目标角色
80+
```python
81+
self.fmodel.setSortIndex(1)
82+
self.fmodel.setSortRole(IdRole)
83+
self.fmodel.setSortRole(ClassifyRole)
84+
self.fmodel.sort(0)
85+
```
86+
87+
![SortItemByRole](ScreenShot/SortItemByRole.gif)

Diff for: QListView/SortItemByRole.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212
from random import choice
1313

14-
from PyQt5.QtCore import QSortFilterProxyModel, Qt, QTimer
14+
from PyQt5.QtCore import QSortFilterProxyModel, Qt
1515
from PyQt5.QtGui import QStandardItem, QStandardItemModel
1616
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QListView, QPushButton
1717

@@ -49,7 +49,7 @@ def lessThan(self, source_left, source_right):
4949
# 保持在最前面
5050
if leftIndex == self._topIndex:
5151
leftIndex = -1
52-
elif rightIndex == self._topIndex:
52+
if rightIndex == self._topIndex:
5353
rightIndex = -1
5454

5555
return leftIndex < rightIndex
@@ -71,7 +71,6 @@ def lessThan(self, source_left, source_right):
7171
3: '明',
7272
4: '清',
7373
}
74-
BigIndex = 100
7574

7675
IdRole = Qt.UserRole + 1 # 用于恢复排序
7776
ClassifyRole = Qt.UserRole + 2 # 用于按照分类序号排序
@@ -102,11 +101,12 @@ def restoreSort(self):
102101

103102
def sortByClassify(self):
104103
self.fmodel.setSortIndex(NameDict.get(
105-
self.sender().text(), ['', BigIndex])[1])
106-
self.restoreSort()
104+
self.sender().text(), ['', 100])[1])
105+
# self.restoreSort()
106+
self.fmodel.setSortRole(IdRole)
107107
# 按照给定的分类排序(这里注意还要按照把给定分类的放在最前面)
108108
self.fmodel.setSortRole(ClassifyRole)
109-
QTimer.singleShot(100, lambda: self.fmodel.sort(0))
109+
self.fmodel.sort(0)
110110

111111
def _initItems(self):
112112
# 初始化Items
@@ -120,7 +120,7 @@ def _initItems(self):
120120
classifies = [v[1] for v in NameDict.values()]
121121
for i in range(5):
122122
# 添加5个100, 用于模拟没有分类, 排序的时候就显示在最后面
123-
classifies.append(BigIndex)
123+
classifies.append(100)
124124
print(classifies) # [4, 2, 0, 3, 1, 100, 100, 100, 100, 100]
125125

126126
# 生成50个Item

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
66

77
## 目录
88

9-
| 分类 | 控件名 |
9+
| 分类 | 控件 |
1010
|:-------|:-------|
1111
| ActiveX | [QAxWidget](QAxWidget)
1212
| 日历 | [QCalendarWidget](QCalendarWidget)

0 commit comments

Comments
 (0)